簡體   English   中英

當我統一使用GridLayoutGroup時,在檢查器中看不到private / public字段

[英]private/public Field is not seen in inspector when I use GridLayoutGroup in unity

當我使用GridLayoutGroup時,在檢查器中看不到private / public字段

這是例子

在此處輸入圖片說明

這就是我定義變量的方式:

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

public class Myscript : GridLayoutGroup 
{ 
    [SerializeField] private bool ishhh; 
}

大多數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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM