private/public 当我在 unity 中使用 GridLayoutGroup 时,在检查器中看不到字段
private/public Field is not seen in inspector when I use GridLayoutGroup in unity
private/public 当我使用 GridLayoutGroup
时,在检查器中看不到字段
这是例子
这就是我定义变量的方式:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEditor;
public class Myscript : GridLayoutGroup
{
[SerializeField] private bool ishhh;
}
大多数 Unity built-in 组件都有相应的 [CustomEditor]
覆盖默认检查器。
具体来说GridLayoutGroup
has a custom inspector called GridLayoutGroupEditor
overwriting the inspector for any class derived from GridLayoutGroup
.
→您必须继承它才能为从 GridLayoutGroup
.
派生的 class 创建自定义编辑器
为了使其他字段可见,您可以执行类似
的操作
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
的文件夹中,以便在构建中删除它以避免 built-errors 关于 UnityEditor
命名空间。
或者(因为我可以看到您的示例代码中已经有一个 using UnityEditor
),您可以将它保留在同一个脚本中,但是您必须手动使用 #if Pre-Processors
才能剥离所有 code-blocks/-lines 关闭使用 UnityEditor
命名空间中的东西,例如
#if UNITY_EDITOR
using UnityEditor;
#endif
...
#if UNITY_EDITOR
// any code using UnityEditor
#endif
否则您将无法构建您的应用程序,因为 UnityEditor
仅存在于 Unity 编辑器本身中,并且在构建时完全被剥离。
注意:在智能手机上打字,因此不提供保修,但我希望这个想法能得到理解
private/public 当我使用 GridLayoutGroup
时,在检查器中看不到字段这是例子
这就是我定义变量的方式:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEditor;
public class Myscript : GridLayoutGroup
{
[SerializeField] private bool ishhh;
}
大多数 Unity built-in 组件都有相应的 [CustomEditor]
覆盖默认检查器。
具体来说GridLayoutGroup
has a custom inspector called GridLayoutGroupEditor
overwriting the inspector for any class derived from 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
的文件夹中,以便在构建中删除它以避免 built-errors 关于 UnityEditor
命名空间。
或者(因为我可以看到您的示例代码中已经有一个 using UnityEditor
),您可以将它保留在同一个脚本中,但是您必须手动使用 #if Pre-Processors
才能剥离所有 code-blocks/-lines 关闭使用 UnityEditor
命名空间中的东西,例如
#if UNITY_EDITOR
using UnityEditor;
#endif
...
#if UNITY_EDITOR
// any code using UnityEditor
#endif
否则您将无法构建您的应用程序,因为 UnityEditor
仅存在于 Unity 编辑器本身中,并且在构建时完全被剥离。
注意:在智能手机上打字,因此不提供保修,但我希望这个想法能得到理解