- Home /
Can I change inspector 'Element 0' to something else?
Ok, I know the trick of having a public string as the first element of a class that can change the name of the Element.
I'm wondering if I can change the whole thing without having to do that. I'm guessing a custom inspector like here: http://answers.unity3d.com/questions/26207/how-can-i-recreate-the-array-inspector-element-for.html
But what I'd like to have is something like:
Enemy 1
Enemy 2
Enemy 3
etc. instead of Element, have that come up automatically.
Your guess is right. If you're not willing to make a public string the first field of your class, the only other solution I'm aware of is to write a custom inspector or property drawer.
Not sure how or where to make that change. Several examples on that linked page, but I'm kinda lost. There's one that explicitly puts 'Element' in but it looks messy and didn't get many votes.
The examples are short. Try 'em out. One explicitly says JavaScript (UnityScript). The others are C#, btw. For the JavaScript one, you can change "Element "+x to just x to get rid of the "Element" part.
Did you find the answer to this? $$anonymous$$ind of having the same issue.
Answer by Neogen13 · Oct 18, 2015 at 09:09 AM
Just had that propblem)
Custom function for arrays in Editor or PropertyDrawer instead of EditorGuiLayout.PropertyField();
public void ShowArrayProperty(SerializedProperty list)
{
EditorGUILayout.PropertyField(list);
EditorGUI.indentLevel += 1;
for (int i = 0; i < list.arraySize; i++)
{
EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i),
new GUIContent ("Bla" + (i+1).ToString()));
}
EditorGUI.indentLevel -= 1;
}
So you have:
Bla 1
Bla 2
Bla 3
Hope it would help someone ^__^
Could you please explain better where do I insert that script?
Throw this at the bottom of your script or make a new script: |`
[UnityEditor.CustomEditor(typeof(ClassContainingTheList))]
public class InspectorCustomizer : UnityEditor.Editor
{
public void ShowArrayProperty(UnityEditor.SerializedProperty list)
{
UnityEditor.EditorGUI.indentLevel += 1;
for (int i = 0; i < list.arraySize; i++)
{
UnityEditor.EditorGUILayout.PropertyField(list.GetArrayElementAtIndex(i), new UnityEngine.GUIContent("Bla" + (i + 1).ToString()));
}
UnityEditor.EditorGUI.indentLevel -= 1;
}
public override void OnInspectorGUI()
{
ShowArrayProperty(serializedObject.FindProperty("NameOfListToView"));
}
}`
I used your scripts but the default display is disabled, I added base.OnInspectorGUI() before ShowArrayProperty(serializedObject.FindProperty("NameOfListToView")); to show the default, but that makes list render twice, how do I edit the list ?
This is great! How can I use the value of a variable ins$$anonymous$$d of the constant "Bla"? If I have a list of struct and in this struct there is a string field, how can I call each element in the editor with:
variable value 1
variable value 2
variable value 3
Ins$$anonymous$$d of:
Bla 1
Bla 2
Bla 3
Answer by jnt · Feb 22, 2018 at 08:54 PM
#if UNITY_EDITOR
public string Name;
#endif
I'll go one-up on you @COAForce ;)
In my opinion this is the simplest and best way. Means you don't have any unwanted serialized data hanging around in your build.
This only works if it's the first element and a string. "Name" is no longer important.
Answer by Elecman · Mar 26, 2015 at 04:43 AM
In a PropertyDrawer you can do this by changing the GUIContent label like so: label.text = "test";
Answer by WalterEspinar · Jun 22, 2016 at 09:29 PM
Add this at the start: public string name;
The OP mentions the trick, it works for any public string field placed first, or for a private [SerializeField] string field.
Answer by RyanNguyen · Sep 17, 2017 at 07:56 PM
For those who are having this problem. This is my solution
public class PropertyWindow : EditorWindow
{
private void ArrayGUI(SerializedProperty property, string itemType, ref bool visible)
{
visible = EditorGUILayout.Foldout(visible, property.name);
if (visible)
{
EditorGUI.indentLevel++;
SerializedProperty arraySizeProp = property.FindPropertyRelative("Array.size");
EditorGUILayout.PropertyField(arraySizeProp);
for (int i = 0; i < arraySizeProp.intValue; i++)
{
EditorGUILayout.PropertyField(property.GetArrayElementAtIndex(i), new GUIContent(itemType + i.ToString()), true);
}
EditorGUI.indentLevel--;
}
}
}
How to use
private void OnGUI()
{
ArrayGUI(property, "Name", ref listVisibilityAttack);
}
Hope this helps!.
Your answer
Follow this Question
Related Questions
need to shorten my code but unsure of how 3 Answers
Get ParticleSystem (Shuriken) to play from array of game objects C# 2 Answers
How to find the index of an Object in an Array 2 Answers
Advance through array elements with math (++)? 1 Answer
Attempt at creating an array of text files results in different errors 1 Answer