我有以下课程:
[Serializable]
public class BaseDamage : MonoBehaviour
{
public int MaximumDamage;
public int MinimumDamage;
public short SpellScaling;
}
以及包含该类的类:
public class SpellDamage : MonoBehaviour
{
public BaseDamage SpellBaseDamage;
}
当我将SpellDamage
脚本附加到gameObject时,我希望能够从编辑器视图中设置BaseDamage
的值,但它只允许我附加脚本。
我该怎么做?
答案 0 :(得分:2)
你走在正确的轨道上。您可以将BaseDamage脚本附加到附加SpellDamage脚本的同一个游戏对象。
您已添加Serializable
属性。您需要做的是在编辑文件夹中为BaseDamage
课程编写自定义属性抽屉。
这样的事情:
[CustomPropertyDrawer(typeof(BaseDamage))]
public class BaseDamageDrawer : PropertyDrawer
{
static string[] propNames = new[]
{
"MaximumDamage",
"MinimumDamage",
"SpellScaling",
};
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Don't make child fields be indented
var indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
SerializedProperty[] props = new SerializedProperty[propNames.Length];
for (int i = 0; i < props.Length; i++)
{
props[i] = property.FindPropertyRelative(propNames[i]);
}
// Calculate rects
float x = position.x;
float y = position.y;
float w = position.width / props.Length;
float h = position.height;
for (int i = 0; i < props.Length; i++)
{
var rect = new Rect(x, y, w, h);
EditorGUI.PropertyField(rect, props[i], GUIContent.none);
x += position.width / props.Length;
}
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}