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 BCChild=new List();

    
    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();
        go.GetComponent().startPoint = Start;
        go.GetComponent().endPoint = End;
        go.GetComponent().gradient = gradient;


    }

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

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

            if (!Cliked)
            {
                SavingBC = GetComponent();
               SavingBC.GetComponent().enabled = true;
            }
            else   if (SavingBC!=GetComponent())
            {
                SavingBC.GetComponent().enabled = false;
                SavingBC.EachBC(transform);
            }
            else
            {
                SavingBC.GetComponent().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.startWidth = GetComponent().startWidth = 0.2f;
            lineRenderer.material = Resources.Load("贝塞尔");
            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;
        }

    }
}

你可能感兴趣的:(Unity工具,猴子系列,unity)