Unity热更新方案C#Like(九)-详解支持的C#特性:Using和命名空间

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

简介

本篇主要介绍C#Like支持的C#特性:Using和命名空间.

  • C#Like免费版:完整的命名空间功能,无需事先注册类型就能直接使用,使用方便;using指令/别名/static; 
  • C#Like完整版:多了using语句
  • C#Light:没有命名空间的功能,在热更新脚本里的所有用到的类型,必须在非热更新脚本里先注册才能在热更新脚本里使用,如果后面突然想要用哪个类型,是无法使用的.

C#Like免费版:

示范代码如下:

using UnityEngine;//测试using指令  
using Random = UnityEngine.Random;//测试using别名(非热更新代码的)  
using System.IO;  
using CSharpLike.Subclass;  
using CSharpLike.SubclassEx;  
using Mammals = CSharpLike.SubclassEx.Mammals;//测试using别名(热更新代码的)  
using System;  
using static CSharpLike.JSONData;//测试using静态(非热更新代码的,类或枚举或结构体里的)  
  
namespace CSharpLike  
{  
    public partial class SampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 4种using用法:  
        /// using 指令,  
        /// using 别名(same class name with different namespace),  
        /// using 语句,  
        /// using 静态(类或枚举或结构体里的)  
        /// </summary>  
        void TestUsingAndNamespace()  
        {  
            Debug.LogError("Test Using and Namespace: 免费版不支持'using 语句'. (完整版里支持的). 强烈建议升级到完整版:");  
            // 我们提供一个解决方案给免费版:  
            // 手动调用.Dispose(), 当然,由于免费版不支持try-catch-finally,无法保证一定能执行得到哦.  
  
            //这里测试不同命名空间下的相同类名的(非热更新代码的),例如System.Random和UnityEngine.Random  
            Debug.Log("test using alias: random number = " + Random.Range(0, 1000) + " at " + DateTime.Now);  
            Debug.LogError("Test Namespace:");  
            //这里测试不同命名空间下的相同类名的(热更新代码的),例如CSharpLike.SampleCSharp.SubclassEx.Mammals和CSharpLike.SampleCSharp.SubclassEx2.Mammals  
            Mammals mammals = new Mammals();  
            Debug.Log("test Namespace: mammals=" + mammals.TestNameSpace());  
            Debug.Log("test Namespace: Toys=" + (new Toys("rabbit")).name);  
            //不使用using,直接使用带命名空间的完整名字  
            Debug.Log("test Namespace: direct use full namespace for hot update script: " + SubclassEx2.TestNamespace.GetTestString());  
            Debug.Log("test Namespace: direct use full namespace for not hot update script: " + System.Text.Encoding.UTF8.GetBytes("test string"));  
  
            //测试using静态(非热更新代码里的,类或枚举或结构体里的)  
            JSONData jsonData = 1;  
            //DataType是CSharpLike.JSONData内的枚举, 我们必须在头部设置'using static CSharpLike.JSONData;'.  
            //你不能直接使用完整名'CSharpLike.JSONData.DataType.DataTypeList'代替'DataType.DataTypeList', 否则会编译报错.  
            //因为typeof(CSharpLike.JSONData.DataType)的完整名是'CSharpLike.JSONData+DataType', 不是'CSharpLike.JSONData.DataType'.  
            //但是我们支持热更新代码里的完整名  
            if (jsonData.dataType != DataType.DataTypeList)  
                Debug.Log("test jsonData.dataType=" + jsonData.dataType);//输出 DataTypeInt  
  
        }  
    }  
} 

 C#Like完整版:

示范代码如下:

using UnityEngine;//测试using指令  
using Random = UnityEngine.Random;//测试using别名(非热更新代码的)  
using System.IO;  
using CSharpLike.Subclass;  
using CSharpLike.SubclassEx;  
using Mammals = CSharpLike.SubclassEx.Mammals;//测试using别名(热更新代码的)  
using System;  
using static CSharpLike.JSONData;//测试using静态(非热更新代码的,类或枚举或结构体里的)  
  
namespace CSharpLike  
{  
    public partial class SampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 4种using用法:  
        /// using 指令,  
        /// using 别名(same class name with different namespace),  
        /// using 语句,  
        /// using 静态(类或枚举或结构体里的)  
        /// </summary>  
        void TestUsingAndNamespace()  
        {  
            Debug.LogError("Test Using:");  
            //这里测试不同命名空间下的相同类名的(非热更新代码的),例如System.Random和UnityEngine.Random  
            Debug.Log("test using alias: random number = " + Random.Range(0, 1000) + " at " + DateTime.Now);  
            Debug.Log("test using sentence: old style");  
            byte[] buff = System.Text.Encoding.UTF8.GetBytes("This's a string for test using");  
            using (MemoryStream ms = new MemoryStream(buff))//旧方式的using语句  
            {  
                byte[] buff2 = ms.ToArray();  
                Debug.Log(System.Text.Encoding.UTF8.GetString(buff2));  
            }//离开该区块的时候回自动调用ms.Dispose()  
            {  
                Debug.Log("test using sentence: new style");  
                buff = System.Text.Encoding.UTF8.GetBytes("This's a string for test using 2");  
                using MemoryStream ms2 = new MemoryStream(buff);//新方式的using语句  
                byte[] buff2 = ms2.ToArray();  
                Debug.Log(System.Text.Encoding.UTF8.GetString(buff2));  
            }//离开该区块的时候回自动调用ms2.Dispose()  
            Debug.LogError("Test Namespace:");  
            //这里测试不同命名空间下的相同类名的(热更新代码的),例如CSharpLike.SampleCSharp.SubclassEx.Mammals和CSharpLike.SampleCSharp.SubclassEx2.Mammals  
            Mammals mammals = new Mammals();  
            Debug.Log("test Namespace: mammals=" + mammals.TestNameSpace());  
            Animal nobody = new Human("nobody");  
            Debug.Log("test Namespace: nobody=" + nobody.GetInfo());  
            Debug.Log("test Namespace: Toys=" + (new Toys("rabbit")).name);  
            //不使用using,直接使用带命名空间的完整名字  
            Debug.Log("test Namespace: direct use full namespace for hot update script: " + SubclassEx2.TestNamespace.GetTestString());  
            Debug.Log("test Namespace: direct use full namespace for not hot update script: " + System.Text.Encoding.UTF8.GetBytes("test string"));  
  
            //测试using静态(非热更新代码里的,类或枚举或结构体里的)  
            JSONData jsonData = 1;  
            //DataType是CSharpLike.JSONData内的枚举, 我们必须在头部设置'using static CSharpLike.JSONData;'.  
            //你不能直接使用完整名'CSharpLike.JSONData.DataType.DataTypeList'代替'DataType.DataTypeList', 否则会编译报错.  
            //因为typeof(CSharpLike.JSONData.DataType)的完整名是'CSharpLike.JSONData+DataType', 不是'CSharpLike.JSONData.DataType'.  
            //但是我们支持热更新代码里的完整名  
            if (jsonData.dataType != DataType.DataTypeList)  
                Debug.Log("test jsonData.dataType=" + jsonData.dataType);//输出 DataTypeInt  
  
        }  
    }  
}

 本系列文章导读:

  • 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、付费专栏及课程。

余额充值