- Home /
How acess private variable inside Custom editor, and interact with it on inspector.
Hi, guys anyone know's how to do like this:
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);
}
}
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.
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!
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"));
}
}
Your answer
Follow this Question
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