Unity获取[SerializedField]私有变量以显示在我的自定义编辑器

时间:2016-02-14 10:46:14

标签: c# unity3d

我很难弄清楚如何通过我创建的编辑器在检查器中显示我的[SerializeField]私有布尔变量。我通常会这样做:

myTarget.AutoFill = EditorGUILayout.Toggle(...)

如果变量是公开的,但由于我将其作为“私人”,我是否会失去在Options_Manager_Editor中使用它的机会?有没有办法绕过这个,因为我想保持封装?我是否必须更改为使用OnGUI,只需将其公开,因为需要另一个脚本,或者在Options_Manager中创建一个公共方法,其中包含我的布尔值AutoFill的代码,用于自定义我的检查员?

我的Options_Manager代码:

public class Options_Manager : MonoBehaviour {
    // Auto fill.
    [SerializeField] 
    private bool AutoFill;
}

我的Options_Manager_Editor代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(Options_Manager))]
public class Options_Manager_Editor : Editor {

    public override void OnInspectorGUI(){
        // Grab the script.
        Options_Manager myTarget = target as Options_Manager;
        // Set the indentLevel to 0 as default (no indent).
        EditorGUI.indentLevel = 0;
        // Update
        serializedObject.Update();

        EditorGUILayout.Toggle(new GUIContent("Test Fill", "Test Fill Tooltip."), serializedObject.FindProperty("AutoFill").boolValue);
        // Apply
        serializedObject.ApplyModifiedProperties();
    }
}
编辑:我刚刚尝试了一些东西,但我不确定这是否会导致任何问题,但如果有人能看到这一点,让我知道这将是伟大的。

我的Options_Manager代码:

public class Options_Manager : MonoBehaviour {
    // Auto fill.
    [SerializeField] 
    private bool _autoFill;

public bool AutoFill{
    get{
        return _autoFill;
    }
    set{
        _autoFill = value;
    }
}
}

我的Options_Manager_Editor代码:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEditor;

[CanEditMultipleObjects]
[CustomEditor(typeof(Options_Manager))]
public class Options_Manager_Editor : Editor {

    public override void OnInspectorGUI(){
        // Grab the script.
        Options_Manager myTarget = target as Options_Manager;
        // Set the indentLevel to 0 as default (no indent).
        EditorGUI.indentLevel = 0;
        // Update
        serializedObject.Update();

        myTarget.AutoFill = EditorGUILayout.Toggle(new GUIContent("Auto Fill", "Test Tooltip"), serializedObject.FindProperty("_autoFill").boolValue);
        // Apply
        serializedObject.ApplyModifiedProperties();
    }
}

1 个答案:

答案 0 :(得分:0)

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

    }

}

供将来参考。请注意,您可以拥有私有变量,只需使用[ SerializeField ],(和[ System.Serializable ]用于不扩展MonoBehaviour的类)。编辑器类可能不需要using UnityEngine.UI