Unity 让静态变量显示在预制体上
要在Unity预制体上显示静态变量,您需要创建一个自定义的Inspector,并使用EditorGUI类来绘制GUI元素。以下是一个示例代码:
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(YourPrefab))]
public class YourPrefabEditor : Editor
{
public override void OnInspectorGUI()
{
YourPrefab targetPrefab = (YourPrefab)target;
EditorGUI.BeginChangeCheck();
//绘制静态变量
targetPrefab.yourStaticVariable = EditorGUILayout.IntField("Your Static Variable", targetPrefab.yourStaticVariable);
if (EditorGUI.EndChangeCheck())
{
//应用变更
EditorUtility.SetDirty(targetPrefab);
}
}
}
在上述示例中,我们使用了Unity的EditorGUI类来绘制一个整数输入字段,以显示预制体的静态变量。我们还使用EditorUtility.SetDirty()方法来标记预制体为“脏”,以便Unity在保存场景时保存变更。
要使用此自定义检查器,请将上述代码保存在名为“YourPrefabEditor.cs”的脚本文件中,并将其放置在“Editor”文件夹中。然后,将“[CustomEditor(typeof(YourPrefab))]”替换为您自己的预制体类名。最后,在Unity编辑器中选择您的预制体,并在检查器窗口中查看您的静态变量已经显示在预制体上了
原文地址: https://www.cveoy.top/t/topic/gCbG 著作权归作者所有。请勿转载和采集!