Unity热更新方案C#Like(六)-详解支持的C#特性:循环语法

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());  
        } 
    }  
}

 本系列文章导读:

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

余额充值