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
}
}
本系列文章导读:
- 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(廿六)-(可选)详解免费版的演示如何升级到完整版的演示