如何根据层次结构中的场景顺序对循环进行排序以debug.log游戏对象? [英] How can I sort the loop to debug.log the gameobjects according to the scenes order in the hierarchy?
本文介绍了如何根据层次结构中的场景顺序对循环进行排序以debug.log游戏对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class CompareObjects : MonoBehaviour
{
private GameObject[] allObjects;
private void Start()
{
allObjects = FindObjectsOfType<GameObject>();
foreach (GameObject go in allObjects)
{
Debug.Log(go.name + " >>>>> " + go.scene.name + " >>>>> is active object");
}
}
}
现在,从我添加用于测试的第二个场景开始,然后有时在第一个场景的中间循环遍历对象,然后回到第二个场景.
Now it's starting with objects from the second scene I added for testing then loop over the objects sometimes in the middle of the first scene then back to the second.
相反,我希望它始终循环层次结构中的顶部场景对象,并将下一个场景循环到底部.
Instead I want that it always will loop the top scene objects in the hierarchy and the next scene to the bottom.
如果层次结构是这样的:
If the hierarchy is like this :
Scene 1
GameObject t1
Scene 2
GameObject t2
Scene 3
GameObject t1
然后是场景1的第一个循环t1,然后是场景3的t2和t1
Then first loop t1 of Scene 1 then t2 and t1 of scene 3
推荐答案
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
public static class EditorHelpers
{
[MenuItem("Tools/PrintHierarchy")]
public static void PrintHierarchy()
{
var scenes = Enumerable.Range(0, SceneManager.sceneCount)
.Select(SceneManager.GetSceneAt)
.Select(s => $"Scene {s.name}\n{GetSceneHierarchy(s.GetRootGameObjects())}")
.ToArray();
Debug.Log(string.Join("\n", scenes));
}
private static string GetSceneHierarchy(GameObject[] gameObjects)
{
var child = gameObjects.Select(g => GetChildHierarchyRecursively(g.transform)).ToArray();
return string.Join("\n", child);
}
private static string GetChildHierarchyRecursively(Transform parentTransform, string indent = " ")
{
var res = indent + parentTransform.name + "\n"
+ string.Join("\n",
Enumerable.Range(0, parentTransform.childCount)
.Select(i => GetChildHierarchyRecursively(parentTransform.GetChild(i), indent += " ")));
return res;
}
}
这篇关于如何根据层次结构中的场景顺序对循环进行排序以debug.log游戏对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文