2017-06-01 61 views
1

我正在尝试更新并在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:类型或命名空间名称'任务不存在命名空间中存在`系统。穿线”。您是否缺少装配参考?

+0

您应该使用商店中现有的免费线程库,您使用的代码示例不是最大的。 –

回答

3

Unity基于.NET 2.0,引入了一些.NET 3.5功能。任务是一个.NET 4.0功能。您需要等待Unity 2017.1发布(或使用the beta version),它将通过选择加入选项来支持.NET 4.6。有关更多详细信息,请参见this forum post

我们想让论坛上的每个人都知道我们未来计划的单机运行时升级版本 。

Unity 2017.1发布将很快作为公开测试版发布。在此版本的 中,Mono运行时升级功能将是一个选项。对于给定的项目 ,您可以选择使用Unity的现有版本的Unity(支持.NET 3.5)或新的Mono运行时(支持.NET 4.6 )。在Unity 2017.1中,默认设置是使用较旧的 版本的Mono。很快(也许是Unity的2017.2版本),我们将使 成为新的Mono运行时默认设置,并将旧运行时作为选项。之后的 仍然会删除对旧运行时的支持。更多详细信息将很快发布在博客文章中,但我们希望首先让这个 论坛的每个人都知道。

我们非常感谢你们为提高这一进程所提供的许多时间,精力和反馈。我们的团队专注于 向所有Unity用户发送Mono运行时升级,而不会破坏 。你已经投入的努力找到许多破门而出的东西 是非常宝贵的。

+0

感谢您的回复,Scott,这很有道理。在Unity编译设置 - >播放器设置 - >其他设置 - >配置 - > Api兼容级别有一个.NET 4.6选项。你知道那将用于什么吗? – sethdippold21

+0

还没有玩过任何新的脚本东西,所以没有。 –