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 /
  • Help Room /
avatar image
2
Question by Gamingdrake · Feb 03, 2015 at 12:38 AM · editorcomponentscriptableobjectcustom-inspectoreditorguilayout

Custom Editor for embedded ScriptableObjects list

If I have a custom editor for a ScriptableObject, and the ScriptableObject has a list of interfaces inside it, what would be the best way to draw the default inspector for those objects?

--More Description Below--

I have a custom editor for a ScriptableObject asset in my project hierarchy. The asset script contains a list of ScriptableObjects that also inherit from IModuleComponent. What I want to do is display the default inspector for the embedded scriptableobjects/IModuleComponents in my custom inspector, to essentially treat the scriptableobject asset like a gameobject and then get all the components in the list to look like monobehaviors do on a gameobject.

I can get something like what I want to work by looping through the embedded components and calling an inherited function, but that requires me to write a custom IModuleComponent.DrawInInspector function for every new component.

Is there a way to get Unity to draw an inspector for each module without writing custom draw code for every module? Or if not, how would I get the custom function to look more like what Unity has?

Here is some of my code. This first snippet is my custom editor.

 if (showPosition[i])
 {
      GUILayout.Space(10);
      //EditorGUILayout.ObjectField(myTarget.componentModules[i] as ScriptableObject, typeof(ScriptableObject), false);
      (myTarget.componentModules[i] as IComponentModule).DrawInInspector();
 }

And in my component code I have this

 public void DrawInInspector()
 {
  #if UNITY_EDITOR
     suckItUnity = EditorGUILayout.Toggle("Suck it Unity", suckItUnity);
  #endif
 }

Halp

halp.png (59.1 kB)
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
3

Answer by syscrusher · Sep 04, 2016 at 06:27 PM

@Gamingdrake, I know it's been a long time since your post, but I think I have your answer. I stumbled upon your question while trying to solve the same problem here. :)

Disclaimer: The following pattern is working for me in a prototype, but I have not fully tested this code for production. Also, I've "filed off the serial numbers" in this example to make it more generic, so there may be some syntax errors. I'm working in Unity 5.4.0.

The class MyOuterObject is a traditional GameObject with a custom editor as shown below:

     [CustomEditor(typeof(MyOuterObject), true)]
     public class MyOuterObjectEditor : Editor {
         
         SerializedProperty myInnerObjectSO;
         
         private static GUIContent innerObjectPrompt = new GUIContent("Inner Object", "Provide an Inner Object asset to define the Outer Object's appearance");
         
         public override void OnInspectorGUI() {
             serializedObject.Update();
             MyOuterObject targetOuter = (MyOuterObject) target;
             myInnerObjectSO = serializedObject.FindProperty("innerObject");
             EditorGUILayout.PropertyField(myInnerObjectSO, innerObjectPrompt);
                     MyInnerObject inner = targetInner.innerObject;
             Editor innerEditor = MyInnerClassEditor.CreateEditor(inner);
             innerEditor.DrawDefaultInspector();
             serializedObject.ApplyModifiedProperties();
         }    
     }

The custom editor for the inner object (a ScriptableObject saved as an asset file) looks like this:

 public class MyInnerClassEditor : Editor {
 
                GUIContent myColorPrompt = new GUIContent("Color", "Pick a color for this asset");
 
         public override void OnInspectorGUI() {
             serializedObject.Update();
                         SerializedProperty myColor = serializedObject.FindProperty("myColor");
             EditorGUILayout.PropertyField(myColor, myColorPrompt);
             // Draw your other fields here            
             serializedObject.ApplyModifiedProperties();
         }
 }

The myColor property of the inner class is just a Color value.

With this code, you can inspect any of the .asset files of the MyInnerObject type. You can drag and drop (or popup select) a file into the innerObject field in the outer object's Inspector. Changes made in the asset file's Inspector are reflected in all instances of MyOuterObject that use the particular MyInnerObject asset, and vice versa.

Again, I note that I've created this code about half an hour ago, so it is emphatically not tested to production-ready standards, but the underlying technique seems to work in my prototype sandbox.

I hope this is helpful to the original poster and to others. Since my own code is still under development, suggested improvements would be most welcome.

Comment
Add comment · Show 3 · 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 syscrusher · Sep 05, 2016 at 05:41 AM 0
Share

Update: The concept is sound, but I've made some improvements to the details. In particular, it was helpful to put the inner object's editor GUI into a "foldout" when it's embedded by the outer object. The good news, though, is that I've spent most of the day today testing this further, and so far the basic pattern has worked well.

avatar image MetzT · Aug 08, 2017 at 01:46 PM 0
Share

In "$$anonymous$$yInnerObject inner = targetInner.innerObject;" where does targetInner come from? what variable is it referring to?

avatar image kodo91 · Jul 13, 2018 at 10:07 AM 0
Share

This helped me a lot, but I replaced the function call innerEditor.DrawDefaultInspector() with innerEditor.OnIspectorGUI() in order to get the custom inspector to be drawn ins$$anonymous$$d of the default one (like what can be seen on https://stackoverflow.com/a/16060171/2593324) - I hope this helps others that run into this.

avatar image
0

Answer by Spy-Shifty · Nov 05, 2016 at 10:05 PM

What does EditorMarkerTypeEditor do?

Can you post that source pls?

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 syscrusher · Nov 07, 2016 at 11:38 PM 0
Share

Oh, sorry!

When I was genericizing the names of classes in my example code, the intent was to decouple them from my real application. I missed a change or two!

I have edited the code in my answer to fix it. Sorry about that!

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

25 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

EditorGUILayout.ObjectField on a ScriptableObject's inspector doesn't update properly 0 Answers

Unity3D ScriptableObject with UnityEvent not saving event parameter 0 Answers

OnPreviewGUI for Canvas and UI Elements 0 Answers

Unable to edit fields from custom inspector 1 Answer

Code executing in play mode when it shouldn't 0 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