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 syscrusher · Apr 21, 2016 at 04:51 AM · monobehaviourcustom editorscriptable objectnested

How to do a custom editor for a ScriptableObject that is a property of a MonoBehaviour

I have what I thought would be a simple situation, but I can't seem to get a custom editor to work.

I have a fairly complex ScriptableObject in my real project, but for testing I created a very simple stand-in:

 public class TestData : ScriptableObject {
     public string s1;
     public float f1 = 3.14159f;
 }
 

An instance of this class is a public property of my component class. Again, I have made a simplified version for testing:

 public class TestComponent : MonoBehaviour {
     [SerializeField]
     public TestData tData = TestData.CreateInstance<TestData>();
 }

I have a custom editor for the inner class:

 [CustomEditor(typeof(TestData))]
 [CanEditMultipleObjects]
 public class TestDataEditor : Editor {
                 
     SerializedProperty s1;
     SerializedProperty f1;
         
     public override void OnInspectorGUI() {
         Debug.Log("TestDataEditor.OnInspGUI");
         base.OnInspectorGUI();
         EditorGUILayout.LabelField("TestDataEditor begins");
         s1 = serializedObject.FindProperty("s1");
         f1 = serializedObject.FindProperty("f1");
         EditorGUILayout.PropertyField(s1);
         EditorGUILayout.PropertyField(f1);
         EditorGUILayout.LabelField("TestDataEdito ends");
     }
 }

Finally, there is a custom editor for the outer class:

 [CustomEditor(typeof(TestComponent))]
 [CanEditMultipleObjects]
 public class TestEditor : Editor {
             
     Editor tDataEditor;
     SerializedProperty tData;
     
     public override void OnInspectorGUI() {
         tData = serializedObject.FindProperty("tDdata");
         EditorGUILayout.LabelField("This is TestEditor");
         if (tData != null) {
             if (tDataEditor == null) {
                 tDataEditor = Editor.CreateEditor((Object) tData.objectReferenceValue);
             }
             tDataEditor.OnInspectorGUI();
         }
     }
 }

When I create a GameObject with the TestComponent attached, I see the LabelField and its message from the outer editor, but the FindProperty("tData") is always returning null. This means the rest of the OnInspectorGUI() logic is skipped.

is there a "right way" to do this, other than what I am trying to do? I also am not clear on what parameter I should be passing to Editor.CreateEditor(), so I would appreciate any advice on that.

I have been all over the forums and search engines, and so far every solution I've seen posted for something akin to this has not worked with current Unity versions (I'm running 5.3.4). I've spent many hours trying to solve this, and I'm finally admitting I'm stumped.

For what it's worth, if I turn the real version of my ScriptableObject into a MonoBehaviour, I have a working custom editor for that more complex situation that is doing just fine. What I am attempting to do is to take my MonoBehaviour and refactor it as a ScriptableObject to allow it to be used away from a GameObject context.

Thanks in advance to anyone who can help.

Comment
Add comment · Show 9
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 · Apr 21, 2016 at 03:02 AM 0
Share

Additional data: If I make the ScriptableObject into just a base object, and change its instantiation from CreateInstance() to new TestData(), then the default nested inspector works, but my inner custom editor is still ignored.

avatar image syscrusher · Apr 22, 2016 at 08:57 PM 0
Share

UPDATE: I think I found the answer to this in a StackOverflow post, using a backhanded method with a custom PropertyDrawer.

I am testing that method in my code, and if it works I'll post my answer here for the benefit of others, because this one has been really hard to solve.

avatar image Polymo · Apr 22, 2016 at 09:38 PM 0
Share

You should be able to inline OnInspectorGUI() of http://docs.unity3d.com/ScriptReference/Editor.CreateEditor.html or http://docs.unity3d.com/ScriptReference/Editor.CreateCachedEditor.html

avatar image syscrusher Polymo · Apr 23, 2016 at 04:56 AM 0
Share

Thanks, @ValooFX. $$anonymous$$y custom property drawer works pretty well, but your method may be cleaner. I'll do some experimenting with that and see what it can do.

avatar image syscrusher · Apr 26, 2016 at 04:29 AM 0
Share

@ValooFX -- Your method works correctly, as did the one in the other article. As it turns out, I unfortunately had to remove the multi-valued ScriptableObject field from my project because it breaks when used in a Prefab. This was because of the Unity limitation on scene objects not being in a Prefab. What I ended up doing ins$$anonymous$$d was to just make my class a $$anonymous$$onoBehaviour and do a regular component. It took away one cool feature but simplified a whole lot of other things.

Notwithstanding all that, I appreciate your reply because I had not previously realized I could do what you suggest. I did get your method working, and I archived the code so I can reuse that suggestion on a future editing script where prefabs are not involved. Thanks!

avatar image wladyslaw · Jan 15, 2018 at 12:32 PM 0
Share

@syscrusher, Apparently the comment of @ValooFX you thanked for disappeared. I'm stuck with the same problem as you had and I would be very happy if I could see the solution he proposed. Could you please tell what was that solution?

avatar image Bunny83 wladyslaw · Jan 15, 2018 at 01:02 PM 0
Share

If you follow the comments carefully you may notice that ValooFX seems to be "Polymo". $$anonymous$$aybe the name got changed.

avatar image wladyslaw Bunny83 · Jan 15, 2018 at 02:20 PM 0
Share

$$anonymous$$aybe you're right. But in that case I think I don't understand his suggestion. What does he mean by inlining OnInspectorGUI() of CreateEditor?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mythirdalias · Jan 15, 2018 at 03:08 PM

I've been working on a similar thing without the MonoBehaviour extension but using ScriptableObjects and asked/solved this question:
https://answers.unity.com/questions/1454591/plug-and-play-unityeventsmethods.html?childToView=1454648#answer-1454648

You might find something useful in there. It looks to me like you need to be calling

 AssetDatabase.CreateAsset(ScriptableObject yourAsset, relPath);
 AssetDatabase.SaveAssets();`

At some point to actually save what you want to disk.

Calling tDataEditor.OnInspectorGUI(), in another OnInspectorGUI() feels wrong as well, I'd find the path of the object and navigate to it instead.

Personally I found the EditorWindow extension far more useful in this regard because it doesn't require you to use the inspector view.

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

6 People are following this question.

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

Related Questions

How to show complex types properties in a Custom Editor 0 Answers

Button to change list highlighted in editor 0 Answers

How to handle Serializable classes in a CustomEditor? 0 Answers

Custom Editor ScriptableObject List shows "Type Mismatch" after CreateInstance 2 Answers

Why child ScriptableObjects class instance cannot be placed in their parent ScriptableObject field in the inspector? 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