Unity-子线程async与协程比较

//子线程async与协程比较
//1 协程 2个缺点:没有返回值,yield语句无法放入try catch中
//2 async 可以规避协程的没有返回值,yield语句无法放入try catch中2个缺点
//3 协程回调
//4 ayync 回调
//5 task 异步的队列逐个执行。
//6 await Task.Run(() =>{};里面的代码是是子线程,协程是主线程里的协同线程,严格意义偏主线程里执行。

//子线程async与协程比较
//1 协程 2个缺点:没有返回值,yield语句无法放入try catch中
//2 async 可以规避协程的没有返回值,yield语句无法放入try catch中2个缺点
//3 协程回调
//4 ayync 回调
//5 task 异步的队列逐个执行。
//6 await Task.Run(() =>{};里面的代码是是子线程,协程是主线程里的协同线程,严格意义偏主线程里执行。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class Demo : MonoBehaviour
{
   
    public Image move;
    public Image myImage;
    public Image myImage2;
    List<Sprite> mySprite = new List<Sprite>();
    List<GameObject> gameObjects = new List<GameObject>();


    void Start()
    {
   
        
        Test1_1();
        Test1_2();
        StartCoroutine(Test2_1());
        StartCoroutine(Test2_2());

        Task<int> testInt = Test1_3();
        Debug.Log($"testInt.Result = {
     testInt.Result}");


        //协程模拟有返回值的做法
        //一般lambda表达式带参数意义不是很大,如果需要带参数,如下的实例比较好。
        //比较有意义的带参数的使用的lambda表达式
        //拿到图片的路径,转换为Texure,然后把Texure转换为Image的sprite,Image显示出来
        //路径为:Assets/StreamingAssets/a.png
        StartCoroutine(GetLocalTexture(StreamingAssetsPath("a.png"), (texture) =>
        {
   
            //图片资源, new Vector4(40, 40, 40, 42))表示图片在slice模式下切四个角的大小。
            Sprite spriteFromWeb =
            Sprite.Create(
            texture,
            new Rect(0, 0, texture.width, texture.height),
            new
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值