using System.Reflection;
MonoBehaviour script=(MonoBehaviour) gameObject.AddComponent(Assembly.GetExecutingAssembly().GetType(item.ToString()));
本文介绍了一种使用C#反射机制动态为Unity游戏对象添加MonoBehaviour组件的方法。通过Assembly.GetExecutingAssembly()获取当前执行程序集,并利用GetType()方法根据类型名称查找并实例化指定的MonoBehaviour脚本。
using System.Reflection;
MonoBehaviour script=(MonoBehaviour) gameObject.AddComponent(Assembly.GetExecutingAssembly().GetType(item.ToString()));

被折叠的 条评论
为什么被折叠?