在Unity5.6.5f1中使用C#7语法

备忘,记忆力越来越差了,必需把这种琐碎的东西记下来,以防1年后想再用完全没头绪。

之前试过用C#6语法,但是怎么配置操作的完全没印象了。

首先去这下载扩展

https://bitbucket.org/alexzzzz/unity-c-5.0-and-6.0-integration/src

左边Donwload选项,写本文时已经支持

C# 7.2 support for Net 4.6 in Unity 2017.1+

但是我目前用的5.6.5,所以下的

CSharp70Support 2.0.6 (for Unity 5.6).zip

用法,新建工程,CSharp7

把解压文件CSharp70Support放到Assets同级目录

管理员权限运行ngen install.cmd

CSharp70Support.unitypackage文件在工程中导入,生成CSharp vNext Support

写入测试代码,代码来源

https://www.cnblogs.com/newnj/p/6530394.html

using UnityEngine;
using System.Collections;


public class CS7Test : MonoBehaviour
{
    public string TestString { get; set; } = "Hello World";

    public class Person
    {
        public int age;
        public string name;

        public Person(int age, string name)
        {
            this.age = age;
            this.name = name;
        }
    };

    public class Male : Person
    {
        public Male(int age, string name) : base(age, name)
        {
            this.age = age;
            this.name = name;
        }
    };

    public class Female : Person
    {
        public Female(int age, string name) : base(age, name)
        {
            this.age = age;
            this.name = name;
        }
    };


    void Start()
	{
        Debug.Log(TestString);

        test1();
        test2();
        test3();
        test4();
        test5();

    }

    void test1()
    {
    	var str = "7";
    	// if(int.TryParse(str, out int number))
    	if(int.TryParse(str, out var number))
    	{
    		Debug.Log(number);
    	}
    }

    void test2()
    {
        Person p1 = new Male(5, "John");
        Person p2 = new Female(10, "Mary");

        Person cp = p1;
        //Person cp = p2;

        // switch支持非常量类型
        switch (cp)
        {
            case Male m:
                Debug.Log("Male:" + m.name);
                break;
            case Female f:
                Debug.Log("Female:" + f.name);
                break;
            default:
                break;
        }
    }

    public class GlobalData
    {
        private static int UserCount = 0;

        public static int GetCount()
        {
            return UserCount;
        }

        public static ref int GetCountNew()
        {
            return ref UserCount;
        }
    }

    void test3()
    {
        var count = GlobalData.GetCount();
        ++count;

        Debug.Log(GlobalData.GetCount());//0

        ref int countNew = ref GlobalData.GetCountNew();
        ++countNew;

        Debug.Log(GlobalData.GetCount());//1
    }

    //本地函数
    void test4()
    {
        Debug.Log("test4==========");

        //本地函数
        void Writer(IEnumerable objs)
        {
            foreach(var item in objs)
            {
                switch(item)
                {
                    case null:break;
                    case IEnumerable enumerable:
                        Writer(enumerable);
                        break;
                    default:
                        Debug.Log(item);
                        break;
                }
            }
        }
        Writer(new object[]
        {
            1,2,new[] {"3", "4"}
        }
        );
    }

    void test5()
    {
        long id = 1234_5678_9012_3456;
        Debug.Log(id);//1234567890123456
    }


  void Update()
  {
		
  }
}

  

其他版本配置应该类似,可以看看作者主页自带的说明,但是好像只有最新版本才有说明,其他版本比如C#6可能文件都不同,要自己测试,随机应变。

顺便一提,编译速度非常慢,感觉除了最后一个显示数字可以加下划线对我有用外,其他功能完全用不上,

这种看起来很方便的语法糖,就像C++的STL一样,看起来方便好用,节省时间,

实际上在不了解语法的外人看来,要花更多的时间去学习,才能看懂写的是什么鬼,不容易记住。

而且长期不用,下次用时还要重新复习才能想得起来用法,完全不适合我这种人使用。

原文地址:https://www.cnblogs.com/kileyi/p/8227902.html