Unity中添加组件的几种方法

一、在编辑器上面添加一个组件。

二、在脚本中利用AddComponent函数添加一个组件,例如:

GameObject player;

player.AddComponent<PlayerManager>();

三、利用RequireComponent添加一个组件。

如:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(PlayerCharacterController))]
[RequireComponent(typeof(PlayerCharacterUI))]
[RequireComponent(typeof(PlayerQuestManager))]
[RequireComponent(typeof(PlayerSave))]
 

这种我们可以经常在脚本中见到,RequireComponent意思是表面这个类一定需要哪些组件,如果目前这些组件没有被加上,就自动加上。

猜你喜欢

转载自blog.csdn.net/weixin_42513339/article/details/84065332