I’m working on a custom inspector that uses reflection to get a list of all my enemies and populates a popup dialogue with them. So far I have that much working and can select and spawn an enemy with the right name, but there’s a problem: as soon as I hit play or take focus away from the gameobject with the custom inspector, the value from the dropdown resets.
Here’s the inspector code:
[CustomEditor(typeof(EnemyFactory))]
public class EnemyFactoryEditor : Editor
{
int _choiceIndex = 0;
string[] inspectorChoices = new string[0];
List <System.Type> listOfDerivedClasses = Assembly.GetExecutingAssembly ().GetTypes ().Where (x => x.IsSubclassOf (typeof(Enemy))).ToList ();
public override void OnInspectorGUI ()
{
EnemyFactory targetFactory = (EnemyFactory)target;
inspectorChoices = new string[listOfDerivedClasses.Count];
for (int i = 0; i < listOfDerivedClasses.Count; i++) {
inspectorChoices [i] = listOfDerivedClasses [i].Name.ToString ();
}
DrawDefaultInspector();
EditorGUILayout.LabelField ("Enemy type:");
_choiceIndex = EditorGUILayout.Popup(_choiceIndex, inspectorChoices);
targetFactory.selectedEnemy = _choiceIndex;
EditorUtility.SetDirty(target);
}
}
And here’s the factory code so far (I still need to get a handle on how to spawn enemies of the right type)
public class EnemyFactory : MonoBehaviour {
public float healthPoints = 100;
public int healthNotches = 0;
public float speed = 0;
public float acceleration = 0;
[HideInInspector]
public int selectedEnemy;
void Awake () {
var type = typeof(Enemy);
var listOfDerivedClasses = Assembly.GetExecutingAssembly ().GetTypes ().Where (x => x.IsSubclassOf (type)).ToList ();
for (int i = 0; i < listOfDerivedClasses.Count; i++) {
// Debug.Log (listOfDerivedClasses [i].Name.ToString ());
}
Debug.Log(selectedEnemy);
Enemy<Jackass> placedEnemy = new Enemy<Jackass> (listOfDerivedClasses[selectedEnemy].Name.ToString());
placedEnemy.ScriptComponent.Initialize(
speed_: speed,
healthNotches_: healthNotches,
healthpoints_: healthPoints,
acceleration_: acceleration,
rotation_: Quaternion.identity,
position_: transform.position);
}
}
It comes with a paired “End” call that allows you to check if there was changes to the data during that span, and only then do you call Dirty(). Take a look for examples on how to use the above API before you spend too much more time tinkering.
Also, there’s various tutorial series by this guy that are fantastically detailed:
Alright, so I’ve gone over and implemented the catlike coding inspectors a couple times, along with a few additional resources (thanks again for pointing me in the right direction!) but now I’m running into the same problem again and I can’t seem to solve it.
I’m working on expanding my custom inspector so that I can have enemies with more/less health than usual in certain situations and I’ve managed to accomplish this:
Basically, it allows me to specify whether or not I’m overriding the default values an enemy may have. It’s a little ugly, like most of my code, but it half works. Once again, I’m faced with it not saving the values (aside from the dropdown, which works flawlessly for some reason) when I take focus away from the object, press play, or do anything. What completely obvious thing have I missed here?