unity 编辑器 对比两次节点信息 查看新增节点和消失节点。

目的 在一个纯净的游戏环境下 , 查看释放技能后, 是否有节点一直增长。 判断两层子节点

 

 

 public class FCheckNodeChildCount : OdinEditorWindow
    {
        public static FCheckNodeChildCount _instance = null;

        [MenuItem("Tools/CheckNodeChildCount")]
        public static void Open()
        {
            System.Type T = System.Type.GetType("UnityEditor.GameView,UnityEditor");
            var gameWindow = EditorWindow.GetWindow(T);

            _instance = GetWindow<FCheckNodeChildCount>();
            _instance.Show();
            _instance.titleContent = new GUIContent("FCheckNodeChildCount");
            _instance.position = new Rect(gameWindow.position.x, gameWindow.position.y, 600, 400);

            _instance.parentObj = GameObject.Find("ControllerObject(Clone)");

            if (_instance.parentObj == null)
            {
                EditorUtility.DisplayDialog("", "1当前没有指定总节点ControllerObject(Clone) ?", "ok");
                return;
            }
            _instance.parentNode = _instance.parentObj.transform;
        }
        [PropertyOrder(0)]
        private GameObject parentObj;
        [SceneObjectsOnly]
        public Transform parentNode;
        [Button("$ButtonName"), PropertyOrder(1)]
        public void CompareNodeInfo()
        {
            newTransList.Clear();
            lostTransList.Clear();
            addStr.Clear();
            lostStr.Clear();
            if (parentNode == null)
            {
                _instance.parentObj = GameObject.Find("ControllerObject(Clone)");

                if (_instance.parentObj != null)
                {
                    _instance.parentNode = _instance.parentObj.transform;
                }
                EditorUtility.DisplayDialog("", "2当前没有指定总节点?", "ok");
                return;
            }
            //只获取二级子节点就可以
            int firstChildCount = parentNode.childCount;
            if (isFirstClick)
            {
                for (int i = 0; i < firstChildCount; ++i)
                {
                    var firstChid = parentNode.GetChild(i);
                    firstTransList.Add(new TransHadInfo(firstChid, false));
                    int secondCount = firstChid.childCount;
                    for (int j = 0; j < secondCount; ++j)
                    {
                        var secondChid = firstChid.GetChild(j);
                        secondTransList.Add(new TransHadInfo(secondChid, false));
                    }
                }
                isFirstClick = false;
            }
            else
            {
                //重新读取所有节点,然后获取新节点:
                for (int i = 0; i < firstChildCount; ++i)
                {
                    var firstChid = parentNode.GetChild(i);
                    int findIndex = firstTransList.FindIndex(o => o.trans == firstChid);
                    if (findIndex != -1)
                    {
                        firstTransList[findIndex].isHad = true;
                    }
                    else
                    {
                        newTransList.Add(firstChid);
                        firstTransList.Add(new TransHadInfo(firstChid, true));
                    }
                    //第二层
                    int secondCount = firstChid.childCount;
                    for (int j = 0; j < secondCount; ++j)
                    {
                        var secondChid = firstChid.GetChild(j);
                        int secondChidIndex = secondTransList.FindIndex(o => o.trans == secondChid);
                        if (secondChidIndex != -1)
                        {
                            secondTransList[secondChidIndex].isHad = true;
                        }
                        else
                        {
                            newTransList.Add(secondChid);
                            secondTransList.Add(new TransHadInfo(secondChid, true));
                        }
                    }
                }

                //获取失去的节点:
                int curCount = firstTransList.Count;
                for (int i = curCount - 1; i >= 0; --i)
                {
                    if (firstTransList[i].isHad)
                    {
                        firstTransList[i].isHad = false;
                    }
                    else
                    {
                        firstTransList.RemoveAt(i);
                        lostTransList.Add(firstTransList[i]);
                    }
                }

                curCount = secondTransList.Count;
                for (int i = curCount - 1; i >= 0; --i)
                {
                    if (secondTransList[i].isHad)
                    {
                        secondTransList[i].isHad = false;
                    }
                    else
                    {
                        lostTransList.Add(secondTransList[i]);
                    }
                }
            }

            //得出结论:
            if (newTransList.Count > 0)
            {
                addStr.Append("新增个数:" + newTransList.Count + " \r\n");
            }
            foreach (var item in newTransList)
            {
                addStr.Append("Name:" + item.name + " 父节点:" + item.parent.name + " \r\n");
            }
            if (lostTransList.Count > 0)
            {
                lostStr.Append("减少总个数:" + lostTransList.Count);
            }
            foreach (var item in lostTransList)
            {
                lostStr.Append("Name:" + item.curName + " 父节点:" + item.parentName + " \r\n");
            }
            addInfos = addStr.ToString();
            lostInfos = lostStr.ToString();
        }
        [LabelText("新增:"), ShowIf("IsShowAddInfo")]
        [PropertyOrder(2), TextArea(10, 100)]
        public string addInfos = "";
        [PropertyOrder(3), TextArea(10, 100)]
        [LabelText("减少:"), ShowIf("IsShowLostInfo")]
        public string lostInfos = "";
        private StringBuilder addStr = new StringBuilder();
        private StringBuilder lostStr = new StringBuilder();
        [Button("清理并重新开始")]
        public void ClearInfo()
        {
            isFirstClick = true;
            firstTransList.Clear();
            secondTransList.Clear();
            addInfos = "";
            lostInfos = "";
        }

        private string ButtonName
        {
            get
            {
                if (isFirstClick)
                {
                    return "第一次获取当前信息";
                }
                else
                {
                    return "和之前节点对比";
                }
            }
        }

        private List<TransHadInfo> firstTransList = new List<TransHadInfo>(50);
        private List<TransHadInfo> secondTransList = new List<TransHadInfo>(500);
        private List<Transform> newTransList = new List<Transform>();
        private List<TransHadInfo> lostTransList = new List<TransHadInfo>();
        private bool isFirstClick = true;

        private bool IsShowAddInfo()
        {
            return newTransList.Count > 0;
        }

        private bool IsShowLostInfo()
        {
            return lostTransList.Count > 0;
        }

        private float timeCount = 1;
        protected override void OnGUI()
        {
            base.OnGUI();

            if (parentNode == null)
            {
                timeCount -= Time.deltaTime;
                if (timeCount < 0)
                {
                    _instance.parentObj = GameObject.Find("ControllerObject(Clone)");

                    if (_instance.parentObj != null)
                    {
                        _instance.parentNode = _instance.parentObj.transform;
                    }
                }
                timeCount = 1;
            }
        }

        private class TransHadInfo
        {
            public TransHadInfo(Transform t, bool isH)
            {
                trans = t;
                isHad = isH;
                curName = trans.name;
                parentName = trans.parent.name;
            }

            public string curName;
            public string parentName;
            public Transform trans;
            public bool isHad = false;
        }
    }

 

unity 编辑器 对比两次节点信息 查看新增节点和消失节点。

上一篇:VC中MFC程序的入口函数


下一篇:Spring事务