当前位置:网站首页>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().
边栏推荐
- The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
- ERROR: Cannot uninstall ‘PyYAML‘. It is a distutils installed project and thus we cannot accurately
- Tronapi-波场接口-PHP版本--附接口文档-基于ThinkPHP5封装-源码无加密-可二开-作者详细指导-2022年6月28日11:49:56
- Four ways for flinksql to customize udtf
- After the market value evaporated by 65billion yuan, the "mask king" made steady medical treatment and focused on condoms
- Definition of variables and assignment of variables in MySQL
- rpm2rpm 打包步骤
- PG基础篇--逻辑结构管理(表继承、分区表)
- zabbix-server启动失败处理方式
- Docker installation of mysql8 and sqlyong connection error 2058 solution [jottings]
猜你喜欢
Apple executives openly "open the connection": Samsung copied the iPhone and only added a large screen
黑马笔记---集合(Collection的常用方法与遍历方式)
Wechat launched the picture big bang function; Apple's self-developed 5g chip may have failed; Microsoft solves the bug that causes edge to stop responding | geek headlines
[Select] resource realization information, news, we media, blog applet (can be drained, open traffic master, with PC background management)
黑马笔记---包装类,正则表达式,Arrays类
Derivation of Park transformation formula for motor control
黑马笔记---常用日期API
uniapp支付之APP微信支付unicloud版(附源码)
JS converts an array to a two-dimensional array based on the same value
RK356x U-Boot研究所(命令篇)3.2 help命令的用法
随机推荐
Postman génère automatiquement des fragments de code Curl
机器学习笔记 - 自相关和偏自相关简介
一次 Keepalived 高可用的事故,让我重学了一遍它!
Hangzhou E-Commerce Research Institute: the official website (website) is the only form of private domain
2022-06-23 帆软部分公式及sql生成(月份、季度取数)
postman 自動生成 curl 代碼片段
黑马笔记---包装类,正则表达式,Arrays类
Definition of variables and assignment of variables in MySQL
电机控制Clarke(α/β)等幅值变换推导
Tronapi wave field interface PHP version - interface document attached - package based on thinkphp5 - source code without encryption - can be opened in two - detailed guidance of the author - 11:49:56
Illustration creating a stored procedure using Navicat for MySQL
The independent station is Web3.0. The national "14th five year plan" requires enterprises to build digital websites!
Android development interview real question advanced version (with answer analysis)
Understanding and mastery of ffmpeg avbufferpool
黑马笔记---集合(Collection的常用方法与遍历方式)
kubeedge的核心理念
Visual studio configures QT and implements project packaging through NSIS
Today in history: Microsoft acquires PowerPoint developers; SGI and MIPS merge
Open source of xinzhibao applet
电机控制park变换公式推导