Unity热更新方案C#Like(十五)-详解支持的C#特性:关键字:unsafe typeof nameof $ @ #pragma #warning #error

C#Like是Unity的热更方案,使用纯C#语言写出可以热更新的代码,就像可以在所有平台使用DLL(动态链接库)文件一样.遵从KISS设计原则,让用户轻松构建或升级成Unity的热更新项目.

简介

本篇主要介绍C#Like支持的C#特性:关键字:unsafe typeof nameof $ @ #pragma #warning #error

  • C#Like免费版:typeof;
  • C#Like完整版:多了unsafe nameof $ @ #pragma #warning #error.

C#Like免费版:

示范代码如下,提供对应方案模拟:


using UnityEngine;//示范using 命令  
using System;  
using Random = UnityEngine.Random;//示范using 别名  
using System.Text;  
using System.IO;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// test not classify keyword   
        /// </summary>  
        void TestKeyword()  
        {  
            Debug.LogError("示范关键字: 你不能在C#Like免费版里使用'$ sizeof unsafe #pragma #warning #error'关键字 (在完整版已支持).强烈推荐升级到完整版: ");  
            // 我们提供以下方案给免费用户:  
            // '$""': 使用string.Format (你的代码在编辑器看起来没有那么清爽).  
            // '@""': 直接使用""代替  
            // sizeof and unsafe: 直接使用数字  
            // #pragma #warning #error: 不使用  
  
            //内置数据类型  
            //Int32/UInt32/Int64/UInt64/Int16/UInt16/Char/Single/Double/Decimal/Boolean/SByte/Byte/String.  
            //等效于  
            //int/uint/long/ulong/short/ushort/char/float/double/decimal/bool/sbyte/byte/string.  
            Int32 i = 123;//equal to 'int i = 123;'  
            Debug.Log("test Int32 i = " + i);//输出 123  

            //示范关键字'typeof'  
            var tType = typeof(ExampleCSharp);//不要定义为'Type tType = typeof(ExampleCSharp);', 必须使用'var',因为获取到的类型在调试模式下和热更模式下的不同  
            Debug.Log("typeof(ExampleCSharp) = " + tType);//输出 CSharpLike.ExampleCSharp  
            var thisType = GetType();//不要定义为'Type thisType = GetType();', 必须使用'var',因为获取到的类型在调试模式下和热更模式下的不同  
            Debug.Log("thisType = " + thisType);//输出 CSharpLike.ExampleCSharp  
            Debug.Log("thisType == tType:" + (thisType == tType));//输出 True  
            if (this.GetType() == typeof(ExampleCSharp))  
                Debug.Log("GetType() == typeof(ExampleCSharp)");  
            else  
                Debug.Log("GetType() != typeof(ExampleCSharp)");  
        }  
    }  
}  

 C#Like完整版:

示范代码如下:


using UnityEngine;//示范using 命令  
using System;  
using Random = UnityEngine.Random;//示范using 别名  
using System.Text;  
using System.IO;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 示范其他没有分类的关键字   
        /// </summary>  
        void TestKeyword()  
        {  
            Debug.LogError("Test Keyword:");  
            //示范关键字'sizeof'  
            //'sizeof(int/uint/long/ulong/short/ushort/char/float/double/decimal/bool/sbyte/byte)'无需开启unsafe开关  
            Debug.Log("sizeof(int) = " + sizeof(int));//输出 sizeof(int) = 4  
  
            //该宏由Menu/Edit/Project Setting/Player/Other Settings/Allow 'unsafe' Code开关  
            //如果关闭unsafe后,还必须手动在Menu/Edit/Project Settings/Player/Other Settings/Scripting Define Symbols里移除该宏  
#if _CSHARP_LIKE_ALLOW_UNSAFE_CODE_   
            //示范在函数体内使用'unsafe'关键字  
            unsafe  
            {  
                Debug.Log("sizeof(Vector2) = " + sizeof(Vector2));//输出 sizeof(Vector2) = 8  
                Debug.Log("sizeof(Rect) = " + sizeof(Rect));//输出 sizeof(Rect) = 16  
            }  
            TestUnSafe();  
#endif  
            //示范关键字'typeof'  
            var tType = typeof(ExampleCSharp);//不要定义为'Type tType = typeof(ExampleCSharp);', 必须使用'var',因为获取到的类型在调试模式下和热更模式下的不同  
            Debug.Log("typeof(ExampleCSharp) = " + tType);//输出 CSharpLike.ExampleCSharp  
            var thisType = GetType();//不要定义为'Type thisType = GetType();', 必须使用'var',因为获取到的类型在调试模式下和热更模式下的不同  
            Debug.Log("thisType = " + thisType);//输出 CSharpLike.ExampleCSharp  
            Debug.Log("thisType == tType:" + (thisType == tType));//输出 True  
            if (this.GetType() == typeof(ExampleCSharp))  
                Debug.Log("GetType() == typeof(ExampleCSharp)");  
            else  
                Debug.Log("GetType() != typeof(ExampleCSharp)");  
  
            //内置数据类型  
            //Int32/UInt32/Int64/UInt64/Int16/UInt16/Char/Single/Double/Decimal/Boolean/SByte/Byte/String.  
            //等效于  
            //int/uint/long/ulong/short/ushort/char/float/double/decimal/bool/sbyte/byte/string.  
            Int32 i = 123;//equal to 'int i = 123;'  
            Debug.Log("test Int32 i = " + i);//输出 123  
              
            //示范关键字 "$", 格式化字符串的语法糖  
            string str = $"abc{i}sdf{"xyz"}";//等效于 'string str = string.Format("abc{0}sdf{1}", i, "xyz");'  
            Debug.Log("test keyword '$' with '\"', $\"abc{i}sdf{\"xyz\"} = " + str);//输出 abc123sdfxyz  
  
            //示范关键字 "@", 忽略转换符  
            string @if = @"{""a"":1,  
                ""b"":""abc""}";//等效于 'str = "{\"a\":1,\n                \"b\":\"abc\"}";'  
            Debug.Log("test keyword '@' with '\"', @if = " + @if);  
              
            //示范关键字 #pragma #warning #error  
#pragma warning disable CS0168  
            int j;//如果不设置'#pragma warning disable CS0168'会被编辑器提示警告CS0168  
#pragma warning restore CS0168  
//在编辑器里报一个警告  
#warning "This's a test #warning."    
#if UNITY_4_0  
#error "This's a test #error."   
#endif//UNITY_4_0  
        }  
  
        //该宏由Menu/Edit/Project Setting/Player/Other Settings/Allow 'unsafe' Code开关  
        //如果关闭unsafe后,还必须手动在Menu/Edit/Project Settings/Player/Other Settings/Scripting Define Symbols里移除该宏  
#if _CSHARP_LIKE_ALLOW_UNSAFE_CODE_  
        //示范在函数名使用'unsafe'关键字  
        unsafe void TestUnSafe()  
        {  
            Debug.Log("sizeof(Vector3) = " + sizeof(Vector3));//输出 sizeof(Vector3) = 12  
        }  
#endif  
    }  
}  

 本系列文章导读:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

C#Like

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值