unity LineRender結合多點觸摸 實現拖拽 重複畫線

之前我也關於linrender劃線寫過一篇博客 這個是最近項目中使用到的功能就研究了下 當然也歡迎大佬幫忙優化代碼
閒話不多說 先上劃線的代碼

    //畫線的端點
    public GameObject[] AllPosition;
    //移動的點
    public Transform MoveCube;
    //移動的座標
    public Vector3[] VecValue;
    int num = 0;
    //LineRenderer
    private LineRenderer lineRenderer;
    void Start()
    {
        //畫線
        //添加LineRenderer組件
        lineRenderer = gameObject.GetComponent<LineRenderer>();
        //設置顏色
        lineRenderer.startColor = new Color32(198, 171, 118, 255);
        lineRenderer.endColor = new Color32(198, 171, 118, 255);
        //設置寬度
        //lineRenderer.SetWidth(0.02f, 0.02f);
        lineRenderer.startWidth = 0.03f;
        lineRenderer.endWidth = 0.03f;
    }
    VecValue = new Vector3[AllPosition.Length];
        for (int i = 0; i < AllPosition.Length; i++)
        {
            VecValue[i] = AllPosition[i].transform.localPosition;
        }

相比較之前的劃線  多了一個需要移動的點跟目標點位 劃線方法 跟隨移動的Transform來不斷移動座標 目標座標就是設置的數組點 當然這個方法還需要在update中調用 並且  如果前面的節點不顯示 後面的節點也不畫線  由於是觸摸屏 所以要根據節點是否顯示 控制劃線 所以要有結點顯示與否的判定

//檢測數組中是否包含某些數據
    public bool DetectionData(int num)
    {
        if (num == 0)
        {
            return true;
        }
        //如果包含
        for (int i = 0; i < num + 1; i++)
        {
            if (!Number.Contains(i))
            {
                return false;
            }
        }
        return true;
    }
    public List<int> Number = new List<int>();
    public void LineMove()
    {
        for (int i = 0; i < AllPosition.Length; i++)
        {
            if (AllPosition[i].activeSelf)
            {
                if (!Number.Contains(i))
                {
                    Number.Add(i);
                    Maxnums = i;
                    NumberValueSet();
                }
            }
            else
            {
                if (Number.Count > i)
                {
                    Number.RemoveAt(i);
                    Maxnums = i - 1;
                }
            }
        }
    }

根據這樣的方法去判斷 前面的結點是否顯示  如果顯示 在開始劃線  劃線結束 將所有劃線的點座標跟隨物體的點座標 這樣移動的時候線條也跟着移動

public int Maxnums = 0;
    bool IsValue = true;
    bool IsLine = false;
    void Update()
    {
        LineMove();
        if (DetectionData(Maxnums) && Maxnums > 0)
        {
            AllActive(Maxnums + 1);
        }
    }
    //畫線方法
    public void AllActive(int MaxNum)
    {
        if (IsValue)
        {
            VecValue = new Vector3[MaxNum];
            for (int i = 0; i < MaxNum; i++)
            {
                VecValue[i] = AllPosition[i].transform.position;
            }
            MoveCube.position = VecValue[0];
            IsValue = false;
            IsLine = true;
        }
        if (IsLine)
        {
            //畫線
            lineRenderer.positionCount = MaxNum;
            MoveCube.position = Vector3.MoveTowards(MoveCube.position, VecValue[num], 5f * Time.deltaTime);
            for (int i = num; i < MaxNum; i++)
            {
                lineRenderer.SetPosition(i, MoveCube.position);
            }
            lineRenderer.SetPosition(num, MoveCube.position);

            if (MoveCube.position == VecValue[num])
            {
                num++;
                if (num > MaxNum - 1)
                {
                    num = MaxNum - 1;
                    IsLine = false;
                }
            }
        }
        if (!IsLine)
        {
            if (lineRenderer.positionCount != MaxNum)
            {
                lineRenderer.positionCount = MaxNum;
            }
            for (int i = 0; i < MaxNum; i++)
            {
                lineRenderer.SetPosition(i, AllPosition[i].transform.position);
            }
        }
    }

在update 中調用方法 並且判斷結點 顯示在開始劃線  

當然還有另外一種方法 就是將判斷結點是否顯示 放在單個結點上檢測  用一個控制器 控制這些劃線動畫 當然也更合理一些 不需要重新開始繪製線條

實現的效果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章