[英]When should I use public, private, or [SerializeField]? Unity C#
[英]private/public Field is not seen in inspector when I use GridLayoutGroup in unity
大多數Unity內置組件都有一個相應的[CustomEditor]
覆蓋默認的Inspector。
具體而言, GridLayoutGroup
具有一個名為GridLayoutGroupEditor
的自定義檢查器, GridLayoutGroupEditor
覆蓋從GridLayoutGroup
派生的任何類的檢查器。
→必須繼承自它,才能為從GridLayoutGroup
派生的類創建自定義編輯器。
為了使其他字段可見,您可以執行例如
using UnityEditor;
...
[CustomEditor(typeof(Myscript))]
public class MyScriptEditor : GridLayoutGroupEditor
{
private SerializedProperty ishhh;
// Called when the Inspector is loaded
// Usually when the according GameObject gets selected in the hierarchy
private void OnEnable ()
{
// "Link" the SerializedProperty to a real serialized field
// (public fields are serialized automatically)
ishhh = serializedObject.FindProperty("ishhh");
}
// Kind of the Inspector's Update method
public override void OnInpectorGUI ()
{
// Draw the default inspector
// base.OnInspectorGUI();
// Fetch current values into the serialized properties
serializedObject.Update();
// Automatically uses the correct field drawer according to the property type
EditorGUILayout.PropertyField(ishhh);
// Write back changed values to the real component
serializedObject.ApplyModifiedProperties();
}
}
重要提示:將此腳本放置在名為Editor
的文件夾中,以便在構建中刪除它,以避免有關UnityEditor
命名空間的內部錯誤。
或者(因為我可以看到示例代碼中已經using UnityEditor
),可以將其保留在同一腳本中,但隨后您必須手動使用#if Pre-Processors
才能刪除所有代碼塊/行使用UnityEditor
命名空間中的內容,例如
#if UNITY_EDITOR
using UnityEditor;
#endif
...
#if UNITY_EDITOR
// any code using UnityEditor
#endif
否則,您將無法構建應用程序,因為UnityEditor
僅存在於Unity Editor本身,並且已完全剝離用於構建。
注意:在智能手機上鍵入內容,因此沒有保修,但我希望這個想法能弄清楚
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.