I want to access each key as gameobject.
I loop first over the dictionary but what next? should I add another inner loop over the keys?
or maybe to loop like this:
for(int i = 0; i < dictio.Keys.Count; i++)
and then how to get each key?
instead looping over the selected objects I want to loop over the dictionary and get each key and value and display each key and value and on the left near each key a number for example:
1 mygameobject(key) myscript(value)
here is a screenshot of the keys on the left and the values this is what I want to display in the editor window and to add on the left a number for each object:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
public class RenameSelected : EditorWindow
{
private static readonly Vector2Int size = new Vector2Int(500, 500);
private string childrenPrefix;
private int startIndex;
private bool showPosition = false;
private bool includeChildren = false;
private GameObject[] objects;
private Vector2 scrollPos;
static Dictionary<GameObject, string> dictio = new Dictionary<GameObject, string>();
[MenuItem("GameObject/Rename Selected")]
public static void Init()
{
EditorWindow window = GetWindow<RenameSelected>();
window.minSize = size;
window.maxSize = size;
MonoBehaviour[] scripts = UnityEngine.Object.FindObjectsOfType<MonoBehaviour>();
foreach (MonoBehaviour script in scripts)
{
Type scriptType = script.GetType();
var scope = scriptType.Namespace;
if (scope == null || !scope.StartsWith("Unity"))
{
if (!dictio.Keys.Contains(script.gameObject))
{
dictio.Add(script.gameObject, scriptType.Name);
}
}
}
}
private void OnSelectionChange()
{
objects = Selection.gameObjects;
}
private void OnEnable()
{
objects = Selection.gameObjects;
}
public void OnGUI()
{
GUILayout.Space(10);
childrenPrefix = EditorGUILayout.TextField("Rename prefix", childrenPrefix);
startIndex = EditorGUILayout.IntField("Start index", startIndex);
includeChildren = EditorGUILayout.Toggle("Include Children", includeChildren);
// When checkbox true include children then to loop
// and add children if there is any of the selected object and make if needed
// folded nested tree of the children of the object.
// to add also checkbox to each selected gameobject so you can select individual
// if to include children for only specific objects and which not !!!!!
if (objects.Length == 0)
{
showPosition = false;
}
GUILayout.Space(20);
EditorGUI.BeginChangeCheck();
EditorGUILayout.GetControlRect(true, 16f, EditorStyles.foldout);
Rect foldRect = GUILayoutUtility.GetLastRect();
if (Event.current.type == EventType.MouseUp && foldRect.Contains(Event.current.mousePosition))
{
showPosition = !showPosition;
GUI.changed = true;
Event.current.Use();
}
showPosition = EditorGUI.Foldout(foldRect, showPosition, "Objects");
GUILayout.Space(2);
if (showPosition)
{
EditorGUI.indentLevel++;
scrollPos =
EditorGUILayout.BeginScrollView(scrollPos);
for(int i = 0; i < dictio.Keys.Count; i++)
//for (int i = 0; i < objects.Length; i++) // To loop and add children if there are
// so each selected gameobject will have nested foldout for children like a
// tree. to think how to do it first.
// the global bool variable includeChildren is for the renaming.
// if true to rename also children if false only parents(the selected objects
// without the children).
{
EditorGUIUtility.labelWidth = 50;
EditorGUILayout.BeginHorizontal();
{
GUILayoutOption[] options = { GUILayout.MaxWidth(300.0f), GUILayout.MinWidth(300.0f) };
//objects[i] = (GameObject)EditorGUILayout.ObjectField(i.ToString(), objects[i], typeof(GameObject), true, options);
dictio.Keys
EditorGUIUtility.labelWidth = 112;
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
EditorGUI.indentLevel--;
}
GUILayout.FlexibleSpace();
if (GUILayout.Button("Rename Objects"))
{
}
Repaint();
}
}
result: