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 SteveFSP · Jun 07, 2011 at 04:42 PM · editorextension

Patterns for Unity Editor Friendly Extensions

My core question is: What are some good design patterns to use when creating Unity Editor extensions that need to permit further extensions?

I've been working on a project that includes a pipeline-like build process and I want the end-user (coders) to be able to plug in components of their own making at different points, and keep it Unity Editor friendly.

An example: The beginning of the pipeline requires a mesh. I provide several components that gather and aggregate scene meshes, but the end-user may want to create their own and still be able to assign it to the fields in my custom editors.

Right now I'm using an inheritance approach. It works OK. But it bothers me a bit from a design perspective.

Continuing with the above example: I have an abstract DSGeometry class that extends MonoBehaviour. My custom editor requires a DSGeometry object, so anyone can create their own concrete class via scripting and assign it to my editor.

Any suggestions on other approaches?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Jun 07, 2011 at 05:48 PM

I think inheritance is one of the best/easiest way. You can use interfaces as well, but at some points you need some workarounds because most of the built-in functions in Unity works only with classes that are derived from UnityEngine.Object.

For example if you want to offer a drag&drop field where the user can plugin only a certain interface you can do that in a custom inspector:

 private IMyInterface m_SomeComponent = null;
 public override void OnInspectorGUI()
 {
     m_SomeComponent = (IMyInterface)EditorGUILayout.ObjectField((MonoBehaviour)m_SomeComponent,typeof(IMyInterface));
 }

Since the editor only allows objects of type IMyInterface to be assigned to the object field you can be sure it has to be a Monobehaviour script that implements your interface.

Another way would be to attach the MonoBehaviours as normal components and use GetComponent to get a component that supports a certain interface. Again the same problem, you can't use the generic version of GetComponent because of the constraint to "Component". The type-version works ;)

 IMyInterface component = (IMyInterface)GetComponent(typeof(IMyInterface));

I would say you can use any design pattern in Unity, but you may have to adjust it a bit. It's kinda useless to implement your own strategy-pattern since the whole component system of unity provides already everything.

Comment
Add comment · Show 1 · 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
avatar image SteveFSP · Jun 10, 2011 at 03:12 PM 0
Share

The first solution is nice, but it doesn't appear to work. The pattern compiles fine, and the object field will display the correct value if one is already assigned. ($$anonymous$$g. If it was assigned via code first.) But the GUI field in the inspector won't accept new assignments. It just blocks everything as invalid. Have you been able to get the first solution to work in Unity 3.x?

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

2 People are following this question.

avatar image avatar image

Related Questions

Automated testing 1 Answer

Unity5 "MenuItem" not work under build-in menu 0 Answers

Wanted editor extension called only when changed 1 Answer

[Editor Extension] - Menu Helper 1 Answer

Having runtime functionality and editor extension in one 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