---
title: คอร์รูทีน
slug: world-sdk-guide-th/Ryp1p51ALInB4LNWGcWgY
docTags: 
createdAt: 2024-09-02T08:34:13.135Z
---

โคโรทีนคือส่วนประกอบของโปรแกรมที่ทำงานแบบมัลติทาสก์ที่ไม่ถูกขัดจังหวะ ซึ่งทำงานโดยการหยุดการทำงานและจากนั้นดำเนินการต่อจากจุดที่หยุดไว้ มันถูกใช้เพื่อดำเนินการฟีเจอร์เช่นการทำงานแบบอะซิงโครนัส ข้อยกเว้น ลูปเหตุการณ์ ตัววนซ้ำ รายการไม่สิ้นสุด และท่อ

คลิกที่นี่เพื่อเรียนรู้เพิ่มเติมเกี่ยวกับโคโรทีนของ Unity

:::hint{type="info"}
**📘 โคโรทีน**

[https://docs.unity3d.com/kr/current/Manual/Coroutines.html](https://docs.unity3d.com/kr/current/Manual/Coroutines.html)
:::



การนำโคโรทีนของ Unity ไปใช้ใน ZEPETOScript มีดังนี้

```javascript
import { ZepetoScriptBehaviour } from 'ZEPETO.Script';
import { WaitForSeconds } from 'UnityEngine';

export default class Coroutine extends ZepetoScriptBehaviour {
    
    private current: number;
    
    Start() {
        this.current = 0;
        console.log(`start routine`);
        this.StartCoroutine(this.DoRoutine());
    }

    *DoRoutine() {
        while(true) {
            yield null;
            console.log(`[${this.current++}] Wait to next routine..`);
            yield new WaitForSeconds(1);
        }
    }
}
```

ตรวจสอบหน้าจอผลลัพธ์ของโค้ดทดสอบด้านล่าง

![](https://api.archbee.com/api/optimize/fCt3n1oCa8rgNJ8fw9I2N-yagc5tebV4H6DBE7pWuAA-20240904-102426.png)

