Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
3
Question by dan19 · Jul 19, 2013 at 08:14 AM · editor-scriptingcustom-inspectorcustom-editor

Showing member ScriptableObject's in the Inspector

Suppose I have a MonoBehavior which has a public member inherited from ScriptableObject. How can I display the editor for the member object in the same Inspector window that shows the MonoBehavior itself?

What I have

This is the situation in more detail. I have my own asset class inherited from ScriptableObject' like this: public class MyAsset : ScriptableObject { public int param1 = 1; public int param2 = 2; public int param3 = 3; } I have an instance of MyAsset` in the Assets folder, and I can edit it in the Inspector: alt text

So far so good. But now I want to use this asset in my MonoBehavior's. Here is a MonoBehavior descendant:

 public class MyBehavior : MonoBehaviour {
 
     public MyAsset memberAsset;
     
 }

Now this is what Inspector looks like when editing a GameObject that has MyBehavior attached to it: alt text

What I want

You see, I can set memberAsset to point to MyAsset instance of my choosing, but if I want to edit the asset itself, I have to do it separately. Is there a way to display an editor for MyAssetInstance below, in the same editor window?

Example that shows it should be possible

As you can see in the same Inspector window, my GameObject has a MeshRenderer component, and that component has in its members a Material. Now the editor for the material appears right below, so you can edit it in place, without losing focus of the GameObject. There must be a way to make MyBehavior-MyAsset pair behave the same as MeshRenderer-Material. My guess is that I want to make a custom Editor for MyBehavior, but I can't find any methods in EditorGUILayout that would allow me to spawn a new Editor for MyAsset and show it below in the same inspector window. Any help?

screenshot1.png (14.1 kB)
screenshot2.png (42.9 kB)
Comment
Add comment · Show 2
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 BigRoy · Feb 20, 2014 at 08:18 AM 0
Share

Have you been able to find an answer by now?

avatar image dan19 · Feb 21, 2014 at 07:04 AM 0
Share

@BigRoy It was a long time ago. As far as I remember, I wasn't able to reproduce the behavior of $$anonymous$$eshRenderer-$$anonymous$$aterial pair exactly.

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Johat · Aug 20, 2015 at 07:39 PM

Hey, don't know if you managed to figure this out, but I think CreateEditor or CreateCachedEditor are what you're after. CreateCachedEditor seemed to be the cleanest and simplest implementation.

So if you have a MonoBehaviour:

 using UnityEngine;
 
 public class MyBehaviour : MonoBehaviour
 {
     public MyAsset MemberAsset;
     
     // Note: assets don't need to be public for it to work
     [SerializeField] private MyAsset _memberAsset;
 }

Where MyAsset derives from ScriptableObject (for example -- but any Object should be okay).

You could create something like this (for example):

 using UnityEngine;
 using UnityEditor;
 
 [CustomEditor(typeof(MyBehaviour))]
 public class MyBehaviourEditor : Editor
 {
     private Editor _editor;
     
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         
         var myAsset = serializedObject.FindProperty("MemberAsset");
         CreateCachedEditor(myAsset.objectReferenceValue, null, ref _editor);
         _editor.OnInspectorGUI();
         
         serializedObject.ApplyModifiedProperties();
     }
 }

(Obviously, you could play with the other functions that can be called on editors here too.)

Note that, it will draw the inspector just as it would if you had it in the inspector. You may not want this (e.g. may not want the script field to display), so you can supply a custom editor that will do the job. In fact, you could write as many editors as you wanted and select which editor will be used as the second parameter (we passed null here, which will just give you the default editor for that Object).

Hope that's what you were after =).

Comment
Add comment · Show 2 · 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 Aberdyne · Sep 18, 2015 at 10:19 AM 1
Share

Here is an article on the subject: http://www.gamasutra.com/blogs/$$anonymous$$arkWahnish/20150904/252962/Unity_Trick_1__$$anonymous$$ake_an_inspector_for_any_ScriptableObject.php

avatar image Munchy2007 · Sep 23, 2016 at 08:51 PM 0
Share

I searched for hours trying to find the answer to this, it was exactly what I was after.

avatar image
2

Answer by TheVastBernie · Jul 22, 2017 at 04:27 PM

If anyone's still wondering and wants a general solution that works for every scriptable object, without needing to write custom inspectors, you can take a look at the post over here: https://forum.unity3d.com/threads/editor-tool-better-scriptableobject-inspector-editing.484393/

It takes a custom property drawer for ScriptableObjects to display every derived class within the main inspector of the MonoBehaviour.

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 yoHasse · Jul 25, 2017 at 09:06 AM 0
Share

This was simple and helped me. Thank you for referring to that thread!

avatar image
2

Answer by as3mbus · Nov 15, 2019 at 10:24 AM

https://gist.github.com/tomkail/ba4136e6aa990f4dc94e0d39ec6a058c

in case someone still looking for a way to do this

Comment
Add comment · Show 2 · 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 trantak · Apr 05, 2021 at 06:14 PM 0
Share

Thanks! This is helped a lot!

avatar image CheekySparrow78 · Apr 11, 2021 at 07:20 PM 0
Share

I love you. IT WORKS!

avatar image
0

Answer by hatuf · Jul 19, 2013 at 08:24 AM

One way for such a simple class would be to add getters and setters to MyBehavior that would access the memberAssets properties.

     public int param1
     {
         get
         {
             return memberAsset.param1;
         }
         
         set
         {
             memberAsset.param1 = value;
         }
     }
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 dan19 · Jul 19, 2013 at 08:42 AM 0
Share

This is an interestind idea, but I have a feeling that if $$anonymous$$yAsset grows more complex, this can quickly get out of hand. Also, it's not clear how to adjust the approach if ins$$anonymous$$d of a single $$anonymous$$yAsset $$anonymous$$yBehavior contained, say, a List of them with unknown length.

avatar image
-3

Answer by JohnnySunshine · Jul 19, 2013 at 09:55 AM

Mark MyAsset as serializable and it will show up in the inspector nicely.

 [System.Serializable]
 public class MyAsset : ScriptableObject
 {
 }
Comment
Add comment · Show 2 · 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 dan19 · Jul 19, 2013 at 10:45 AM 0
Share

No, it doesn't work that way. If $$anonymous$$yAsset didn't inherit from ScriptableObject, then it would work, but then $$anonymous$$yAsset wouldn't be a separate asset, it would be basically just a structure inside of $$anonymous$$yBehavior. And I want it to be a separate asset.

avatar image JohnnySunshine · Jul 19, 2013 at 11:52 AM 0
Share

Oh, you're right, sorry. That slipped my $$anonymous$$d somehow. (Do I remove this answer, or just wait for others to downvote 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

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

Related Questions

Custom Editors for classes that derive from system.object 2 Answers

Custom editor tags not saving 1 Answer

How to force close Popup Window? 1 Answer

Edit an object in isolation quickly, as in the new Prefab Mode 0 Answers

Custom editor: How to initialise a new array element? 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