c# - 使用 C# 任务的 Unity 构建错误

标签 c# visual-studio win-universal-app unity3d

我正在尝试在 Unity 5.6.1 中更新和构建一个 C# 项目,以便稍后在 Hololens 上运行。最初,该项目使用 System.Threading,但我认为由于 Unity 的一些问题我需要使用 Tasks。

当我在 Visual Studio 中打开项目时,它使用任务运行良好。当我在 Unity 中构建项目时,它说任务不存在(下面的错误)。我正在 Universal 10 SDK 上的 Unity 中使用 .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();
        }
    }
}

给出的统一错误:

Assets/SampleCSApp/BaseThread.cs(14,34): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Are you missing an assembly reference?

最佳答案

Unity 基于 .NET 2.0,并加入了一些 .NET 3.5 功能。Tasks 是 .NET 4.0 的一项功能。您需要等待 Unity 2017.1 发布(或使用 the beta version ),它将通过选择加入选项支持 .NET 4.6。参见 this forum post了解更多详情。

We wanted to let everyone on the forum know our future plans for the Mono runtime upgrade.

The Unity 2017.1 release will be available as a public beta soon. In this release, the Mono runtime upgrade feature will be an option. For a given project, you can choose to use the existing version of Mono in Unity (with .NET 3.5 support) or the new Mono runtime (with .NET 4.6 support). In Unity 2017.1, the default setting is to use the older version of Mono. Soon (maybe the 2017.2 release of Unity) we will make the new Mono runtime default, with the old runtime as an option. Later still, we will remove support for the old runtime. More details will be coming in a blog post soon, but we wanted to let everyone on this forum know first.

We really appreciate the time, effort, and feedback so many of you have provided to move this process forward. Our team is focused on shipping the Mono runtime upgrade to all of the Unity users without breaking things. The work you have put in to find many of the things that did break is invaluable.

关于c# - 使用 C# 任务的 Unity 构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44315073/

相关文章:

c# - 使用方法的 MethodHandle 作为键来缓存无参数方法的结果是否安全?

c# - 使用 LINQ 将嵌套循环展平为一个列表

c# - 如何正确地将类与框架解耦(源代码级解耦)

c# - 在 visual studio 2005 中禁用集成网络服务器的自动启动

c# - 我如何使用 Visual Studio 2005/resharper 自动将 "using"语句添加到文件夹、命名空间或项目中的每个文件

xaml - ContentDialog 的大小不适合具有大 TextBlock 的通用应用程序中的内容

wpf - 更改 UIElement 的角度

C# 强制运算符?

C++ - 你能将一个静态库构建到另一个静态库中吗?

c# - 未找到依赖的 dll