Unity多线程调用

写了一段废代码,不过以后可能有他的用处 

using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using Knight.Core;
using UnityEngine;
using UnityEngine.SceneManagement;

public class TaskAsset : MonoBehaviour
{
    void Start()
    {
        Task loadTask = AssetManager.Instance.LoadAsset("", "");
        Task.WhenAll(loadTask).ContinueWith((callback)=> {
            Debug.Log(callback.Result[0].result.Num);
        });
    }

    private void OnGUI()
    {
        if (GUILayout.Button("click"))
        {
            AssetTaskQueue.Instance.Dequeue();
        }
    }
}

  public interface IAssetTask
  {
      void Process();
      void Start();
  }

  public class AssetLoadTask : IAssetTask
  {
      public AssetLoadResult result;
      public AssetLoadTask(string rABName, string rAssetName)
      {
           
      }

      public AssetLoadTask(string rABName, bool rIsAllAssets)
      {

      }

      /// 
      /// 处理异步耗时操作
      /// 
      public void Process()
      {
        result = new AssetLoadResult();
        result.Num = 1999;
    }

    public void Start()
    {
        throw new System.NotImplementedException();
    }
}

  public class AssetTaskQueue:TSingleton
  {
      private Queue> ProcessQueue = new Queue>();

      private AssetTaskQueue() { }

      public void Enqueue(Task task) {
          ProcessQueue.Enqueue(task);
      }

      public void Dequeue() {
           ProcessQueue.Dequeue().Start();
      }
  }

  public class AssetLoadResult
  {
      public int Num;
  }


  public class AssetManager : TSingleton
  {
      private AssetManager() {

      }

      public Task LoadAsset(string rABName, string rAssetName) {
           Task task = new Task(()=> {
               AssetLoadTask t =  new AssetLoadTask(rABName, rAssetName);
               t.Process();
               return t;
           });
          AssetTaskQueue.Instance.Enqueue(task);
          return task;
      }

  }

 

你可能感兴趣的:(Unity,游戏开发)