在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一樣,看起來方便好用,節省時間,

實際上在不了解語法的外人看來,要花更多的時間去學習,才能看懂寫的是什么鬼,不容易記住。

而且長期不用,下次用時還要重新復習才能想得起來用法,完全不適合我這種人使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM