我正在为Unity实现一个深度对象复印机。
我在这里找到了这个很棒的序列化/反序列化方法:https://stackoverflow.com/a/78612/3324388
但是我遇到了MonoBehaviour对象。如果类型是GameObject,我需要使用Instantiate
而不是序列化。所以我添加了一张支票:
if (typeof(T) == typeof(GameObject))
{
GameObject clone = Instantiate(source as GameObject);
T returnClone = clone as T;
return returnClone;
}
我可以将源代码转换为GameObject(使用as
)但是当我尝试反向执行时它会失败并且
类型参数
T
不能与as
参数一起使用,因为它没有类类型约束,也没有“类”约束。
如果我尝试将它像:
if (typeof(T) == typeof(GameObject))
{
GameObject clone = Instantiate(source as GameObject);
T returnClone = (T)clone;
return returnClone;
}
无法将GameObject转换为
T
类型
我觉得我很接近但是我不能完全正确地进行投射。你知道我错过了什么让这个工作吗?
在return语句中使用as T
似乎可以解决问题。在下面附加到场景中游戏对象的测试代码中,我看到了Test
的一个副本,控制台显示了Count
的不同值:
public class Test : MonoBehaviour
{
private static bool _cloned = false;
public static T Clone<T>(T source) where T : class
{
if (typeof(T) == typeof(GameObject))
{
GameObject clone = Instantiate(source as GameObject);
return clone as T;
}
else if (typeof(T) == typeof(PlainType))
{
PlainType p = new PlainType();
// clone code
return p as T;
}
return null;
}
public class PlainType
{
private static int _counter = 0;
public int Count = ++_counter;
public string Text = "Counter = " + _counter;
}
public PlainType MyPlainType = new PlainType();
void Update ()
{
if (!_cloned)
{
_cloned = true;
Clone(gameObject);
PlainType plainClone = Clone(MyPlainType);
Debug.Log("Org = " + MyPlainType.Count + " Clone = " + plainClone.Count);
}
}
}
它不漂亮,但你可以强制编译器执行以前的引用转换为object
:
public static T Clone<T>(T source)
{
if (source is GameObject)
{
return (T)(object)Instantiate((GameObject)(object)source);
}
else ...
}
是的,它有点乱,但有时你无法避免它。作为一般规则,当您开始将泛型与运行时类型检查混合时,事情往往会变得混乱,这肯定表明您可能不应该首先使用泛型。但有时候,它可能是合理的,但丑陋的代码往往会突然出现。