【筆記】unity 資源樹結構VirtualizingTreeView的使用 模型樹

【插件】VirtualizingTreeView

感謝插件來源:Virtualizing Tree View - 自購稀缺Unity資源

本插件的主要功能是加載本地資源生成UI目錄樹,DOME中的功能是生成跟Hierarchy面板一樣的UI,想當與該資源面板的副本。

而我這次需要的功能是:只顯示項目中模型的樹結構功能

一、在項目UI中添加TreeView.prefab預製體,同時添加一個空對象並綁定TreeViewDemo.cs腳本

二、把需要顯示的模型樹父對象設置Tag,子級不需要設置,然後通過添加判斷Tag條件來只顯示該模型樹

三、添加Tag判斷條件:在TreeViewDemo.cs腳本中的Start()方法裏在以下代碼中添加條件

IEnumerable<GameObject> dataItems = Resources.FindObjectsOfTypeAll<GameObject>().Where(go => !IsPrefab(go.transform) && go.transform.parent == null && go.tag == "Finish").OrderBy(t => t.transform.GetSiblingIndex());

四、點擊模型樹對象,獲得該模型:在TreeViewDemo.cs腳本中添加代碼如下

        private void OnSelectionChanged(object sender, SelectionChangedArgs e)
        {
            #if UNITY_EDITOR
            //Do something on selection changed (just syncronized with editor's hierarchy for demo purposes)
            UnityEditor.Selection.objects = e.NewItems.OfType<GameObject>().ToArray();
           #endif

            //獲得對象
            foreach (var item in e.NewItems.OfType<GameObject>().ToArray())
            {
                print("測試中。。。" + item.name);
                print(item.gameObject.transform.localPosition);
            }

            //獲得上一次點擊的對象
            foreach (var item in e.OldItems.OfType<GameObject>().ToArray())
            {
                print(item.gameObject);
            }
            
        }

OnSelectionChanged 此方法原本是在編輯器下運行的,用來展示同步效果的,我們使用時不要將代碼寫到 #if UNITY_EDITOR裏面即可。
           

===========項目緊急,其他功能尚未深入研究

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