当前位置:网站首页>Development of unity script program
Development of unity script program
2022-06-30 13:01:00 【ht_ game】
This is my right to Unity Check and fill the gaps in script knowledge , A lot of familiar knowledge has not been written .
Unity in C# Script specification
Inherited from MonoBehavior class
Unity All classes in scripts attached to game objects in must inherit MonoBehavior class ( Directly or indirectly ),MonoBehavior Class defines various callback methods (Start()、Update() …).
The class name must match the file name
C# The class name in the script needs to be written manually , And the class name must be the same as the file name , Otherwise, when the script is attached to the GameObject , The console will report an error
Use Awake or Start Method initialization
The code used to initialize the script must be placed in Awake or Start In the method .Awake Than Start Run earlier , Is run when the scene is loaded . and Start Method is called the first time Update or FixedUpdate Method used before .
Unity Co program in script ( coroutines ) There are different grammatical rules
Unity Medium coroutines Must be IEnumerator Return type
yield use yield return replace
using System.Collections;
using System.Collections.Generic;
using UnityEngine;// Import system package
public class NewBehaviourScript : MonoBehaviour // Declaration class
{
private void Start()
{
StartCoroutine(SomeCoroutine());// Start the coroutines
}
IEnumerator SomeCoroutine()//C# coroutines
{
yield return 0;// Wait for one frame
yield return new WaitForSeconds(2);// wait for 2s
}
}
Only variables that meet certain conditions can be displayed in the property panel
Only serialized member variables can be displayed in the property panel , If the property wants to be displayed in the property panel , Then it must be public type
Try to avoid constructors
Do not initialize any variables in the constructor , Use Awake and Start Method realization . because Unity The constructor will be called automatically , It may be called by a prefab or an inactive GameObject .
Unity The singleton pattern in
Because the script file inherits MonoBehavior
Using constructors in singleton mode can have serious consequences , It will cause similar random null parameter exceptions .
To implement the singleton pattern, you cannot use constructors , But use Awake or Start Method .
in fact , There is no need to inherit from MonoBehaviour Write any code in the constructor of the class
How to be in Unity Use singleton mode in the script
public static ItemMgr instance;
void Awake()
{
instance=this;
}
debugging
stay Unity in , have access to print() and Debug.Log() Output debugging information . however print() Only in Mono Used in , So generally, it is best to use Debug.Log().
边栏推荐
- mqtt-ros模拟发布一个自定义消息类型
- Flinksql customizes udatf to implement topn
- Exploring the source code of Boca Cross Chain Communication: Elements
- dataworks 同步maxcomputer 到sqlserver ,汉字变乱码了,请问怎么解决
- 写信宝小程序开源
- Resource realization applet opening traffic main tutorial
- Substrate 源码追新导读: Pallet Alliance 并入主线,
- Sqlserver query code is 936 simplified Chinese GBK. Should I write 936 or GBK?
- Dark horse notes -- List series collections and generics
- PG基础篇--逻辑结构管理(表继承、分区表)
猜你喜欢
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
ABAP工具箱 V1.0(附实现思路)
今日睡眠质量记录80分
Qt中的数据库使用
Resource realization applet opening wechat official small store tutorial
MATLAB小技巧(22)矩阵分析--逐步回归
微信小程序报错:TypeError: Cannot read property ‘setData‘ of undefined
【C】深入理解指针、回调函数(介绍模拟qsort)
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
Questionnaire star questionnaire packet capturing analysis
随机推荐
Substrate 源码追新导读: 波卡系波卡权重计算全面更新, Governance 2.0 版本的优化和调整
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
江西财经大学智慧江财登录分析
kaniko官方文档 - Build Images In Kubernetes
Event handling in QT
Postman automatically generates curl code snippets
Definition of variables and assignment of variables in MySQL
In line with the trend of media integration, Zhongke Wenge and Meishe jointly create digital intelligence media publicity
Kubeedge's core philosophy
golang基础 —— 切片几种声明方式
Dataworks synchronizes maxcomputer to sqlserver. Chinese characters become garbled. How can I solve it
Rk356x u-boot Institute (command section) 3.2 usage of help command
Unity脚本的基础语法(5)-向量
深度长文探讨Join运算的简化和提速
elementui中清除tinymce富文本缓存
机器学习笔记 - 自相关和偏自相关简介
你想要的异常知识点都在这里了
商品服务-平台属性
Database usage in QT
【OpenGL】OpenGL Examples