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
1
Question by nycucumber · Jan 01, 2018 at 08:48 PM · custom editorstruct

Show struct fields in custom editor

I am using a struct to save all the tweakable variables and I am writing a customer editor for the designer to tweak them.

Because the variable contained by the struct is always changing. So I am wondering if it is possible to expose a struct and its fields on a customer editor.

To clarify me, here are some sample code.

ScriptA:

 [Serializable]
 public struct Settings
 {
     public float boo;
     public float foo;
     public int baz;
 }

 public Settings settings {get;set;}
 //this one will be exposed but i don't want to use a public field
 public Settings settings2;


ScriptAEditor:

 public override void OnInspectorGUI(){
         //how to expose `ScriptA.settings` so the user can tweak its field.

        //i can also do a bunch of  EditorGUILayout.IntField and EditorGUILayout.FloatField but it is too manual.
  }
 



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 nycucumber · Jan 03, 2018 at 11:18 PM 0
Share

Problem solved. I just knew that you could actually serialize private field by attribute [SerializeField]

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Trey1232 · Sep 19, 2019 at 05:26 AM

You don't need to write a custom editor in order to expose private fields to the editor. You can use [SerializeField] in order to do this.

ScriptA:

 public class ScriptA : MonoBehaviour
 {
     [Serializable]
     public struct Settings
     {
         public float boo;
         public float foo;
         public int baz;
     }
     public Settings settings { get; set; }
     
     //Expose without making public
     [SerializeField]
     Settings settings2;
 }



If you are dead-set on writing your own custom editor, then you will need.

EditorGUILayout.PropertyField

In order to display your struct correctly, you will need to set includeChildren to true (otherwise, you will get a dropdown with no child components). Also, the settings variable cannot be displayed because it is a property (and properties cannot be serialized into the editor).

For the sake of completeness, I'll include full working code (non-automatic using statements included).


ScriptA:

 public class ScriptA : MonoBehaviour
 {
     [Serializable]
     public struct Settings
     {
         public float boo;
         public float foo;
         public int baz;
     }
     public Settings settings { get; set; }
     
     Settings settings2;
 }



ScriptAEditor:

 using UnityEditor;
 
 [CustomEditor(typeof(GameConfigReader))]
 public class ScriptAEditor : Editor
 {
     SerializedProperty settings2;
 
     ScriptA scriptA;
 
     private void OnEnable()
     {
         settings2 = serializedObject.FindProperty("settings2"); //name of your settings2 property in your other file
         scriptA = (ScriptA)target;
     }
 
     public override void OnInspectorGUI()
     {
         serializedObject.Update();
 
         //Not something you asked for, but this will get that faded bar with the script name
         GUI.enabled = false;
         EditorGUILayout.PropertyField(serializedObject.FindProperty("m_Script"), true);
         GUI.enabled = true;
 
         EditorGUILayout.PropertyField(settings2, true);
         serializedObject.ApplyModifiedProperties();
     }
 }

Note that is is exactly the same as using [SerializeField]. Using [SerializeField] has the added benefit of not needing to update an editor script if you decide to add more fields to ScriptA. @nycucumber

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

123 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 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 Editor: Array of Structs with elements hidden by toggles 0 Answers

List of Structs variable values not changing 1 Answer

Use placeholder text in CustomEditor 0 Answers

Custom inspector ReorderableList gives an error when adding item to list 0 Answers

How do you hide a slider using Custom editor? 0 Answers


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