- Home /
How can I create a list of preset class/objects to be used in the editor
Sorry I wasn't really sure how to word the question and not exactly sure how to ask this but basically I want to know how exactly can I get this goal done;
After creating a class , Lets say "Equipment", I create an array of that Equipment class and preset all the variables in the inspector drop down. Now lets say I have 10 items in this Equipment array... How can I make that a permanent list for the game where I can now call "Equipment item" from a script and when I try to initialize the object in the inspector it will give me a drop down of the set list of equipment where I can now choose one of those to be that item?
I'm sorry in advanced if this is a bit confusing. If you have other questions that will clear things up a bit please let me know. I would really like to know how I can get this done.
Thanks!
Answer by frarees · Mar 29, 2014 at 12:28 PM
Ok so if I understood correctly, you want to define an array with some Equipment
instances, and then a drop-down where you choose just one of them. Right?
If so, you'd have:
public class MyScript : MonoBehaviour {
public Equipment[] equipments;
public Equipment currentEquipment;
}
And editor related code (quick custom editor for MyScript, not optimized, so it won't be as fast as it should):
[SerializeField]
int index = 0;
[SerializeField]
MyScript myScript;
void OnEnable () {
if (myScript == null)
myScript = target as MyScript;
}
public override void OnInspectorGUI () {
string[] equipmentStrings = new string[myScript.equipments.Length];
for (int i = 0; i < equipmentStrings.Length; i++) {
equipmentStrings[i] = myScript.equipments[i].equipmentName; // choose here the string you want to show in the drop-down
}
serializedObject.Update ();
EditorGUILayout.PropertyField (serializedObject.FindProperty ("equipments"));
EditorGUI.BeginChangeCheck ();
index = EditorGUILayout.Popup (index, equipmentStrings);
if (EditorGUI.EndChangeCheck ()) {
serializedObject.FindProperty ("currentEquipment").objectReferenceValue = myScript.equipments[index];
}
serializedObject.ApplyModifiedProperties ();
}
Note that the example above doesn't support multi-edit out of the box (as we're accessing target
, things will probably mess up if you enable multi-edit). It would require some tweaking.
Check the code yourself, as I've typed it straight in the browser and may contain typos.
Not sure if this is it but even when trying this all I'm getting in the Inspector is "$$anonymous$$ulti-object editing is not supported"
And also, maybe this would be a better explanation of what I want to achieve... Do you know how in the inspector if you have an enum variable, there will be a drop down to choose from the set of enum? Well picture that, a drop down of 'enum' (in this case custom class), that will display a list of 'name' s and then when one is chosen then that part of the inspector will populate with the associated attributes that was predefined with that element from the predefined array
Also, I don't want to actually create a custom editor for that script alone. I want that any script that will call an instance of my custom class to have this set feeling to it. That allows to do the above action.
Dam... Ok, I'm sorry. So I tested it again and I think it sort of worked but not fully... I'm getting an error saying Cannot Implicity convert type Equipment to Unity.Object for the line that says "serializedObject.FindProperty ("currentEquipment").objectReferenceValue = myScript.equipments[index];"
This is a very contrived example which demonstrates the changing of enum value to draw different views in the inspector. There are better ways to actually do the drawing, but this way proves that different code can be executed via switching the enum value.
Test.cs
using UnityEngine;
using System.Collections;
public enum Type
{
One,
Two,
Three,
}
public class Test : $$anonymous$$onoBehaviour
{
public Type type;
}
TestInspector.cs
using UnityEditor;
using UnityEngine;
using System.Collections;
[CustomEditor(typeof(Test))]
public class TestInspector : Editor
{
private Test instance;
void OnEnable()
{
instance = (Test)target;
}
public override void OnInspectorGUI ()
{
instance.type = (Type)EditorGUILayout.EnumPopup("Type", instance.type);
switch(instance.type)
{
case Type.One:
DrawTypeOne();
break;
case Type.Two:
DrawTypeTwo();
break;
case Type.Three:
DrawTypeThree();
break;
}
}
void DrawTypeOne()
{
GUILayout.Label("Type one selected!");
}
void DrawTypeTwo()
{
GUILayout.Label("Type two selected!");
}
void DrawTypeThree()
{
GUILayout.Label("Type three selected!");
}
}
Your answer
Follow this Question
Related Questions
How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers
Get if inspector field labels are on separate line 1 Answer
Unity editor and inspector header 0 Answers
Can Editor Windows function like the inspector ? 2 Answers
How To Draw a PropertyDrawer within a PropertyDrawer 0 Answers