Unity:Inspector 找不到 ScriptableObject 的字段 [英] Unity: Inspector can't find field of ScriptableObject

查看:44
本文介绍了Unity:Inspector 找不到 ScriptableObject 的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法显示 ScriptableObject 的公共字段,该字段是我正在检查的组件的子项.虽然我可以用另一种方式轻松做到这一点,但我需要这种方法来处理其他变量.(ReorderableLists)

I'm having trouble showing a public field of a ScriptableObject which is a child of the component I'm inspecting. While I can easily do this in another way, I need this method to work for other variables. (ReorderableLists)

我简化了问题,也许我只是做了一些明显的错误,但我看不出我做错了什么.

I simplified the problem, maybe I was just doing something obvious wrong, but I can't see what I'm doing wrong.

代码+错误:http://answers.unity3d.com/storage/temp/70243-error.png

class SomeComponent : MonoBehaviour{
   public MyScriptable scriptable; //instantiated and saved as asset
}

[Serializable] class MyScriptable : ScriptableObject{
   [SerializeField] public float value = 0.1f;
}

[CustomEditor(typeof(SomeComponent))] class SomeComponentEditor : Editor{
     public override void OnInspectorGUI() {

         if((target as SomeComponent).scriptable==null) (target as SomeComponent).scriptable = ScriptableObject.CreateInstance(typeof(MyScriptable)) as MyScriptable;

         EditorGUILayout.PropertyField(serializedObject.FindProperty("scriptable"));                                 
         //shows the asset
         EditorGUILayout.PropertyField(serializedObject.FindProperty("scriptable").FindPropertyRelative("value"));   
         //error
    }
}

推荐答案

要修复您的代码,您可以这样做:

To fix your code you can do this:

using UnityEditor;
using UnityEngine;

class SomeComponent : MonoBehaviour
{
    public MyScriptable myScriptable;
}

class MyScriptable : ScriptableObject
{
    public float myChildValue = 0.1f;
}

[CustomEditor(typeof(SomeComponent))]
class SomeComponentEditor : Editor
{
    public override void OnInspectorGUI()
    {
        SomeComponent someComponent = target as SomeComponent;
        if (someComponent.myScriptable == null)
            someComponent.myScriptable = CreateInstance<MyScriptable>();

        SerializedProperty myScriptableProp = serializedObject.FindProperty("myScriptable");
        EditorGUILayout.PropertyField(myScriptableProp);

        SerializedObject child = new SerializedObject(myScriptableProp.objectReferenceValue);
        SerializedProperty myChildValueProp = child.FindProperty("myChildValue");
        EditorGUILayout.PropertyField(myChildValueProp);
        child.ApplyModifiedProperties();
    }
}

要检查子对象,首先需要创建它的 SerializedObject 版本,然后可以像往常一样搜索属性.

To inspect a child object, you first need to create a SerializedObject version of it, which then can be searched for properties as usual.

此外,从 ScriptableObject 派生的类不需要 Serializable 属性,并且只有在序列化私有字段时才需要 SerializeField 属性;公共字段在 Unity 中默认被序列化.

Also, the Serializable attribute is not needed on classes which derive from ScriptableObject and the SerializeField attribute is only needed when serializing private fields; public fields are serialized by default in Unity.

在不知道您的代码的原始上下文的情况下,您的方法对我来说似乎有点奇怪.ScriptableObject 实例旨在用作 Unity 项目中的资产文件.您是否使用过 CreateAssetMenu 属性?通常,您会通过菜单手动创建资产,然后将它们插入到您的组件中.你这样做的方式,它不会被写入磁盘,那么为什么要使用 ScriptableObject 而不仅仅是一个普通的类呢?但也许这一切在您的实际情况下都有意义,所以不要介意我是否错了.

Without knowing the original context of your code, your approach seems a little peculiar to me. ScriptableObject instances are meant to be used as asset files in your Unity project. Have you used the CreateAssetMenu attribute yet? Usually, you would create your assets manually via the menu and then plug them into your components. The way you are doing it, it won't be written to disk, so why use ScriptableObject and not just a normal class? But maybe it all makes sense in your actual context, so never mind if I'm wrong.

这篇关于Unity:Inspector 找不到 ScriptableObject 的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆