unity的AddComponent(string)提示已过时的解决方法

由于之前的AddComponent(string name)过时了,听说是性能方面的问题,所以被废弃了,但是我可以使用AddComponent(System.Type t)。下面上代码

Type t = Type.GetType(“CustomClassName”);
/*这个type如果是使用我们自己的类,那么直接这么用好了。
但是如果是系统的类的话,比如Button什么的,
那么直接用类名可能是找不到的,要带上命名空间,但是系统函数的命名空间分的很细,不好找,所以请用下面这个方式获取名字: string className = typeof(Button).AssemblyQualifiedName;
*/ gameObject.AddComponent(t);

 

posted on 2019-06-27 17:31  炼金师  阅读(1998)  评论(0编辑  收藏  举报

导航