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
}
}
}
本系列文章导读:
- Unity热更新方案C#Like(一)-序言
- Unity热更新方案C#Like(二)-导出官方示范的例子,确认方案可行性
- Unity热更新方案C#Like(三)-详解支持的C#特性:类
- Unity热更新方案C#Like(四)-详解支持的C#特性:委托和Lambda
- Unity热更新方案C#Like(五)-详解支持的C#特性:运算表达式
- Unity热更新方案C#Like(六)-详解支持的C#特性:循环语法
- Unity热更新方案C#Like(七)-详解支持的C#特性:get/set访问器
- Unity热更新方案C#Like(八)-详解支持的C#特性:多线程
- Unity热更新方案C#Like(九)-详解支持的C#特性:Using和命名空间
- Unity热更新方案C#Like(十)-详解支持的C#特性:宏和区域
- Unity热更新方案C#Like(十一)-详解支持的C#特性:枚举
- Unity热更新方案C#Like(十二-详解支持的C#特性:参数修饰符
- Unity热更新方案C#Like(十三)-详解支持的C#特性:函数重载和默认参数
- Unity热更新方案C#Like(十四)-详解支持的C#特性:异常处理
- Unity热更新方案C#Like(十五)-详解支持的C#特性:关键字:unsafe typeof nameof $ @ #pragma #warning #error
- Unity热更新方案C#Like(十六)-详解支持的C#特性:其他杂项:初始值设定项,表达式主体,内联变量声明
- Unity热更新方案C#Like(十七)-详解支持的长链接Socket和WebSocket
- Unity热更新方案C#Like(十八)-详解如何和Unity交互
- Unity热更新方案C#Like(十九)-详解KissJSON:唯一可以在本热更新框架使用的JSON库
- Unity热更新方案C#Like(二十)-详解KissCSV:一个简易实用的CSV表格读取方式
- Unity热更新方案C#Like(廿一)-详解KissFrameworkServer:对应的示范例子和官网所用的服务器框架
- Unity热更新方案C#Like(廿二)-详解内置的例子C#Like Demo:飞机大战,简易聊天室,简易账号/物品/邮件系统
- Unity热更新方案C#Like(廿三)-实战:示范如何把Unity官方免费例子Tanks! Tutorial转成可热更新项目
- Unity热更新方案C#Like(廿四)-实战:示范如何把Unity官方免费例子Platformer Microgame转成可热更新项目
- Unity热更新方案C#Like(廿五)-实战:示范如何建立初始包CSharpLikeFreeDemo项目
- Unity热更新方案C#Like(廿六)-(可选)详解免费版的演示如何升级到完整版的演示