常用方法:
删除:
添加:
AddComponentMenu
给物体添加一个脚本
例如:
![](https://img.haomeiwen.com/i2900438/fe30dd9b663c8472.png)
RequireComponent
自动帮你添加你需要的组件,而且删除组件的时候会有提示
例如:
![](https://img.haomeiwen.com/i2900438/9ced90c60d07d07f.png)
GetComponent
获取组件
AddComponent
添加组件
上面的编辑器模式下面也可以用,需要继承于MonoBehaviour
例如:
BuildinGrowthGroup buildinGrowthGroup = obj.GetComponent<BuildinGrowthGroup>() ?? obj.AddComponent<BuildinGrowthGroup>();
buildinGrowthGroup = obj.GetComponent<BuildinGrowthGroup>();
(判断一个物体是否有BuildinGrowthGroup这个脚本,如果没有则添加上,添加之后如果需要获取的话,还需要再次GetComponent)
Instantiate
添加一个物体
例如:
![](https://img.haomeiwen.com/i2900438/ac5aaca99e4c9643.png)
网友评论