1、游戏运行的时候通过类名挂载脚本
Type type = Type.GetType(scriptName);
var common = gameObject.AddComponent(type);
Type type = Type.GetType("HeadCom");
var aa = gameObject.AddComponent(type);
aa = (UIComponentBase)aa;
2、编辑器下可以通过类名挂载脚本
string scriptName = "xxxCom";
Debug.Log(scriptName);
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
var defaultAssembly = assemblies.First(assembly => assembly.GetName().Name == "Assembly-CSharp");
var type = defaultAssembly.GetType(scriptName);
if (type == null)
{
Debug.Log("添加失败" + scriptName);
return;
}
var scriptComponent = obj.GetComponent(type);
if (!scriptComponent)
{
scriptComponent = obj.AddComponent(type);
}
Debug.Log("完成添加" + scriptName);
|