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 Clet_ · Apr 09, 2014 at 12:10 AM · editorcustomeditorserializefield

Access serialized field in custom editor

I am quite new to writing custom editors and I've been struggling with it ever since. I am not a fan of making every field public for the sake of being able to access it from the inspector, so I'm mainly using [SerializeField] on protected/private variables.

However, when I can't seem to find the way to access these serialized properties within a custom editor.

Here's an example to demonstrate my situation:

 [System.Serializable]
 public class Settings {
     public string myName = "My Name";
 }
 
 public class MyClass : MonoBehaviour {
     [SerializeField] protected Settings mySettings;
 }

I want to be able to affect mySettings.myName through a custom editor, but I can't seem to find the way. I could use reflection to get the field, but it seems a little overkill for this...

Can anyone help me on this?

Comment
Add comment · Show 1
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 Clet_ · Apr 09, 2014 at 01:26 AM 0
Share

For now, I've resolved to using Reflection with a helper method

 using System.Reflection;
 
 public static T GetFieldByName<T>(string fieldName, BindingFlags bindingFlags, object obj)
 {
     FieldInfo info = obj.GetType().GetField(fieldName, bindingFlags);
     if(info == null) 
         return default(T);
     
     return (T)info.GetValue(obj);
 }

2 Replies

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

Answer by frarees · Apr 09, 2014 at 08:22 AM

You should use SerializedObject.

Example:

 void OnInspectorGUI () {
     EditorGUILayout.LabelField (serializedObject.FindProperty ("mySettings.myName").stringValue);
 }

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

Answer by BroVodo · Dec 12, 2016 at 09:02 AM

MyProjectNamespace.cs

 using UnityEngine;
 using UnityEditor;
 
 namespace MyProjectNamespace
 {
     [ CustomEditor( typeof( MyMonoBehaviour ) ) ]
 
     [ CanEditMultipleObjects ]
     
     public class MyMonoBehaviourInspector : Editor
     {
         #region Variables
         
         SerializedProperty _myBoolSP;
 
         MyMonoBehaviour _myMonoBehaviour;
         
         #endregion Variables
         
         #region Functions
         
         void OnEnable()
         {
             
             _myMonoBehaviour = serializedObject.targetObject as MyMonoBehaviour;
             
             _myBoolSP = serializedObject.FindProperty( "_myClass._myBool" );
             
             Debug.Log( "_myBoolSP = " + _myBoolSP.boolValue );
             
         }
         
         
         
         public override void OnInspectorGUI()
         {
             serializedObject.Update();
             
             _myBoolSP.boolValue = EditorGUILayout.Toggle( "A Boolean", _myBoolSP.boolValue );
 
             GUILayout.Label( _myMonoBehaviour.MyBoolValue() );
 
             serializedObject.ApplyModifiedProperties();
         }
         
         #endregion Functions
         
     }
     
 }

MyMonoBehaviour.cs

 using UnityEngine;
 
 namespace MyProjectNamespace
 {
     public class MyMonoBehaviour : MonoBehaviour
     {
         #region Variables
         
         [ SerializeField ] private MyClass _myClass;
         
         #endregion Variables
         
         #region Functions
         
         public string MyBoolValue()
         {
             return "The value of _myBool is " + _myClass.MyBool;
         }
         
         #endregion Functions
         
     }
     
 }


MyClass.cs

 using UnityEngine;
 
 namespace MyProjectNamespace
 {
     [ System.Serializable ]
     
     public class MyClass
     {
         #region Variables
         
         [ SerializeField ] private bool _myBool;
         
         #endregion Variables
         
         #region Functions
         
         public bool MyBool
         {
             get{ return _myBool; }
         }
         
         #endregion Functions
         
     }
     
 }

For future reference. Note that you can have private variables, you just need to use [ SerializeField ], (and [ System.Serializable ] for classes that don't extend MonoBehaviour). You probably won't need using UnityEngine.UI for editor classes.

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

23 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

Related Questions

How to make CustomEditor as in Terrain script ? 1 Answer

Property values changed by custom editor are reset when start playing 2 Answers

Sortlayer Renderer Extension 1 Answer

Type casting SerializedProperty.objectReferenceValue doesn't work? 3 Answers

BCE0019 errors in Custom Editor script 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