当前位置:网站首页>Unity脚本程序的开发
Unity脚本程序的开发
2022-06-30 12:18:00 【ht_game】
这是我对Unity脚本知识的查漏补缺,很多已经熟悉的知识就没有写。
Unity中C#脚本规范
继承自MonoBehavior类
Unity中所有挂载到游戏对象上的脚本中的类必须继承MonoBehavior类(直接或间接),MonoBehavior类中定义了各种回调方法(Start()、Update() …)。
类名必须匹配文件名
C#脚本中类名需要手动编写,并且类名必须和文件名相同,否则当脚本挂载到游戏对象时,控制台会报错
使用Awake或Start方法初始化
用于初始化脚本的代码必须置于Awake或Start方法中。Awake比Start运行更早,是在加载场景时运行。而Start方法是在第一次调用Update或FixedUpdate方法之前使用。
Unity脚本中协同程序(协程)有不同的语法规则
Unity中的协程必须是IEnumerator返回类型
yield用yield return替代
using System.Collections;
using System.Collections.Generic;
using UnityEngine;//导入系统包
public class NewBehaviourScript : MonoBehaviour //声明类
{
private void Start()
{
StartCoroutine(SomeCoroutine());//启动协程
}
IEnumerator SomeCoroutine()//C#协程
{
yield return 0;//等待一帧
yield return new WaitForSeconds(2);//等待2s
}
}
只有满足特定情况变量才能显示在属性面板中
只有序列化的成员变量才能显示在属性面板中,如果属性想在属性面板中显示,那么其必须是public类型
尽量避免构造函数
不要在构造函数中初始化任何变量,要用Awake和Start方法实现。因为Unity会自动调用构造函数,它或许会被预制体或未激活的游戏对象调用。
Unity中的单例模式
因为脚本文件继承MonoBehavior
单例模式使用构造函数可能会导致严重后果,会带来类似随机的空参数异常。
要实现单例模式就不能使用构造函数,而是使用Awake或Start方法。
事实上,没有必要在继承自MonoBehaviour的类的构造函数中写任何代码
如何在Unity脚本中使用单例模式
public static ItemMgr instance;
void Awake()
{
instance=this;
}
调试
在Unity中,可以使用print()和Debug.Log()输出调试信息。但是print()只能在Mono的类中使用,所以一般情况下最好使用Debug.Log()。
边栏推荐
- 黑马笔记---List系列集合与泛型
- Scratch drawing square electronic society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- Redis configuration files and new data types
- Sublist3r error reporting solution
- Terms related to JMeter performance test and performance test passing standards
- 【MySQL】MySQL的安装与配置
- QT MSVC installation and commissioning
- Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
- Splitting e-commerce systems into micro services
- 江西财经大学智慧江财登录分析
猜你喜欢
![[MySQL] MySQL installation and configuration](/img/82/8500949734e57e6a1047c4e838f625.png)
[MySQL] MySQL installation and configuration

Redis-緩存問題

FlinkSQL自定义UDTF使用的四种方式

Sarsa notes

Tencent two sides: @bean and @component are used on the same class. What happens?

Android development interview real question advanced version (with answer analysis)
![[one day learning awk] Fundamentals](/img/09/a3eb03066eb063bd8594065cdce0aa.png)
[one day learning awk] Fundamentals
![[one day learning awk] array usage](/img/75/4333452142a3c7325e0712f3306985.png)
[one day learning awk] array usage

Shell基础入门

【一天学awk】基础中的基础
随机推荐
腾讯二面:@Bean 与 @Component 用在同一个类上,会怎么样?
Qt中的数据库使用
MATLAB小技巧(22)矩阵分析--逐步回归
Redis的配置文件及新数据类型
Google refutes rumors and gives up tensorflow. It's still alive!
【一天学awk】基础中的基础
FFMpeg AVBufferPool 的理解与掌握
视频按每100帧存一个文件夹,处理完再图片转视频
[QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
基于ThinkPHP5封装tronapi-波场接口-PHP版本--附接口文档-20220627
Flinksql customizes udatf to implement topn
【OpenGL】OpenGL Examples
Essay: Research on smart home scheme
Android development interview real question advanced version (with answer analysis)
Sublist3r error reporting solution
Redis cache problem
How to select an OLAP database engine?
Substrate 源码追新导读: 波卡系波卡权重计算全面更新, Governance 2.0 版本的优化和调整
Q-learning notes
Redis-缓存问题