using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
public class GetChildsEditorWindow : EditorWindow
{
public Transform transform;
int levelsMin = 0, levelsMax = 1;
static int currentLevel;
List allChildren = new List();
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();
IterateOverChild(transform, currentLevel, levelsMax);
oldLevel = 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);
}
}
}
What i want to do is that the loop will start automatic with a button maybe and will loop deep through all the levels and will give me the the whole number of levels there is and when i move the slider display the level number of each objects what object belong to what level.
I can't figure how to make the automation mapping.
↧