- Home /
How do you instantiate a reference in a custom editor?
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.
Answer by Bunny83 · Jul 15, 2016 at 09:22 AM
This won't work as Unity doesn't support inheritance for cusom serializable classes. Unity serializes those classes based on the variable type which is in this case just an interface (IEmitterShape). It doesn't work that way. You either need to derive your classes from ScriptableObject or MonoBehaviour. Those two baseclasses support inheritance since they are serialized on their own and the shape variable would only hold a reference. Though you can't use a variable of IEmitterShape as this can not be serialized at all.
Common workaround are to implement a MonoBehaviour or ScripbableObject baseclass and then create derived classes from that.
Another common way is to use an interface like in the original code above, but derive your class from MonoBehaviour. You can't serialize the reference to an interface, but you can get the actual instance in Start / Awake by using:
shape = GetComponent<IEmitterShape>();
As said each emitter implementation would need to be a seperate monobehaviour class (with matching filename)
public class CircleEmitterShape : MonoBehaviour, IEmitterShape
{ ... }
Now you simply need to attach the desired emitter shape to the gameobject
Your answer
![](https://koobas.hobune.stream/wayback/20220612075825im_/https://answers.unity.com/themes/thub/images/avi.jpg)