小编Sta*_*ell的帖子

通用类C#Unity的自定义属性抽屉

我在检查器中显示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
}
Run Code Online (Sandbox Code Playgroud)

自定义属性抽屉如下:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

MonoBehaviour我正在使用测试泛型类如下:

using UnityEngine;
using System.Collections; …
Run Code Online (Sandbox Code Playgroud)

c# generics properties unity-game-engine drawer

4
推荐指数
1
解决办法
5335
查看次数

标签 统计

c# ×1

drawer ×1

generics ×1

properties ×1

unity-game-engine ×1