Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by DavidDebnar · Jun 22, 2013 at 10:46 AM · editorinspectorcustom editor

How could I merge 2 components' inspectors through a custom editor?

I've been thinking about doing this for a while. Is there a way to make a custom editor, that 'picks up' components? Lets say I'd have a main script called 'Manager' that'd have an a custom inspector with 3 buttons. But if I added a script called 'Extend', instead of showing the inspector for 'Extend', it'd add a button to the 'Manager' inspector. Also, I'd like the 'Manager' component to be added automatically if only the 'Extend' script is added - through RequireComponent, probably.

--David

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jamora · Jun 22, 2013 at 02:12 PM

This seems to work:

 #pragma strict
 
 @CustomEditor(tst)
 class Manager extends Editor{
 
     static var extensionFunctions = new ArrayList();
     
     function OnInspectorGUI(){
         EditorGUILayout.LabelField("Basic Manager");
         
         for(var func:function() in extensionFunctions){
             func();
         }
     }
 }
 #pragma strict
 
 @CustomEditor (tstExtended)
 class extEditor extends Editor{
 
     function Awake(){
         Manager.extensionFunctions.Add(func);
     }
     
     var func = function(){ 
         EditorGUILayout.LabelField("Extended Manager");
     };
 }

Now, it doesn't completely hide the extending monobehaviour from the gameobject component list, but there are some tips on achieving that at http://answers.unity3d.com/questions/34616/is-there-a-way-to-hide-a-monobehaviour-in-the-insp.html. There is also a warning about casting Object to function, but that can be ignored if you make sure you add only functions to the ArrayList in Manager. Alternatively, make a list of functions instead of Objets in Manager.

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 numberkruncher · Jun 22, 2013 at 02:46 PM 0
Share

I have updated my answer for you :)

avatar image
0

Answer by numberkruncher · Jun 22, 2013 at 02:34 PM

In your answer you are resorting to use of static state which could lead to problems further down the line. For example, if you had multiple objects with the same component, or when you switch between scenes.

Why not just place all of your editor functionality into the one editor?

Warning: Not tested, but something like this should work :)

 #pragma strict

 @CustomEditor(tst)
 class Manager extends Editor {

     var extendedTarget:tstExtended;
     var extendedSerializedObject:SerializedObject;

     function OnEnable() {
         tstExtended = target.GetComponent.<tstExtended>();
         if (tstExtended != null)
             extendedSerializedObject = new SerializedObject(tstExtended);
     }

     function OnInspectorGUI() {
         serializedObject.Update();

         // Use serialized properties as normal :)

         serializedObject.ApplyModifiedProperties();

         // Draw extended inspector controls when extended
         // component is present.
         if (extendedSerializedObject != null)
             OnExtendedInspectorGUI();
     }

     function OnExtendedInspectorGUI() {
         extendedSerializedObject.Update();

         // Use serialized properties as normal :)

         extendedSerializedObject.ApplyModifiedProperties();
     }

 }

 @CustomEditor(tstExtended)
 class extEditor extends Editor {

     function OnEnable() {
         // This might work, might not...
         // Based upon suggestion in the other FAQ you linked to.
         target.hideFlags = HideFlags.HideInInspector;
     }

     function OnInspectorGUI() { /* Intentionally empty! */ }

 }
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 DavidDebnar · Jun 22, 2013 at 08:10 PM 0
Share

Thanks for both of your answers, I was out all day, and only saw this now. I'll make sure to test it out myself tomorrow, see what works best and then post about it ;)

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

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers

Custom editor super slow even with no draw operations 1 Answer

Saving editor-only variables 0 Answers

Overwrite the inspector window on Scene Asset heading chosen in the hierarchy window 0 Answers

Edit chosen material in the inspector for custom editor. 2 Answers


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