当前位置:网站首页>Unity脚本的基础语法(8)-协同程序与销毁方法
Unity脚本的基础语法(8)-协同程序与销毁方法
2022-07-02 03:25:00 【ht_game】
协同程序
协同程序,即在主程序运行的同时开启另一段逻辑处理来协同当前程序的执行。但它与多线程程序不同,所有的协同程序都是在主程序中运行的,它还是一个单线程程序。在Unity中可以通过StartCoroutine方法来启动协同。
StartCoroutine方法为MonoBehavior类中的一个方法,即该方法必须在MonoBehavior或继承于MonoBehavior的类中调用。StartCoroutine方法可以将返回值为IEnumerator的类型方法作为参数
void Start()
{
StartCoroutine(doThing1());//开启doThing1协同程序
}
IEnumerator doThing1()//声明doThing方法
{
yield return StartCoroutine(doThing2());//开启doThing2协同程序
}
IEnumerator doThing2()
{
yield return new WaitForSeconds(2);//协同程序休眠2s
}
与销毁相关的方法
在游戏的开发过程中,经常会遇见对象、组件、资源等在使用完毕后就失去了作用的情况,如果放任其不管,轻则影响项目运行效率,重则可能影响到项目的正常运行。因此,必须有一类方法来管理、删除这些没有用的资源。
| 函数 | 功能 |
|---|---|
| Object.Destroy | 删除游戏对象、组件或资源 |
| NetWork.Destroy | 销毁网络对象 |
| MonoBehavior.OnDestroy | 脚本被销毁时调用 |
Object.Destroy方法
Object.Destroy方法可以将对象立即销毁,也可以设置时间后销毁。如果删除的对象是一个组件,则该组件会被移除。
public GameObject obj;
void Start()
{
Destroy(obj.GetComponent<Rigidbody>());//销毁游戏物体的刚体组件
Destroy(obj, 5);//5s后删除obj游戏对象
}
NetWork.Destroy方法
NetWork.Destroy方法可以销毁网络对象,该对象有两种重载方式
public static void Destroy(NetworkViewID viewID)
public static void Destory(Gameobject gameobject)
1、系统会删除所有和该ViewID相关的物体
2、销毁网络上的游戏对象
Network.Destory(GetComponent<NetworkView>().viewID)
Network.Destory(gameobject)
MonoBehaviour.OnDestroy方法
MonoBehaviour.OnDestroy方法是MonoBehaviour中的销毁回调方法
边栏推荐
- Exchange rate query interface
- Common means of modeling: aggregation
- Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
- Generate random numbers that obey normal distribution
- PMP personal sprint preparation experience
- Start a business
- halcon图像矫正
- Gradle 笔记
- Verilog reg register, vector, integer, real, time register
- IPhone 6 plus is listed in Apple's "retro products" list
猜你喜欢

Download and use of the super perfect screenshot tool snipaste

Screenshot literacy tool download and use

JIT deep analysis

Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
![[golang] leetcode intermediate bracket generation & Full Permutation](/img/93/ca38d97c721ccba2505052ef917788.jpg)
[golang] leetcode intermediate bracket generation & Full Permutation

Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU

Form custom verification rules

Continuous assignment of Verilog procedure

Verilog timing control
![[JVM] detailed description of the process of creating objects](/img/6e/0803b6b63c48337985faae8d5cbe1a.png)
[JVM] detailed description of the process of creating objects
随机推荐
C#聯合halcon脫離halcon環境以及各種報錯解决經曆
Kotlin基础学习 17
Kotlin 基础学习13
Delphi xe10.4 installing alphacontrols15.12
Framing in data transmission
What is hybrid web containers for SAP ui5
表单自定义校验规则
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
Global and Chinese markets for welding equipment and consumables 2022-2028: Research Report on technology, participants, trends, market size and share
Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
Force deduction daily question 540 A single element in an ordered array
Detailed explanation of the difference between Verilog process assignment
KL divergence is a valuable article
Calculation of page table size of level 2, level 3 and level 4 in protection mode (4k=4*2^10)
Discussion on related configuration of thread pool
Detailed explanation of ThreadLocal
What is the binding path of SAP ui5
MySQL connection query and subquery
Just a few simple steps - start playing wechat applet
/silicosis/geo/GSE184854_scRNA-seq_mouse_lung_ccr2/GSE184854_RAW/GSM5598265_matrix_inflection_demult