当前位置:网站首页>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中的销毁回调方法
边栏推荐
- Large screen visualization from bronze to the advanced king, you only need a "component reuse"!
- OSPF LSA message parsing (under update)
- Review materials of project management PMP high frequency examination sites (8-1)
- uniapp 使用canvas 生成海报并保存到本地
- [golang] leetcode intermediate bracket generation & Full Permutation
- C shallow copy and deep copy
- Global and Chinese market of gynaecological health training manikin 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
- Principle of computer composition - interview questions for postgraduate entrance examination (review outline, key points and reference)
- halcon图像矫正
猜你喜欢

OSPF LSA message parsing (under update)

Detailed explanation of ThreadLocal

How to establish its own NFT market platform in 2022

Review materials of project management PMP high frequency examination sites (8-1)

Getting started with MQ

Discrimination between sap Hana, s/4hana and SAP BTP

Learn PWN from CTF wiki - ret2shellcode

只需简单几步 - 开始玩耍微信小程序

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

Failed to upgrade schema, error: “file does not exist
随机推荐
Cache processing scheme in high concurrency scenario
GSE104154_ scRNA-seq_ fibrotic MC_ bleomycin/normalized AM3
Named block Verilog
In depth interpretation of pytest official documents (26) customized pytest assertion error information
Kotlin 基础学习13
Verilog timing control
Aaaaaaaaaaaa
Kubernetes cluster storageclass persistent storage resource core concept and use
Merge interval, linked list, array
Load different fonts in QML
Kotlin基础学习 17
MySQL advanced (Advanced) SQL statement (II)
Kotlin基础学习 14
汇率的查询接口
2022 hoisting machinery command examination paper and summary of hoisting machinery command examination
Force deduction daily question 540 A single element in an ordered array
Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
Kotlin基础学习 15
/silicosis/geo/GSE184854_ scRNA-seq_ mouse_ lung_ ccr2/GSE184854_ RAW/GSM5598265_ matrix_ inflection_ demult
Apple added the first iPad with lightning interface to the list of retro products