簡體   English   中英

無法在Unity中將DecoratorDrawer與ScriptableObject一起使用

[英]Can't use DecoratorDrawer with ScriptableObject in Unity

我想我可能在這里遺漏了一些非常明顯的東西,但是這里是我遇到的問題的精簡版。

我有這個游戲對象腳本:

public class PingPong : MonoBehaviour {
    [Test]
    public int value1 = 10;
    [Test]
    public int value2 = 10;
}

這個TestAttribute.cs類

public class TestAttribute : PropertyAttribute {}

還有這個DecoratorDrawer擴展類:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : DecoratorDrawer {
    public override void OnGUI(Rect position) {
        base.OnGUI(position);
        EditorGUILayout.LabelField("Hi", "hello");
    }
}

這很棒!

但是,如果我嘗試將完全相同的內容放入這樣的ScriptableObject實例中:

[CreateAssetMenu(fileName = "Example", menuName = "Example/Settings", order = 1)]
public class SpecialSettings : ScriptableObject {
    [Test]
    public int value1 = 10;
    [Test]
    public int value2 = 10;
}

它不會這樣做,並給我這個錯誤:

ArgumentException: Getting control 4's position in a group with only 4 controls when doing Repaint
Aborting
UnityEngine.GUILayoutGroup.GetNext () (at C:/buildslave/unity/build/Modules/IMGUI/LayoutGroup.cs:117)
UnityEngine.GUILayoutUtility.DoGetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Modules/IMGUI/GUILayoutUtility.cs:444)
UnityEngine.GUILayoutUtility.GetRect (UnityEngine.GUIContent content, UnityEngine.GUIStyle style, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Modules/IMGUI/GUILayoutUtility.cs:404)
UnityEditor.InspectorWindow.CheckDragAndDrop (UnityEditor.Editor[] editors) (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:622)
UnityEditor.InspectorWindow.OnGUI () (at C:/buildslave/unity/build/Editor/Mono/Inspector/InspectorWindow.cs:443)
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
Rethrow as TargetInvocationException: Exception has been thrown by the target of an invocation.
System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)
System.Reflection.MethodBase.Invoke (System.Object obj, System.Object[] parameters) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)
UnityEditor.HostView.Invoke (System.String methodName, System.Object obj) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:291)
UnityEditor.HostView.Invoke (System.String methodName) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:284)
UnityEditor.HostView.InvokeOnGUI (Rect onGUIPosition) (at C:/buildslave/unity/build/Editor/Mono/HostView.cs:251)

誰能告訴我怎么了?

我不知道為什么,但是我可以向您展示解決方法:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        return base.GetPropertyHeight(property, label) * 2;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        position.height /= 2;
        EditorGUI.PropertyField(position, property, label);
        position.y += EditorGUIUtility.singleLineHeight;
        EditorGUI.LabelField(position, "hello", "hi");
    }
}

已編輯

希望這段代碼對您有所幫助。

public class TestAttribute : PropertyAttribute
{
    public string content;
    public int maxlineCount;

    private TestAttribute() { }
    public TestAttribute(string content, int maxlineCount)
    {
        this.content = content;
        this.maxlineCount = maxlineCount;
    }
}

和抽屜:

[CustomPropertyDrawer(typeof(TestAttribute))]
public class TestDecorator : PropertyDrawer
{
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        var lineCount = ((TestAttribute) attribute).maxlineCount + 1;
        return (base.GetPropertyHeight(property, label) + EditorGUIUtility.standardVerticalSpacing) * lineCount;
    }

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var ta = (TestAttribute) attribute;

        position.height = EditorGUIUtility.singleLineHeight;
        EditorGUI.PropertyField(position, property, label);

        position.height *= ta.maxlineCount;
        position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
        position = EditorGUI.PrefixLabel(position, new GUIContent("About"));

        EditorGUI.TextArea(position, ta.content, EditorStyles.wordWrappedLabel);
    }
}

現在:

public class PingPong : MonoBehaviour
{
    [Test("some single line text here.", 1)]
    public int value1 = 10;
    [Test("some other text for two lines and with no meaning.", 2)]
    public int value2 = 10;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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