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 Rafael_Gratival · May 10, 2019 at 08:33 PM · unityeditoreditor-scripting

How acess private variable inside Custom editor, and interact with it on inspector.

Hi, guys anyone know's how to do like this:

alt text

but with private variables? Basically what i want is to set private variables that i could get from hierarchy.

Here the codes that i'm using.

 [System.Serializable]
 public class ButtonScript : Button
 {
     public GameObject m_Obj;
 }

And

 [ CustomEditor ( typeof(ButtonScript) ), CanEditMultipleObjects ]
 public class Editor_Button : ButtonEditor
 {
     public override void OnInspectorGUI ( )
     {
         base.OnInspectorGUI (  );
 
         var content = (ButtonScript)target;
 
         GUILayout.Label("Custom Extend", EditorStyles.boldLabel);
 
         content.m_Obj = (GameObject)EditorGUILayout.ObjectField("Game Object",content.m_Obj, typeof(GameObject), true);
     }
 }

screenshot-1.png (19.2 kB)
Comment
Add comment · Show 2
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 Bunny83 · Jun 23, 2019 at 05:11 PM 0
Share

Could you please split your question and answer into an actual question and an actual answer? Do not add an answer to your question. Also your question in its current form is very confusing. You talk about a private variable but the script you show has only a public variable.


Also this line in your "solution" makes no sense:

 [SerializeField] private protected Gameobject m_Obj2;

A field can not have two access modifiers (private and protected).

You probably want to call base.OnInspectorGUI() inside your overridden method in order to get the default behaviour as well.


Apart from those things, congratulations, your solution is spot on.

avatar image Rafael_Gratival Bunny83 · Jun 23, 2019 at 05:21 PM 0
Share

Done. And well about the question , the script was showing a public variable because, i couldn't figure out how to achieve that label being private, was just a reference.

And thanks for the feedback!

1 Reply

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

Answer by Rafael_Gratival · Jun 23, 2019 at 05:18 PM


Solution

Found the Solution for anyone look for it.


Class:

     public class ButtonScript : Button
     {
         [SerializeField] private GameObject m_Obj;
         [SerializeField] private protected Gameobject m_Obj2; // this is just to example.
     }

Editor:

     [ CustomEditor ( typeof(ButtonScript) ), CanEditMultipleObjects ]
     public class Editor_Button : ButtonEditor
     {
          private   SerializedProperty myGameObject;
          private   SerializedProperty myGameObject2;
 
     protected virtual void OnEnable ( )
     {
         myGameObject         = serializedObject.FindProperty ( "m_Obj" );
         myGameObject2      = serializedObject.FindProperty ( "m_Obj2" );
     }
 
         public override void OnInspectorGUI ( )
         {

            base.OnInspectorGUI();

             serializedObject.Update (  );
 
             DrawComponents (  );
 
             serializedObject.ApplyModifiedProperties (   );
         }
     protected virtual void DrawComponents()
     {
         EditorGUILayout.ObjectField(myGameObject, new GUIContent("My Game Object", "My GameObject Description"));
         EditorGUILayout.ObjectField(myGameObject2, new GUIContent("My Game Object 2", "My GameObject 2 Description"));
     }
 
     }
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

117 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

Related Questions

[ExecuteInEditMode] OnEnable Running twice on Play? 1 Answer

Event or hook for executing code before unity opens and compiles/loads the project. 1 Answer

in editor object placement by script in editor 1 Answer

How to use OnWIllDeleteAsset - Not being called 2 Answers

I give up. My Foldout creates a very small space when expanded. How do I get rid of it? 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