Files
ichni_Creator_Studio/Assets/Scripts/DynamicUI/Hierarchy/HierarchyTab.cs
TRAfoer 1b3c3556a1 优化hierarchy
即时生成和删除tab(不会做动态刷新
基本上hierarchy的性能就到这了,之后还是做点辅助工具防止1000+的栏展开吧
(重要)bug测试不完全,需要再加改进
2025-02-12 01:57:18 +08:00

135 lines
4.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using Ichni.RhythmGame;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace Ichni.Editor
{
public partial class HierarchyTab : MonoBehaviour
{
public GameObject indentationLinePrefab;
public GameElement connectedGameElement;
public HierarchyTab parentTab;
public List<HierarchyTab> childTabList;
public List<int> IDqueue;
public int tabLayer;
public bool isSelected;
public bool isExpanded;
public RectTransform tabRect;
public LayoutElement layoutElement;
public RectTransform tabMainRect;
public Button tabButton;
public Button expandButton;
public Button gotoButton;
public TMP_Text tabButtonText;
public Hierarchy hierarchy;
public void SetTab(GameElement targetElement, GameElement parentElement)
{
connectedGameElement=targetElement;
tabButtonText.text = targetElement.elementName;
targetElement.connectedTab = this;
this.isExpanded = false;
this.isSelected = false;
this.childTabList = new List<HierarchyTab>();
if (parentElement == null)
{
this.tabLayer = 0;
this.parentTab = null;
this.transform.SetAsLastSibling();
}else{
this.parentTab = parentElement.connectedTab;
parentElement.connectedTab.childTabList.Add(this);
this.tabLayer = this.parentTab.tabLayer + 1;
this.transform.SetSiblingIndex(this.parentTab.transform.GetSiblingIndex() +
this.parentTab.connectedGameElement.childElementList.Count);
if (!this.parentTab.isExpanded)
{
this.isExpanded = false;
SetExpansion(false);
}
for (int i = 1; i <= this.tabLayer; i++)
{
float lineX = 30 * i;
Instantiate(indentationLinePrefab, tabRect).GetComponent<RectTransform>().anchoredPosition = new Vector2(lineX, 0);
}
}
float posX = -5 + 30 * tabLayer;
tabMainRect.anchoredPosition = new Vector2(posX, tabMainRect.anchoredPosition.y);
expandButton.onClick.AddListener(ExpandOrFold);
}
}
public partial class HierarchyTab
{
private int GetAllChildrenCount(HierarchyTab tab)
{
int c = tab.childTabList.Count;
for (int i = 0; i < tab.childTabList.Count; i++)
{
c += GetAllChildrenCount(tab.childTabList[i]);
}
return c;
}
private void ExpandOrFold()
{
this.childTabList.RemoveAll(s => s == null);
isExpanded=!isExpanded;
if(isExpanded){
expandButton.transform.Rotate(new Vector3(0,0,180));
foreach(GameElement i in connectedGameElement.childElementList){
HierarchyTab a=hierarchy.GenerateTab(i,connectedGameElement);
childTabList.Add(a);
}
}else{
expandButton.transform.Rotate(new Vector3(0,0,180));
for (int i = childTabList.Count-1; i>=0; i--)
{
childTabList[i].SetExpansion(isExpanded);
}
}
}
private void SetExpansion(bool expand)
{
if (!expand&&isExpanded)
{
foreach (var tab in childTabList)
{
tab.SetExpansion(expand);//false
}
}
if (!expand)
{
parentTab.childTabList.Remove(this);
Destroy(gameObject);
}
}
}
}