C#Like是Unity的热更方案,使用纯C#语言写出可以热更新的代码,就像可以在所有平台使用DLL(动态链接库)文件一样.遵从KISS设计原则,让用户轻松构建或升级成Unity的热更新项目.
简介
本篇主要介绍C#Like支持的C#特性:循环语句.
- C#Like免费版:for foreach continue break if-else return while do-while
- C#Like完整版:多了switch-case-default yield-return yield-break
- 两者都不支持goto语法
C#Like免费版:
示范代码如下:
using System;
using System.Collections.Generic;
using UnityEngine;
namespace CSharpLike
{
public partial class ExampleCSharp : LikeBehaviour
{
/// <summary>
/// 示范循环语句
/// for foreach continue break if-else return while do-while switch-case-default.
/// </summary>
void TestLoop()
{
Debug.LogError("示范循环语句:你不能在C#Like免费版里使用协程switch-case-default.(在完整版已支持).强烈推荐升级到完整版: ";
// 我们提供以下方案给免费用户:
// switch-case-default: 直接使用"if-else"语句代替 (分支较多情况下看起来难看点和效率稍微低点).
int x = 1;
int y = 3;
int z = 100;
//示范 if-else
if (x < y)
Debug.Log("test 'if-else': enter 'if'");
else if (x < z)
Debug.Log("test 'if-else': enter 'else if'");
else
Debug.Log("test 'if-else': enter 'else'");
List<Vector3> lists = new List<Vector3>();// this generic type should be AOT
lists.Add(Vector3.zero);
lists.Add(Vector3.one);
//示范 for
for (int i = 0; i<lists.Count; i++)
{
if (i == 2)
{
Debug.Log("test 'continue':");
continue;
}
Debug.Log("test 'for': lists[" + i + "] = " + lists[i]);
}
//示范 foreach
foreach(var item in lists)
{
Debug.Log("test 'foreach': item = " + item);
if (item == 2)
{
Debug.Log("test 'break':");
break;
}
}
//示范 while
while(x < y)
{
x++;
Debug.Log("test 'while': x = " + x + ", y = " + y);
}
//示范 do-while
x = 1;
do
{
x++;
Debug.Log("test 'do-while': x = " + x + ", y = " + y);
} while (x < y) ;
//示范 switch-case-default(模仿)
//免费版下:
if (x == 0)
Debug.Log("test 'switch': enter 0");
else if (x == 1)
Debug.Log("test 'switch': enter 1");
else if (x == 2 || x == 3)
Debug.Log("test 'switch': enter 2 or 3");
else
Debug.Log("test 'switch': enter default");
//完整版下:
//switch(x)
//{
// case 0:
// Debug.Log("test 'switch': enter 0");
// break;
// case 1:
// Debug.Log("test 'switch': enter 1");
// break;
// case 2:
// case 3:
// Debug.Log("test 'switch': enter 2 or 3");
// break;
// default:
// Debug.Log("test 'switch': enter default");
// break;
//}
}
}
}
C#Like完整版:
示范代码如下:
using System;
using System.Collections.Generic;
using UnityEngine;
namespace CSharpLike
{
public partial class ExampleCSharp : LikeBehaviour
{
/// <summary>
/// 示范循环语句
/// for foreach continue break if-else return while do-while switch-case-default.
/// </summary>
void TestLoop()
{
Debug.LogError("Test Loop:");
int x = 1;
int y = 3;
int z = 100;
//示范 if-else
if (x < y)
Debug.Log("test 'if-else': enter 'if'");
else if (x < z)
Debug.Log("test 'if-else': enter 'else if'");
else
Debug.Log("test 'if-else': enter 'else'");
List<int> ints = new List<int>();
ints.Add(1);
ints.Add(2);
ints.Add(3);
//示范 for
for (int i = 0; i<ints.Count; i++)
{
if (i == 2)
{
Debug.Log("test 'continue':");
continue;
}
Debug.Log("test 'for': ints[" + i + "] = " + ints[i]);
}
//示范 foreach
foreach(var item in ints)
{
Debug.Log("test 'foreach': item = " + item);
if (item == 2)
{
Debug.Log("test 'break':");
break;
}
}
//示范 while
while(x < y)
{
x++;
Debug.Log("test 'while': x = " + x + ", y = " + y);
}
//示范 do-while
x = 1;
do
{
x++;
Debug.Log("test 'do-while': x = " + x + ", y = " + y);
} while (x < y) ;
//示范 switch-case-default
switch(DateTime.Now.DayOfWeek)
{
case DayOfWeek.Monday:
Debug.Log("test 'switch-case-default': sad because have to work.");
break;
case DayOfWeek.Friday:
Debug.Log("test 'switch-case-default': happy because rest day is coming!");
break;
case DayOfWeek.Saturday:
case DayOfWeek.Sunday:
Debug.Log("test 'switch-case-default': exciting because had fun in rest day.");
break;
default:
Debug.Log("test 'switch-case-default': work hard.Fighting!");
break;
}
/*只支持'StartCoroutine(string methodName, params object[] vars)'的方式
注意:不支持StartCoroutine(SubCoroutine("test string", 123, -1f))写法
这里示范调起带3个参数的子协程*/
StartCoroutine("SubCoroutine", "test string", 123, -1f);
}
/// <summary>
/// 示范协程
/// </summary>
IEnumerator SubCoroutine(string strValue, int iValue, float fValue)
{
Debug.Log("SubCoroutine(" + strValue + "," + iValue + "," + fValue + ")");
yield return new WaitForSeconds(2f);
if (fValue > 0f)
yield break;
else
{
//示范可空类型'?' 合并运算 '?.' 和 '??'
System.Random r = null;//new System.Random();
int? i = r?.Next(1000);
Debug.Log("SubCoroutine:i=" + (i ?? iValue));
}
yield return new WaitForEndOfFrame();
Debug.Log("SubCoroutine:end:" + DateTime.Now.ToString());
}
}
}
本系列文章导读:
- 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(廿六)-(可选)详解免费版的演示如何升级到完整版的演示