Unity中泛型类中的公开字段无法给Inspector
Unity中泛型类中的公开字段无法给Inspector
Unity中泛型类中的公开字段无法给Inspector
00 前言
在Unity中泛型的类, 比如
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
using System.Collections.Generic;
using UnityEngine;
using XLua;
[Serializable]
public class LuaLoader<T>
{
public TextAsset luaScript; // 从Resources加载的Lua脚本
[HideInInspector]
public Dictionary<int, T> DataDictionary;
private LuaEnv _luaEnv;
public Dictionary<int, T> GetData()
{
// TextAsset luaScript = Resources.Load<TextAsset>("Cfg_Effect.lua");
// Debug.Log(luaScript.text);
_luaEnv = _luaEnv ?? new LuaEnv();
// 执行Lua脚本
_luaEnv.DoString(luaScript.text);
// 获取Lua表中的数据
// var data = _luaEnv.Global.Get<Dictionary<int,Dictionary<string,object>>>("data");
var data2 = _luaEnv.Global.Get<Dictionary<int, T>>("data");
// Debug.Log(data2[1].Path);
// 将数组赋值给到CfgEffects以便外部调用.
DataDictionary = data2;
return data2;
}
public void Dispose()
{
_luaEnv.Dispose();
}
}
此时, 如果用
1
public LuaLoader<GameObject> luaLoader = new LuaLoader<GameObject>()
声明的话, 在Unity的Inspector面板上是不会显示这个类的public TextAsset luaScript;字段的.
01 处理方法
此时使用子类的方式来处理即可.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System;
using UnityEngine;
[Serializable]
public class LuaEffectLoader : LuaLoader<CfgEffect>
{
public void Check()
{
DataDictionary[1].DebugLog();
}
}
public struct CfgEffect
{
public int EffectId;
public string Path;
public string Path2;
public int DestoryTime;
public int PreLoadEff;
public int FlowBones;
public int Direction;
public int FollowScale;
public int Mirror;
public int Scaling;
public void DebugLog()
{
Debug.Log($"EffectId: {EffectId}\nPath: {Path}");
}
}
此时, 用以下代码声明的字段就可以在Inspector面板上显示.
1
public LuaEffectLoader luaEffectLoader = new LuaEffectLoader();
参考网页
本文由作者按照 CC BY 4.0 进行授权
