Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by atonparker · Apr 29, 2016 at 06:29 AM · editor-scriptingoninspectorgui

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RibsNGibs · Jul 15, 2016 at 08:01 AM 0
Share

Did you ever figure out the answer to this?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Using a CustomEditor in the inspector and also seeing normal inspector fields 1 Answer

( Schrödinger's boolean?) custom Editor class OnSceneGUI() and OnInspectorGUI() accessing variable problem!! 2 Answers

Callback when custom PropertyAttribute changes 0 Answers

Editor values not updating in OnSceneGUI 1 Answer

Editor GUI TEXT On Inspector: How to add header text note in the inspector using a Custom Editor Inspector script ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges