简体   繁体   English

Unity使用C#任务构建错误

[英]Unity build errors using C# Tasks

I am trying to update and build a C# project in Unity 5.6.1 to later be run on the Hololens. 我正在尝试在Unity 5.6.1中更新并构建一个C#项目,以便稍后在Hololens上运行。 Originally, the project used System.Threading, but I think that I need to use Tasks instead because of some issues with Unity. 最初,该项目使用了System.Threading,但我认为我需要使用Tasks,因为Unity存在一些问题。

When I open the project in Visual Studio, it runs fine using Tasks. 当我在Visual Studio中打开项目时,它使用任务运行正常。 When I build the project in Unity, it says that Tasks do not exist (error below). 当我在Unity中构建项目时,它表示任务不存在(下面的错误)。 I am building with .Net 4.6 in Unity on Universal 10 SDK. 我在Universal on Universal 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();
        }
    }
}

Unity error given: 给出的统一错误:

Assets/SampleCSApp/BaseThread.cs(14,34): error CS0234: The type or namespace name `Tasks' does not exist in the namespace `System.Threading'. Assets / SampleCSApp / BaseThread.cs(14,34):错误CS0234:命名空间“System.Threading”中不存在类型或命名空间名称“Tasks”。 Are you missing an assembly reference? 你错过了装配参考吗?

Unity is based on .NET 2.0 with some .NET 3.5 features thrown in. Tasks is a .NET 4.0 feature. Unity基于.NET 2.0,引入了一些.NET 3.5功能。任务是.NET 4.0的一项功能。 You need to wait for Unity 2017.1 to be released (or use the beta version ) which will support .NET 4.6 via a opt-in option. 您需要等待Unity 2017.1发布(或使用测试版 ),它将通过选择加入选项支持.NET 4.6。 See this forum post for more details. 有关详细信息,请参阅此论坛帖子

We wanted to let everyone on the forum know our future plans for the Mono runtime upgrade. 我们想让论坛上的每个人都知道我们未来的Mono运行时升级计划。

The Unity 2017.1 release will be available as a public beta soon. Unity 2017.1版本很快将作为公开测试版提供。 In this release, the Mono runtime upgrade feature will be an option. 在此版本中,Mono运行时升级功能将是一个选项。 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). 对于给定项目,您可以选择使用Unity中的现有Mono版本(支持.NET 3.5)或新的Mono运行时(支持.NET 4.6)。 In Unity 2017.1, the default setting is to use the older version of Mono. 在Unity 2017.1中,默认设置是使用较旧版本的Mono。 Soon (maybe the 2017.2 release of Unity) we will make the new Mono runtime default, with the old runtime as an option. 很快(可能是Unity的2017.2版本)我们将使用旧的运行时作为选项,使新的Mono运行时默认。 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. 我们的团队专注于将Mono运行时升级发送给所有Unity用户,而不会破坏任何内容。 The work you have put in to find many of the things that did break is invaluable. 你找到许多破坏的东西是非常宝贵的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM