//子线程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