您的位置:首页 > 移动开发 > Unity3D

在unity通过组件名称(string)添加脚本

2016-01-07 12:58 447 查看

unity 5.0之后不支持AddComponent(string)

以下为核心代码:

private static Dictionary<string, Type> types = new Dictionary<string, Type>();

public static EnemyPlane AddPlaneAI(GameObject go, string operate)

{

EnemyPlane enemy = null;

Type ttt =null;

if (types.ContainsKey(operate)) {

ttt = types[operate];

}

else

{

ttt = Type.GetType(operate);

}

Component component = go.AddComponent(ttt);

if (component != null)

enemy = component as EnemyPlane;

return enemy;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: