- Home /
Custom Inspector for a [System.Serializable]
I am trying to mimic RPGMaker XP database using Unity Objects and custom inspectors. But I've found a wall I can't get past.
This is my Classes inspector:
Currently, to select which skill is gained at certain level, I need to insert the skill index. What I wanted was to make a drop down selector with all skills on it, and select one from it. Or at least, be able to visualize the name of the skill in the selected index.
I've found quite a bit of examples of how to make a drop down selector, but couldn't find out how to replace only that single variable with one.
Example of skills:
All of the objects shown here are [System.Serializable] For instance, this is my Classes class:
public class Classes : MonoBehaviour {
public HeroClass[] classes;
}
All the data(Name, STRGain..., Skills[]) is in the HeroClass, which extends nothing but have that [System.Serializable] (tag?) on it.
Answer by Guedez · Jul 27, 2015 at 07:59 PM
Managed to make it work with an PropertyDrawer.
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomPropertyDrawer(typeof(SkillGainLevel))]
public class SkillGainLevelInspector : PropertyDrawer {
string[] _choices = new[] { "foo", "foobar" };
int _choiceIndex = 0;
public override void OnGUI(Rect rect, SerializedProperty prop, GUIContent label) {
SerializedProperty sp = prop.FindPropertyRelative("level");
SerializedProperty sp2 = prop.FindPropertyRelative("skill");
EditorGUI.indentLevel++;
string[] options = new string[]
{
"Option1", "Option2", "Option3",
};
int[] values = new int[]
{
0, 1, 2
};
sp2.intValue = EditorGUI.IntPopup(new Rect(rect.x, rect.y, rect.width, 16), "Skill: ", sp2.intValue, options, values);
sp.intValue = EditorGUI.IntField(new Rect(rect.x, rect.y + 16, rect.width, 16), "Level: ", sp.intValue);
EditorGUI.indentLevel--;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
return 36;
}
}
Answer by DerpMerf · Jul 28, 2015 at 04:32 AM
What you're really looking for here is a way to make an enum that is sort of like an array. If you set the Skill variable in the Classes object to an enum with all the skills in it, then you'd be getting somewhere. I imagine it'd go like this:
Set the Tag of the Skills object to a new Tag called "Skills".
Then, I'd need you to provide the HeroClass and Skills class so that I can edit them and ACTUALLY help you.
Your answer
Follow this Question
Related Questions
Serializable class with interactivity 0 Answers
Logic Problems using for loops 0 Answers
[Solved]Why doesn't my ScriptableObject based asset save using a custom inspector ? 1 Answer
'Unsupported type' error in custom editor script 4 Answers
Can Unity not replace customized values on re-imported packages? 0 Answers