Unity点击两个物体生成贝塞尔曲线工具

using System;
using System.Collections.Generic;
using UnityEngine;

public class BCLine : MonoBehaviour
{
    public Gradient gradient;
    public static bool Cliked = false;
    public static BCLine SavingBC;
    public List<GameObject> BCChild=new List<GameObject>();

    
    private LineRenderer lineRenderer;

    private void Awake()
    {
        BCChild.Clear();
      
    }

    public void MakeBC(Vector3 Start,Vector3 End)
    {
        GameObject go = Instantiate(new GameObject(), transform.parent);
        BCChild.Add(go);
        go.AddComponent<Line>();
        go.GetComponent<Line>().startPoint = Start;
        go.GetComponent<Line>().endPoint = End;
        go.GetComponent<Line>().gradient = gradient;


    }

    public bool EachBC(Transform Target)
    {
        foreach (var VARIABLE in BCChild)
        {
            if (VARIABLE.GetComponent<Line>().endPoint ==Target.position)
            {
                BCChild.Remove(VARIABLE);
              Destroy(VARIABLE);
              return false;
            }
        }

        MakeBC(transform.position,Target.position);
        return true;
    }
    private void OnMouseDown()
    {
        

            if (!Cliked)
            {
                SavingBC = GetComponent<BCLine>();
               SavingBC.GetComponent<Outline>().enabled = true;
            }
            else   if (SavingBC!=GetComponent<BCLine>())
            {
                SavingBC.GetComponent<Outline>().enabled = false;
                SavingBC.EachBC(transform);
            }
            else
            {
                SavingBC.GetComponent<Outline>().enabled = false;
                SavingBC = null;
            }

        


        Cliked = !Cliked;
    }

   
    public class Line : MonoBehaviour
    {
        private LineRenderer lineRenderer;
        public Vector3 startPoint;
        public Vector3 endPoint;
        public Vector3 controlPoint;
        public Gradient gradient;
        public int numberOfPoints = 20;

        private void Start()
        {
            controlPoint = new Vector3(startPoint.x, startPoint.y, endPoint.z);
            lineRenderer = gameObject.AddComponent<LineRenderer>();
            lineRenderer.startWidth = GetComponent<LineRenderer>().startWidth = 0.2f;
            lineRenderer.material = Resources.Load<Material>("贝塞尔");
            lineRenderer.colorGradient = gradient;
        }

        private void Update()
        {
            Vector3[] positions = new Vector3[numberOfPoints];
            float t = 0f;
            for (int i = 0; i < numberOfPoints; i++)
            {
                positions[i] = CalculateBezierPoint(t, startPoint, endPoint, controlPoint);
                t += 1f / numberOfPoints;
            }
            lineRenderer.positionCount = numberOfPoints;
            lineRenderer.SetPositions(positions);
        }

        private Vector3 CalculateBezierPoint(float t, Vector3 startPoint, Vector3 endPoint, Vector3 controlPoint)
        {
            float u = 1 - t;
            float tt = t * t;
            float uu = u * u;

            Vector3 point = uu * startPoint;
            point += 2 * u * t * controlPoint;
            point += tt * endPoint;

            return point;
        }

    }
}

猜你喜欢

转载自blog.csdn.net/qq_58804985/article/details/129342109