美文网首页
unity基于NOT_Lonely连线脚本CablesBuild

unity基于NOT_Lonely连线脚本CablesBuild

作者: Rayson | 来源:发表于2023-08-27 16:24 被阅读0次

    找到ACC_Trail脚本自动定位关掉

    using System;
    using System.Collections.Generic;
    using UnityEngine;
    using NOT_Lonely;
    [ExecuteAlways]
    public class CablesBuild : MonoBehaviour
    {
        public List<GameObject> towerList = new List<GameObject>();
        public Material[] newMaterials;
        private List<List<Transform>> _transList = new List<List<Transform>>();
        //前一次的位置,监听位置是否变化,实时更新
        private List<List<Vector3>> _pointsListLate = new List<List<Vector3>>();
        private List<ACC_Trail> _trails = new List<ACC_Trail>();
        [SerializeField][Header("电线连接的方向")] private LinkDirect _linkDirect = LinkDirect.后向;
        [SerializeField][Header("重力大小")] private float cableGravity= 0.005f;
        [SerializeField][Header("半径")] private float cableRadius = 0.05f;
        private void OnEnable()
        {
            UpdateCable();
        }
    
        enum LinkDirect
        {
            前向,
            后向
        }
        private string Flip(string str)//字符串转换
        {
            if (str.Contains("前"))
                return str.Replace("前", "后");
            else if (str.Contains("后"))
            {
                return str.Replace("后", "前");
            }
            else return string.Empty;
        }
        private void UpdateCable()
        {
           // Log.red("CableBuild.cs=>UpdateCable()");
            // 销毁
            for (int i=transform.childCount-1; i >=0; i--)
            {
                DestroyImmediate(transform.GetChild(i).gameObject);
            }
            _transList.Clear();
            _pointsListLate.Clear();
            _trails.Clear();
            
            for(int i=0;i<towerList.Count-1;i++)
            {
                for (int j =0; j < towerList[i].transform.childCount;j++)
                {
                    var childStart = towerList[i].transform.GetChild(j);
                    var word = _linkDirect == LinkDirect.前向 ? "Point前" : "Point后";
                    if (childStart.name.Contains(word))
                    {
                        try
                        {
                            //如果
                            var childEnd = towerList[i + 1].transform.Find(Flip(childStart.name));
                            if(childEnd)
                            {
                                var o = new GameObject($"Cable_{towerList[i].name}_{childStart.name}", typeof(ACC_Trail))
                                 {
                                     transform =
                                     {
                                         parent = this.transform
                                     }
                                 };
                                var trail = o.GetComponent<ACC_Trail>();
                                trail.controlPoints = new List<Transform>{childStart, childEnd};
                                trail.lengthDependentHeight = cableGravity;//弯曲度
                                // cablesTrail.lengthSegments = 12;//路径方向的段数
                                trail.thickness = new Vector2(cableRadius, cableRadius);//半径
                                trail.materials = newMaterials;
                                //...etc
                                trail.CreateFirstCableSegment(trail.transform);
                                trail.UpdateAllSequences();
    
                                //update
                                trail.UpdateCableTrail();
            
                                _transList.Add(trail.controlPoints);
                                var vs = new List<Vector3>();
                                //更新pointLate
                                trail.controlPoints.ForEach(t=>vs.Add(t.position));
                                _pointsListLate.Add(vs);
    
                                _trails.Add(trail);
                            }
                        }
                        catch (Exception e)
                        {
                            //Log.blue(e.ToString());
                        }
                    }
                }
            }
    
            // _pointsListLate = _transList;
        }
    
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                UpdateCable();
            }
            // 监听位置是否变化
            for (int i = 0; i < _transList.Count; i++)
            {
                for (int j = 0; j < _transList[i].Count; j++)
                {
                    if (_transList[i][j].position != _pointsListLate[i][j])
                    {
                        UpdateCable();
                        break;
                    }
                }
            }
        }
    }
    
    
    

    orient/strip%7CimageView2/2/w/1240)

    1.塔的预制体点位 前后名字要保持一致,不得有空格,否则会出现连不上的情况



    2.新建空物体,把脚本拖入到空物体上,并按照先后顺序排列。开关脚本可以刷新


    相关文章

      网友评论

          本文标题:unity基于NOT_Lonely连线脚本CablesBuild

          本文链接:https://www.haomeiwen.com/subject/fvswmdtx.html