简体   繁体   English

Unity:ScriptableObjects的CustomDrawers?

[英]Unity: CustomDrawers for ScriptableObjects?

I'm trying ot make a custom drawer for a simple Aspect:ScriptableObject. 我正在尝试为一个简单的Aspect:ScriptableObject创建一个自定义抽屉。 I can get everything to work for the Aspect class as long as it's a generic class, but as soon as it's a ScriptableObject everything breaks. 只要它是一个泛型类,我就可以为Aspect类提供一切工作,但只要它是一个ScriptableObject,一切都会中断。 Specifically: 特别:

EditorGUI.PropertyField(position, property.FindPropertyRelative("AspectName"),
                        new GUIContent("Aspect:"));

but the findPropRel is null. 但是findPropRel为null。 using property.serializedObject returns the parent object the Aspect is on, not the Aspect. using property.serializedObject返回Aspect所在的父对象,而不是Aspect。 I can't even hack together a solution that uses that since I don't know which aspect it is. 我甚至不能破解使用它的解决方案,因为我不知道它是哪个方面。

Is there no way to create a customDrawer for scriptableObjects or Monobehaviours? 有没有办法为scriptableObjects或Monobehaviours创建customDrawer? I can't imagine Unity would make CustomDrawers that can't even be used with the most common Unity class. 我无法想象Unity会使CustomDrawers甚至无法与最常见的Unity类一起使用。

And no, a customEditor really doesn't work in this situation. 不,customEditor在这种情况下确实不起作用。 I'd have to make dozens for completely unrelated classes just to draw the aspect. 我必须为完全不相关的类制作几十个才能画出方面。

I am also having the same problem. 我也有同样的问题。 I've reached a kind of trick to get properties from a ScriptableObject , but not using FindPropertyRelative , wich does not work for ScriptableObject but works well for plain objects. 我已经达成了一种从ScriptableObject获取属性的技巧,但没有使用FindPropertyRelative ,它不适用于ScriptableObject,但适用于普通对象。

Also using MonoBehaviour will not work if you plan to instantiate the class in another MonoBehavior class, as MonoBehavior classes can only be added as components to GameObject (s). 如果您计划在另一个MonoBehavior类中实例化该类,那么使用MonoBehaviour将无法工作,因为MonoBehavior类只能作为组件添加到GameObject (s)。 You cannot create it using new , or Unity will complain. 你不能用new创建它,否则Unity会抱怨。

I have to tell you, that yet I don't think my solution is really valid because I am not sure if it works well on prefabs, but that could be maybe because I am trying to save some scene references on the prefab and that seems that cannot be done in Unity. 我必须告诉你,但我不认为我的解决方案真的有效,因为我不确定它是否适用于预制件,但这可能是因为我试图在预制件上保存一些场景参考,这似乎在Unity中无法完成。

Well. 好。 Stop speech. 停止发言。 That is what I do: 这就是我做的:

Supposing your ScriptabeObject you want to access is of class MySOClass. 假设您要访问的ScriptabeObject属于MySOClass类。

public override void OnGUI(Rect pos, SerializedProperty prop, GUIContent label) {
    // Make a new serialized object for your property that is a class that inherits
    // from ScriptableObject
    SerializedObject serializedObject = new SerializedObject(prop.objectReferenceValue as MySOClass);
    SerializedProperty propSP = serializedObject.FindProperty("prop");

    if (propSP != null) {
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(propSP , true);
        if (EditorGUI.EndChangeCheck()) {
            serializedObject.ApplyModifiedProperties();
        }
    }
}

The fact that I test nulliness of propSP , is because if the actual value of prop is null, Serialized property returned will be also null. 我测试propSP的无效的事实是因为如果prop的实际值为null,则返回的Serialized属性也将为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM