【发布时间】:2014-09-13 14:26:16
【问题描述】:
我在检查器中显示 Serializable 泛型类时遇到问题。
Serializable 泛型类如下:
using UnityEngine;
using System;
using System.Collections;
[Serializable]
public class GenericClass<T> where T : struct
{
[SerializeField]
string _serializedString;
public string SerializedString
{
get { return _serializedString; }
set { _serializedString = value; }
}
... // Functions using the generic type
}
自定义属性抽屉如下:
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(GenericClass<>))]
public class GenericClassDrawer: PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty stringProperty = property.FindPropertyRelative("SerializedString");
stringProperty.stringValue = EditorGUI.TextField(position, stringProperty.stringValue);
}
}
而我使用的MonoBehaviour测试的泛型类如下:
using UnityEngine;
using System.Collections;
public class TestClass : MonoBehaviour
{
enum BasicEnum
{
First,
Second,
Third,
Fourth
}
[SerializeField]
GenericClass<BasicEnum> _editorEnum;
}
使用当前代码,MonoBehaviour TestClass 不会在检查器中显示任何内容。它应该显示一个文本字段。
我相信这是由于该类的通用性质,但我一直无法找到任何人使用具有通用类的自定义属性抽屉的示例。
我的问题是 - 这可能吗?我从允许文本字段按预期显示的代码中遗漏了什么?如果目前的代码不可能,那么对于泛型类的属性抽屉是否有任何解决方法?
感谢您的宝贵时间!
【问题讨论】:
标签: c# generics properties unity3d drawer