Unity C# 中Async await 的几种使用方式

Unity中 Async - await 的 几种实用场景


//Asyn 常用方法
public class AsynFun
{
    public void Main()
    {
        TryGetUnittOnLocal("sss");
       // OnRunStarted = OnRunStarted2;
    }

	//返回上级目录。leave设置级数
	string UpDirectory(string path,int leave)
    {
        string destPath = Path.GetFullPath(path);
        for (int i = 0; i < leave; i++)
        {
                Directory.SetCurrentDirectory(Directory.GetParent(destPath).FullName);
                destPath = Directory.GetCurrentDirectory();
        }
        return destPath;
     }
	
	// 异步拷贝文件
     async Task CopyToAndriodRoot()
     {
        string orginPath = Path.Combine(Application.persistentDataPath, "test.txt");

        string destNewPath = UpDirectory(Application.persistentDataPath, 3); 

        if (!Directory.Exists(destNewPath))
        {
                Debug.Log("没有这个文件夹路径 :" + destNewPath);
        }
        else
        {
                destNewPath = Path.Combine(destNewPath, "test.txt");

                if (File.Exists(orginPath))
                {
                    await Task.Run(() => {

                        File.Copy(orginPath, destNewPath,true);

                    });
                }
                else
                {
                    Debug.Log("没有这个文件 :" + orginPath + "\n" + "新文件夹 :" + destNewPath);
                }
        }

          
    }
        
    /// 
    /// 延时等待
    /// 
    public async void Delay()
    {
        await Task.Delay(2000);

        //操作//
    }
    /// 
    /// 延时三针再操作
    /// 
    public async void DelaySet()
    {
        await Task.Yield();
        await Task.Yield();
        await Task.Yield();

        //操作//
    }


    Func<Task> OnRunStarted;
    /// 
    /// 等待一个委托方法有返回之后再运行
    /// 
    public async void WaitFun()
    {
        await OnRunStarted?.Invoke();
        //操作//
    }

    async Task OnRunStarted2()
    {

    }

    /// 
    /// 通过BOOL判断,来暂停现成
    /// 
    public async void BoolPause()
    {
        bool isRun = false;
        while (!isRun)
        {
            await Task.Yield();
        }

        //操作//
    }


    /// 
    /// 返回一个已经完成的Task对象【未测试】
    /// 
    /// 
    public Task ReturnCompletedTask()
    {
        return Task.CompletedTask;
    }

    /// 
    /// 返回值为Task
    /// 
    public async Task<Dictionary<int,string>> RetureTask()
    {
        return await Task.Run(()=> {
            return new Dictionary<int, string>();
        });

        //操作//
    }


    /// 
    /// 异步读取文本文件【写入同样这样】
    /// 
    /// 
    public async void TryGetUnittOnLocal(string filePath)
    {
        try
        {
           // string unitJson = await Task.Run(() => File.ReadAllText(filePath));

            Task<string> task = Task.Run(() => File.ReadAllText(filePath));
            await task;
            string unitJson = task.Result;

            //List dd = new List();
            //dd.Where(str => str =="d").ToList();
        }
        catch (Exception ex)
        {
            Debug.LogWarning(ex);
        }
    }

你可能感兴趣的:(Unity,unity,C#,Async,await,异步)