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 /
  • Help Room /
avatar image
0
Question by bmargogne · Mar 09, 2017 at 10:56 AM · custom editorcustom-inspector

Custom Editor on Serialized Objects which are not derived from Monobehaviour

Hi... I cannot manage to have the desired behaviour on my custom editor when using objects which are not extending anything... Below is an oversimplified version of what I would like to achieve.

What I have :

  • a serialized class MyClass holding an enum and a string

  • a script object MyClassHolder, extending MonoBehaviour, holding various instances of MyClass

  • a custom Editor MyClassEditor which should create a dynamic behaviour on "MyClass" properties

Behaviour wanted :

  • I inspect MyClassHolder using the inspector

  • I see the list of instances of MyClass

  • I select one instance

  • I select one of the value from the enum dropdown list

  • I would like the string value to update using some calculation.

However the string value does not update... Below is the actual code I try to fix... Can anyone help me ??

 // file Assets/Scripts/MyClass.cs
 using UnityEngine;
 public class MyClassHolder : MonoBehaviour
 {
     public MyClass example1;
     public MyClass example2;
 }
 
 [System.Serializable]
 public class MyClass
 {
     public Dropdown dropdown;
     public string textFromDropdown;
     public enum Dropdown {ResponseOne, ResponseTwo }
 }

.

 // file Assets/Scripts/Editor/MyClassEditor.cs
 using UnityEditor;
 
 [CustomEditor(typeof(MyClass)), CanEditMultipleObjects]
 public class MyClassEditor : Editor
 {
     public SerializedProperty dropdown_prop;
     public SerializedProperty textFromDropdown_prop;
 
     void OnEnable()
     {
         dropdown_prop = serializedObject.FindProperty("dropdown");
         textFromDropdown_prop = serializedObject.FindProperty("textFromDropdown");
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
         EditorGUILayout.PropertyField(dropdown_prop);
         MyClass.Dropdown dropdownIndex = (MyClass.Dropdown)dropdown_prop.enumValueIndex;
 
         switch (dropdownIndex)
         {
             case MyClass.Dropdown.ResponseOne:
                 textFromDropdown_prop.stringValue = "I am Response One";
                 break;
             case MyClass.Dropdown.ResponseTwo:
                 textFromDropdown_prop.stringValue = "I am Response Two";
                 break;
         }
         serializedObject.ApplyModifiedProperties();
     }
 }







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
2

Answer by Adam-Mechtley · Mar 09, 2017 at 11:42 AM

For a custom serializable type, you want to use PropertyDrawer (and CustomPropertyDrawer), not Editor (and CustomEditor).

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 bmargogne · Mar 09, 2017 at 01:21 PM 0
Share

That works, thank you very much !

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

96 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 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 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 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 to access a Custom Editor components 1 Answer

Unity3D ScriptableObject with UnityEvent not saving event parameter 0 Answers

Custom editor for a global list of variables, similar to Tags and Layers. 0 Answers

Custom Editor: Array of Structs with elements hidden by toggles 0 Answers

Detect Mouse Click Over BoxCollider2D in Scene View (Custom Editor) 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