当前位置:网站首页>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中的销毁回调方法
边栏推荐
- JDBC details
- [C Advanced] brother Peng takes you to play with strings and memory functions
- Kubernetes cluster storageclass persistent storage resource core concept and use
- ORA-01547、ORA-01194、ORA-01110
- aaaaaaaaaaaaa
- JIT deep analysis
- FFMpeg AVFrame 的概念.
- Kotlin基础学习 16
- Grpc quick practice
- IPhone 6 plus is listed in Apple's "retro products" list
猜你喜欢

Kubernetes cluster storageclass persistent storage resource core concept and use

SAML2.0 笔记(一)

Gradle foundation | customize the plug-in and upload it to jitpack

JIT deep analysis

One of the future trends of SAP ui5: embrace typescript

初出茅庐市值1亿美金的监控产品Sentry体验与架构

Getting started with MQ

Which of PMP and software has the highest gold content?

OSPF LSA message parsing (under update)

Discussion on related configuration of thread pool
随机推荐
Detailed explanation of ThreadLocal
Eight steps of agile development process
MySQL connection query and subquery
Delphi xe10.4 installing alphacontrols15.12
[HCIA continuous update] working principle of OSPF Protocol
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
3124. Word list
Kotlin 基础学习13
Comment élaborer une stratégie nuageuse à l'ère des nuages mixtes
高并发场景下缓存处理方案
This article describes the step-by-step process of starting the NFT platform project
In depth interpretation of pytest official documents (26) customized pytest assertion error information
[C Advanced] brother Peng takes you to play with strings and memory functions
MSI announced that its motherboard products will cancel all paper accessories
Cache processing scheme in high concurrency scenario
Qualcomm platform WiFi -- Native crash caused by WiFi
Global and Chinese market of autotransfusion bags 2022-2028: Research Report on technology, participants, trends, market size and share
竞争与冒险 毛刺
Sentry experience and architecture, a fledgling monitoring product with a market value of $100million
Work hard all day long and be alert at sunset