I’m working on a short script that emits game objects from a 2D shape. I would like to have a custom editor that draws a preview of the area emitter in the scene, sort of like how the particle emitter does it. Here’s some pared down code:
public class ObjectEmitter
{
public IEmitterShape shape;
public void Emit(GameObject go)
{
go.transform.position = shape.PickRandomPosition(transform);
}
}
public interface IEmitterShape
{
public Vector3 PickRandomPosition(Transform origin);
}
[Serializable]
public class SquareEmitterShape : IEmitterShape
{ ... }
[Serializable]
public class CircleEmitterShape : IEmitterShape
{ ... }
Now in the editor I would like to just expose a dropdown, letting you select from a list of shapes, and then assigning a new instance of IEmitterShape
to the editor target. I wrote something that works in Edit Mode but I guess doesn’t serialize the changes, so I get null references in Play Mode.
public enum EmitterShapeName
{
Square,
Circle,
}
[CustomEditor(typeof(ObjectEmitter))]
public class ObjectEmitterEditor : Editor
{
public override void OnInspector
{
ObjectEmitter emitter = (ObjectEmitter) target;
// Convert the shape type to our enum.
EmitterShapeName name = GetEmitterNameOf(emitter.shape);
EmitterShapeName selection = (EmitterShapeName) EditorGUILayout.EnumPopup("Shape", name);
if (GUI.Changed)
{
// Construct a new instance of emitter shape. This step isn't getting serialized.
emitter.shape = MakeEmitterShapeOf(selection);
EditorUtility.SetDirty(target);
}
}
void OnSceneGUI()
{
// Draw some handles based on the editor shape.
}
}
I typed this out from memory (the project isn’t in front of me) so please forgive any typos. I’m thinking I need to use SerializedProperty to access and modify the emitter.shape
reference but I’m not sure how. Thanks for any help.