我正在尝试更新并在Unity 5.6.1中构建一个C#项目,以便以后在Hololens上运行。最初,该项目使用了System.Threading,但我认为我需要使用Tasks,因为Unity有一些问题。使用C#任务构建Unity错误
当我在Visual Studio中打开项目时,它使用任务运行正常。当我在Unity中构建项目时,它说任务不存在(下面的错误)。我在Unity 10 SDK中使用.Net 4.6编译。
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace SampleCSApp
{
// Based on http://answers.unity3d.com/questions/357033/unity3d-and-c-coroutines-vs-threading.html
class BaseThread
{
private bool _isDone = false;
private object _handle = new object();
//private System.Threading.Thread _thread = null;
private System.Threading.Tasks.Task _thread = null;
private static int WAIT_FOR_MS = 100;
public bool IsDone
{
get
{
bool tmp;
lock (_handle)
{
tmp = _isDone;
}
return tmp;
}
set
{
lock (_handle)
{
_isDone = value;
}
}
}
public virtual void Start()
{
_thread = new System.Threading.Tasks.Task(Run);
//_thread = new System.Threading.Thread(Run);
_thread.Start();
}
public virtual void Abort()
{
_thread.Dispose();
//_thread.Abort();
}
protected virtual void ThreadFunction() { }
public void WaitFor()
{
while (!IsDone)
{
_thread.Wait(WAIT_FOR_MS);
//System.Threading.Thread.Sleep(WAIT_FOR_MS);
}
}
private void Run()
{
ThreadFunction();
}
}
}
团结错误给出:
资产/ SampleCSApp/BaseThread.cs(14,34):错误CS0234:类型或命名空间名称'任务不存在命名空间中存在`系统。穿线”。您是否缺少装配参考?
您应该使用商店中现有的免费线程库,您使用的代码示例不是最大的。 –