feat:场景视图添加属性编辑,添加轮廓显示

This commit is contained in:
m0_75251201
2025-11-13 16:24:49 +08:00
parent 9b91218973
commit 8fcbdc5649
95 changed files with 2836 additions and 445 deletions

View File

@@ -1,14 +1,15 @@
using System;
using System.Collections;
using SceneView;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace SceneView
{
public class TreeViewNode : MonoBehaviour
{
public TreeViewNode? parent = null;
public TreeViewNode? parent;
public RectTransform? rectTransform;
public TextMeshProUGUI? text;
@@ -23,7 +24,7 @@ namespace SceneView
private string originalText = "";
private void Awake()
{
@@ -37,10 +38,7 @@ namespace SceneView
if (rectTransform == null)
{
rectTransform = GetComponent<RectTransform>();
if (rectTransform == null)
{
rectTransform = gameObject.AddComponent<RectTransform>();
}
if (rectTransform == null) rectTransform = gameObject.AddComponent<RectTransform>();
if (rectTransform == null)
{
@@ -62,7 +60,7 @@ namespace SceneView
// 创建按钮并检查是否已存在
if (label == null)
{
var button = ControlUtilities.CreateButton(rectTransform, buttonConfig, Expand);
var button = ControlUtilities.CreateButton(rectTransform, buttonConfig, null);
label = button.button;
text = button.text;
if (text == null)
@@ -78,10 +76,10 @@ namespace SceneView
{
var button = ControlUtilities.CreateButton(rectTransform, buttonConfig, null);
objectEnable = button.button;
objectText= button.text;
objectText = button.text;
button.text.text = "-";
button.button.image.color = Color.yellow;
var rect=objectEnable.GetComponent<RectTransform>();
var rect = objectEnable.GetComponent<RectTransform>();
rect.anchorMin = Vector2.one;
rect.anchorMax = Vector2.one;
rect.pivot = Vector2.one / 2;
@@ -154,18 +152,21 @@ namespace SceneView
verticalLayout.childControlWidth = true;
sizeFitter.verticalFit = ContentSizeFitter.FitMode.PreferredSize;
}
public void InsertChild(RectTransform childRectTransform)
{
if (child != null && childRectTransform != null)
{
// 将子节点添加到 content 容器中
childRectTransform.SetParent(child.transform, false);
}
}
private void OnExpand(GameObject go)
{
if (!go) return;
CanvasControl.FocusGameObject = go;
Expand();
}
private void Expand()
{
if (child == null)
@@ -198,14 +199,10 @@ namespace SceneView
}
if (child.activeSelf)
{
rectTransform.sizeDelta =
new Vector2(rectTransform.sizeDelta.x, buttonHeight + childRectTransform.sizeDelta.y);
}
else
{
rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, buttonHeight);
}
if (parent != null)
{
@@ -214,24 +211,53 @@ namespace SceneView
}
}
public IEnumerator DisplayGameObjectStructureCoroutine(GameObject targetObject, int depth = 0)
private IEnumerator DisplayGameObjectStructureCoroutineImpl(GameObject targetObject, int depth,
Action onComplete = null)
{
if (targetObject == null)
{
Debug.LogError("Target object is null.");
// 如果目标对象为空,则遍历场景中的所有对象
var rootObjects = SceneManager.GetActiveScene().GetRootGameObjects();
var totalRootObjects = rootObjects.Length;
var completedRootObjects = 0;
originalText = $"场景{SceneManager.GetActiveScene().name}";
if (text) text.text = originalText;
foreach (var rootObject in rootObjects)
{
if (!rootObject) continue;
var childNode = new GameObject($"{rootObject.name}").AddComponent<TreeViewNode>();
childNode.transform.SetParent(childRectTransform, false);
childNode.parent = this;
childNode.CreateUI();
StartCoroutine(childNode.DisplayGameObjectStructureCoroutineImpl(rootObject, depth + 1,
() =>
{
completedRootObjects++;
if (completedRootObjects == totalRootObjects)
{
// 所有根对象及其子对象都已处理完成
onComplete?.Invoke();
Expand();
}
}));
yield return null;
}
yield break;
}
if (targetObject.name == CanvasControl.ViewCanvasName)
yield break;
label?.onClick.RemoveAllListeners();
label?.onClick.AddListener(() =>OnExpand(targetObject));
ClearChildNodes();
originalText = GetIndentedName(targetObject.name, depth);
if (text != null) text.text = originalText;
objectEnable.onClick.AddListener(() => OnObjectEnable(targetObject, objectEnable, objectText));
UpdateObjButton(targetObject,objectEnable,objectText);
objectEnable.onClick.AddListener(() => OnObjectEnable(targetObject, objectEnable, objectText));
UpdateObjButton(targetObject, objectEnable, objectText);
var components = targetObject.GetComponents<Behaviour>();
foreach (var component in components)
@@ -262,7 +288,6 @@ namespace SceneView
// 计数器,用于跟踪子协程的数量
var childCount = targetObject.transform.childCount;
var completedChildren = 0;
var allChildrenExpanded = false;
// 遍历目标对象的所有子对象
@@ -278,121 +303,35 @@ namespace SceneView
childNode.transform.SetParent(childRectTransform, false);
childNode.parent = this;
childNode.CreateUI();
StartCoroutine(childNode.DisplayGameObjectStructureCoroutine(childTransform.gameObject, depth + 1, () =>
{
completedChildren++;
if (completedChildren == childCount)
StartCoroutine(childNode.DisplayGameObjectStructureCoroutineImpl(childTransform.gameObject, depth + 1,
() =>
{
allChildrenExpanded = true;
}
}));
completedChildren++;
if (completedChildren == childCount) allChildrenExpanded = true;
}));
yield return null;
}
// 等待所有子协程完成
while (!allChildrenExpanded && childCount > 0)
{
yield return null;
}
while (!allChildrenExpanded && childCount > 0) yield return null;
// 调用完成回调
onComplete?.Invoke();
// 在所有子节点初始化完毕后调用Expand
Expand();
}
public IEnumerator DisplayGameObjectStructureCoroutine(GameObject targetObject, int depth,
System.Action onComplete)
public IEnumerator DisplayGameObjectStructureCoroutine(GameObject targetObject, int depth = 0)
{
if (targetObject == null)
{
Debug.LogError("Target object is null.");
yield break;
}
ClearChildNodes();
originalText = GetIndentedName(targetObject.name, depth);
if (text != null) text.text = originalText;
objectEnable.onClick.AddListener(() => OnObjectEnable(targetObject, objectEnable, objectText));
UpdateObjButton(targetObject,objectEnable,objectText);
var components = targetObject.GetComponents<Behaviour>();
foreach (var component in components)
{
if (childRectTransform == null)
{
Debug.LogError("childRectTransform is null");
continue;
}
var config = ButtonConfig.Default;
config.RectConfig.SizeDelta = new Vector2(0, 40);
config.BackgroundColor = Color.green;
config.Text = new string(' ', (depth + 1) * 2) + component.GetType().Name;
var buttonBack = ControlUtilities.CreateButton(childRectTransform, config, null);
if (buttonBack.button == null)
{
Debug.LogError("buttonBack.button is null");
continue;
}
var button = buttonBack.button;
var comp = component;
buttonBack.button.onClick.AddListener(() => OnButtonClick(button, comp));
yield return null;
}
// 计数器,用于跟踪子协程的数量
var childCount = targetObject.transform.childCount;
var completedChildren = 0;
var allChildrenExpanded = false;
// 遍历目标对象的所有子对象
foreach (Transform childTransform in targetObject.transform)
{
if (childTransform == null)
{
Debug.LogError("Child transform is null.");
continue;
}
var childNode = new GameObject($"{childTransform.gameObject.name}").AddComponent<TreeViewNode>();
childNode.transform.SetParent(childRectTransform, false);
childNode.parent = this;
childNode.CreateUI();
StartCoroutine(childNode.DisplayGameObjectStructureCoroutine(childTransform.gameObject, depth + 1, () =>
{
completedChildren++;
if (completedChildren == childCount)
{
allChildrenExpanded = true;
}
}));
yield return null;
}
// 等待所有子协程完成
while (!allChildrenExpanded && childCount > 0)
{
yield return null;
}
// 在所有子节点初始化完毕后调用Expand
Expand();
// 调用完成回调
onComplete?.Invoke();
yield return DisplayGameObjectStructureCoroutineImpl(targetObject, depth);
}
private string GetIndentedName(string name, int depth)
{
var indent = "";
for (int i = 0; i < depth; i++)
{
indent += $"{i}_";
}
for (var i = 0; i < depth; i++) indent += $"{i}_";
return $"{indent}_{name}";
}
@@ -400,9 +339,7 @@ namespace SceneView
{
if (childRectTransform != null)
foreach (Transform childRectTransform in this.childRectTransform)
{
Destroy(childRectTransform.gameObject);
}
}
private void OnButtonClick(Button button, Behaviour component)
@@ -447,4 +384,4 @@ namespace SceneView
}
}
}
}
}