using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class GetChildsEditorWindow : EditorWindow
{
public Transform transform;
int levelsMin = 3, levelsMax = 4;
static int currentLevel;
List<Transform> allChildren = new List<Transform>();
static int oldLevel = 0;
Vector2 scrollPos;
public int levelNum = 0;
public int totalLevels = 0;
// Add menu named "My Window" to the Window menu
[MenuItem("Get Childs/Get")]
static void Init()
{
oldLevel = currentLevel;
// Get existing open window or if none, make a new one:
GetChildsEditorWindow window = (GetChildsEditorWindow)EditorWindow.GetWindow(typeof(GetChildsEditorWindow), false, "Get Childs");
window.Show();
}
private void OnGUI()
{
GUILayout.Space(20);
transform = EditorGUILayout.ObjectField("Transform to get childs", transform, typeof(Transform), true) as Transform;
EditorGUI.BeginDisabledGroup(transform == null);
currentLevel = (int)EditorGUILayout.Slider("Slider", currentLevel, levelsMin, levelsMax);
EditorGUI.EndDisabledGroup();
if (allChildren != null && allChildren.Count > 0)
{
EditorGUILayout.BeginHorizontal();
scrollPos =
EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(400), GUILayout.Height(400));
for (int i = 0; i < allChildren.Count; i )
{
allChildren[i] = EditorGUILayout.ObjectField("Transform " i.ToString(), allChildren[i], typeof(Transform), true) as Transform;
}
EditorGUILayout.EndScrollView();
EditorGUILayout.EndHorizontal();
}
if (oldLevel != currentLevel)
{
if (transform != null)
{
allChildren = new List<Transform>();
IterateOverChild(transform, currentLevel, levelsMax);
oldLevel = currentLevel;
}
}
if(GUILayout.Button("GEt it"))
{
IterateOverChild(transform, 1);
Debug.Log(currentLevel);
}
}
public void IterateOverChild(Transform original, int currentLevel, int maxLevel)
{
if (currentLevel > maxLevel) return;
for (var i = 0; i < original.childCount; i )
{
Debug.Log($"{original.GetChild(i)}");
allChildren.Add(original.GetChild(i));
IterateOverChild(original.GetChild(i), currentLevel 1, maxLevel);
}
}
public void IterateOverChild(Transform original, int currentLevel)
{
for (var i = 0; i < original.childCount; i )
{
Debug.Log($"{original.GetChild(i)}");
allChildren.Add(original.GetChild(i));
IterateOverChild(original.GetChild(i), currentLevel 1);
}
}
}
I want to get automatic all the level depth for each object and display it in the editorwindow.
For example :
Object 1 is at level 0
Onject 2 is at level 3
Object 3 is at level 0
but i'm having some problems :
The code now the second method IterateOverChild display in the end 3 levels total but there are 4 levels. and how to display each object the level he is at ?
In the screenshot the arrows in red are level 1 the arrows in green are level 2 yellow level 3 and blue level 4 :
I see two GameObject's at level 1 GameObject and GameObject (1) then 2 GameObject's at level 2 and two objects at level 3 and one last at level 4. this is how i see the logic of the levels.
I want to update the ui editor window panel with the objects and each object on what level he is at near each object to show the level number. ( I gave the objects names Level 1 Level 2 Level 3 Level 4 just to name them with my logic of how i see the levels ).
CodePudding user response:
How To Get Level of Any Transform?
This method completely shows the level. Just put the desired transform in the parameter:
public static int ObjectLevel(Transform current, int level = 0)
{
if (current.parent) return ObjectLevel(current.parent, level);
return level;
}
Similar method in a line:
public static int ObjectLevel(Transform current, int level = 0) => current.parent ? ObjectLevel(current.parent, level) : level;
An Example...
Here that shows the level of all game objects:
public void Start()
{
foreach (var _transform in GameObject.FindObjectsOfType<Transform>())
{
var level = ObjectLevel(_transform);
_transform.name = ", level: " level;
}
}