• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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 add UnityEvent using Custom Inspector? 1 Answer

How to properly serialize fields and store them for use in play mode? 1 Answer

How to get a drop-down list of options defined by another object in editor? 0 Answers

How to prevent Editor script variables resetting at runtime 2 Answers

Editor: How to do PropertyField for List elements? 4 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges