RequireComponent的使用
来源:互联网 发布:php打印直角三角形 编辑:程序博客网 时间:2024/06/06 04:47
当某个脚本必须依赖其他脚本或者组件共同使用时,为了避免人为添加过程的操作失误,可以在代码中使用RequireComponent,它的作用就是添加该脚本时,会自动将所依赖的各个组件添加至gameobject上,避免人为操作的失误。
具体使用方法如下:
1、新建一个GameObject对象,同时新建两个C#脚本,例如图中的test1.cs与test2.cs
2、编辑test1.cs脚本,将编辑好后的test1拖至GameObject对象上
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Rigidbody),typeof(test2))]
public class test1 : MonoBehaviour {
}
或:
using System.Collections;
using System.Collections.Generic;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(test2))]
public class test1 : MonoBehaviour {
}
(也可以分开写,效果一样。同时可根据实际项目需求,修改[RequireComponent(typeof(Rigidbody))] 中的组件类型)
3、此时若手动删除Rigidbody对象,会有如下提示
因此,RequireComponent的使用可以很好的避免项目中因为相互依赖的组件缺失而导致不必要的错误。
阅读全文
0 0
- RequireComponent的使用
- RequireComponent的使用
- RequireComponent的使用
- RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体)。这个可以有效的避免组装错误
- Unity3D脚本依赖RequireComponent(自动添加关联的脚本)
- Unity3D脚本依赖RequireComponent(自动添加关联的脚本)
- 我的世界开发日志3——什么是RequireComponent
- RequireComponent 需要组件
- Unity3D之RequireComponent()
- Unity3d 编辑器扩展系列-RequireComponent
- [ExecuteInEditMode] 和 [RequireComponent(typeof(UIRoot))]理解
- Unity编辑器扩展之RequireComponent等详解
- RequireComponent需要组件与Quaternion 四元数
- AddComponentMenu ContextMenu ExecuteInEditMode HideInInspector NonSerialized RPC RequireComponent Se
- GUID使用的使用
- ${}的使用
- 的使用
- @[],@()的使用
- Scala基础—多线程示例
- java.net.UnknownHostException: api.weixin.qq.com解决办法
- 嵌入式面试题总结-C语言知识点相关
- Semantic-Versioning 语义化版本控制
- 使用IntellJ idea以maven作为项目构建的方式创建web工程
- RequireComponent的使用
- spring boot整合memcached
- Visual studio如何更改应用程序的文件描述,以便在进程启动之后显示想要的进程描述
- web前端知识1
- 用命令将本地项目上传到GitHub
- 值类型与引用类型的区别
- Android开发中的权限设置
- android webview cookie (遇到的坑,终极方案,解决你的疑惑)
- ZooKeeper启动报错 JAVA_HOME is incorrectly set