当前位置:网站首页>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中的销毁回调方法
边栏推荐
- Kotlin基础学习 14
- Grpc quick practice
- C reflection practice
- ThreadLocal详解
- Go执行shell命令
- [HCIA continuous update] overview of dynamic routing protocol
- Eight steps of agile development process
- One of the future trends of SAP ui5: embrace typescript
- Form custom verification rules
- Screenshot literacy tool download and use
猜你喜欢
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
Verilog 过程赋值 区别 详解
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
JS <2>
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
Verilog 避免 Latch
[HCIA continuous update] overview of dynamic routing protocol
《MATLAB 神經網絡43個案例分析》:第42章 並行運算與神經網絡——基於CPU/GPU的並行神經網絡運算
On redis (II) -- cluster version
初出茅庐市值1亿美金的监控产品Sentry体验与架构
随机推荐
Apple added the first iPad with lightning interface to the list of retro products
Kotlin基础学习 16
C shallow copy and deep copy
Custom classloader that breaks parental delegation
venn圖取交集
aaaaaaaaaaaaa
Discussion on related configuration of thread pool
Find duplicates [Abstract binary / fast and slow pointer / binary enumeration]
知物由学 | 自监督学习助力内容风控效果提升
Go execute shell command
The capacity is upgraded again, and the new 256gb large capacity specification of Lexar rexa 2000x memory card is added
Verilog avoid latch
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
GSE104154_scRNA-seq_fibrotic MC_bleomycin/normalized AM3
汇率的查询接口
Global and Chinese market of X-ray detectors 2022-2028: Research Report on technology, participants, trends, market size and share
竞争与冒险 毛刺
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
Docker installs canal and MySQL for simple testing and implementation of redis and MySQL cache consistency
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions