当前位置:网站首页>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()。
边栏推荐
- Visual studio configures QT and implements project packaging through NSIS
- 黑马笔记---包装类,正则表达式,Arrays类
- Essay: Research on smart home scheme
- 机器学习笔记 - 自相关和偏自相关简介
- [QNX Hypervisor 2.2用户手册]6.2.3 Guest与外部之间通信
- 解决numpy.core._exceptions.UFuncTypeError: ufunc ‘add‘ did not contain a loop with signature matchin问题
- Sublist3r error reporting solution
- Pharmacy management system
- Splitting e-commerce systems into micro services
- Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
猜你喜欢

“\“id\“ contains an invalid value“

问卷星问卷抓包分析

After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms

Reading the table data of Tencent documents in the applet

Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
![[MySQL] MySQL installation and configuration](/img/82/8500949734e57e6a1047c4e838f625.png)
[MySQL] MySQL installation and configuration

Pinda general permission system (day 7~day 8)

Dqn notes

60 个神级 VS Code 插件!!

Flinksql customizes udatf to implement topn
随机推荐
Linux系统Redis的安装
数据仓库建设之确定主题域
Redis cache problem
QT implementation dynamic navigation bar
21. Notes on WPF binding
How to solve cross domain problems
电机控制park变换公式推导
Building a database model using power designer tools
Solve numpy core._ exceptions. Ufunctypeerror: UFUNC 'Add' did not contain a loop with signature matching
Hisilicon 3559 sample parsing: Venc
Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
Introduction to the novelty of substrat source code: indexing of call calls and fully completing the materialization of storage layer
Sword finger offer 05 Replace spaces: replace each space in the string s with "%20"“
基于ThinkPHP5封装-tronapi-波场接口-源码无加密-可二开--附接口文档-作者详细指导-2022年6月30日08:45:27
力扣之螺旋矩阵,一起旋转起来(都能看懂)
Shell基础入门
ffmpeg 杂项
Dqn notes
ECDSA signature verification in crypt
图解使用Navicat for MySQL创建存储过程