Unity热更新方案C#Like(十二-详解支持的C#特性:参数修饰符

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

简介

本篇主要介绍C#Like支持的C#特性:参数修饰符

  • C#Like免费版:不支持;
  • C#Like完整版:支持ref out in param.

C#Like免费版:

示范代码如下,提供对应方案模拟:

using UnityEngine;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
            /// 示范函数参数修饰符ref out in param  
        /// </summary>  
        void TestModifier()  
        {  
            Debug.LogError("示范函数参数修饰符: 你不能在C#Like免费版里使用函数参数修饰符'ref out in param'(在完整版已支持).强烈推荐升级到完整版:");  
            // 我们提供以下方案给免费用户:  
            // ref out in param : 各自做了模拟的使用方法  
              
            // 示范ref  
            Vector3 current = Vector3.zero;  
            Vector3 target = Vector3.one;  
            Vector3 currentVelocity = Vector3.zero;  
            Debug.Log("TestModifier ref:before:current=" + current + ",target=" + target + ",currentVelocity=" + currentVelocity);  
  
            //免费版:(需要在非热更代码里先修改)  
            SampleHowToUseModifier.currentVelocity = currentVelocity;  
            current = SampleHowToUseModifier.SmoothDamp(current, target, 0.5f);  
            currentVelocity = SampleHowToUseModifier.currentVelocity;  
            完整版:  
            //current = Vector3.SmoothDamp(current, target, ref currentVelocity, 0.5f);  
  
            Debug.Log("Test ref:after:current=" + current + ",target=" + target + ",currentVelocity=" + currentVelocity);  
  
            // 示范out  
            Dictionary<string, int> dicts = new Dictionary<string, int>();  
            dicts.Add("test", 1);  
            int iValue;  
            //免费版:(需要在非热更代码里先修改)  
            if (SampleHowToUseModifier.TryGetValue(dicts, "test"))  
            {  
                iValue = SampleHowToUseModifier.value;  
                Debug.Log("TestModifier out:iValue=" + iValue);  
            }  
            完整版:  
            //if (dicts.TryGetValue("test", out iValue))  
            //    Debug.Log("TestModifier out:iValue=" + iValue);  
  
            // 示范in  
            iValue = 100;  
            //免费版:  
            SampleHowToUseModifier.TestModifierIn("test", iValue);//You can ignore 'in' keyword 'SampleHowToUseModifier.ModifierIn("test", iValue);'  
            完整版:  
            //SampleHowToUseModifier.ModifierIn("test", in iValue);//You can ignore 'in' keyword 'SampleHowToUseModifier.ModifierIn("test", iValue);'  
  
            // 示范params  
            //免费版:(需要在非热更代码里先修改)  
            SampleHowToUseModifier.TestModifierParams("free version");  
            SampleHowToUseModifier.TestModifierParams("free version", "test");  
            SampleHowToUseModifier.TestModifierParams("free version", "test", 1);  
            SampleHowToUseModifier.TestModifierParams("free version", "test", 1, 0.5f);  
            完整版:  
            //SampleHowToUseModifier.ModifierParams("free version");  
            //SampleHowToUseModifier.ModifierParams("free version", "test");  
            //SampleHowToUseModifier.ModifierParams("free version", "test", 1);  
            //SampleHowToUseModifier.ModifierParams("free version", "test", 1, 0.5f);  
        }  
    }  
}  

对应非热更新部分的代码:

using UnityEngine;  
  
namespace CSharpLike  
{  
    public class SampleHowToUseModifier  
    {  
        #region Modifier ref  
        public static Vector3 currentVelocity = Vector3.zero;  
        public static Vector3 SmoothDamp(Vector3 current, Vector3 target, float smoothTime)  
        {  
            return Vector3.SmoothDamp(current, target, ref currentVelocity, smoothTime);  
        }  
        #endregion  
        #region Modifier out  
        public static int value = 0;  
        public static bool TryGetValue(Dictionary<string, int> dics, string key)  
        {  
            return dics.TryGetValue(key, out value);  
        }  
        #endregion  
        #region Modifier in  
        public static void TestModifierIn(string str, int iValue)  
        {  
            ModifierIn(str,in iValue);  
        }  
        public static void ModifierIn(string str, in int iValue)  
        {  
            Debug.Log("ModifierIn:"+str + iValue);  
        }  
        #endregion  
        #region Modifier params  
        public static void TestModifierParams(string str)  
        {  
            ModifierParams(str);  
        }  
        public static void TestModifierParams(string str, object v1)  
        {  
            ModifierParams(str, v1);  
        }  
        public static void TestModifierParams(string str, object v1, object v2)  
        {  
            ModifierParams(str, v1, v2);  
        }  
        public static void TestModifierParams(string str, object v1, object v2, object v3)  
        {  
            ModifierParams(str, v1, v2, v3);  
        }  
        public static void ModifierParams(string str, params object[] values)  
        {  
            string strTemp = "ModifierParams:" + str;  
            foreach (var value in values)  
                strTemp += "," + value;  
            Debug.Log(strTemp);  
        }  
        #endregion  
    }  
}  

 C#Like完整版:

示范代码如下:

using UnityEngine;  
  
namespace CSharpLike  
{  
    public partial class ExampleCSharp : LikeBehaviour  
    {  
        /// <summary>  
        /// 示范函数参数修饰符ref out in param  
        /// </summary>  
        void TestModifier()  
        {  
            Debug.LogError("Test Modifier:");  
            //示范调用热更脚本中带ref/out/in修饰符的函数  
            int v1 = 0;  
            int v2;  
            Vector2 v3 = new Vector2();  
            Vector2 v4;  
            int v5 = 5;  
            TestRefOutIn(ref v1, out v2, ref v3, out v4, v5);  
            //下面输出 v1=1,v2=16,v3=(1.0, 16.0),v4=(16.0, 1.0),v5=5  
            Debug.Log("TestModifier:TestRefOutIn:v1=" + v1 + ",v2=" + v2 + ",v3=" + v3 + ",v4=" + v4 + ",v5=" + v5);  
  
            //示范调用静态热更脚本中带ref/out/in修饰符的函数  
            v3 = new Vector2(2, 10);  
            v5 = 100;  
            TestRefOutInStatic(ref v1, out v2, ref v3, out v4, v5);  
            //下面输出 v1=1,v2=16,v3=(2.0, 112.0),v4=(16.0, 1.0),v5=100  
            Debug.Log("TestModifier:TestRefOutInStatic:v1=" + v1 + ",v2=" + v2 + ",v3=" + v3 + ",v4=" + v4 + ",v5=" + v5);  
  
            //示范调用热更脚本中带param修饰符的函数  
            Debug.Log("TestModifier:TestParams:" + TestParam("param1"));//输出 param1:  
            Debug.Log("TestModifier:TestParams:" + TestParam("param2", 1));//输出 param2:1,  
            Debug.Log("TestModifier:TestParams:" + TestParam("param3", 0, "abc"));//输出 param3:0,abc,  
            Debug.Log("TestModifier:TestParams:" + TestParam("param4", 3, "cba", 'x'));//输出 param4:3,cba,x,  
  
            //示范调用非热更脚本中带ref/out/in修饰符的函数  
            SampleNotHotUpdateScript example = new SampleNotHotUpdateScript();  
            string str1 = "Sunday";  
            string str2 = "Saturday";  
            v2 = 1;  
            example.TestRefOutIn(str1, ref str2, out v1, v2);  
            Debug.Log("TestModifier:(not hot update)TestRefOutIn:str2=" + str2 + ",v1=" + v1);//输出 str2=Sunday test,v1=11  
  
            //示范调用静态非热更脚本中带ref/out/in修饰符的函数  
            string str3 = "Monday";  
            SampleNotHotUpdateScript.TestRefOutInStatic("abc", ref str3, out v1, 123);  
            Debug.Log("TestModifier:(not hot update)TestRefOutInStatic:str2=" + str2 + ",v1=" + v1);//输出 str2=Sunday test,v1=137  
  
            //示范调用非热更脚本中带param修饰符的函数  
            Debug.Log("TestModifier:(not hot update)TestParams:" + example.TestParam());//输出 :  
            Debug.Log("TestModifier:(not hot update)TestParams:" + example.TestParam(10));//输出 :10,  
            Debug.Log("TestModifier:(not hot update)TestParams:" + example.TestParam(1, "xyz"));//输出 :1,xyz,  
            Debug.Log("TestModifier:(not hot update)TestParams:" + example.TestParam('y', "cba", 123));//输出 :y,cba,123,  
        }  
        void TestRefOutIn(ref int i, out int j, ref Vector2 k, out Vector2 l, in int m)  
        {  
            i += 1;  
            j = 10 + i + m;  
            k.x = i;  
            k.y = j;  
            l = new Vector2(j, i);  
        }  
        static void TestRefOutInStatic(ref int i, out int j, ref Vector2 k, out Vector2 l, in int m)  
        {  
            i += 1;  
            j = 10 + i + m;  
            k.x = i;  
            k.y = j;  
            l = new Vector2(j, i);  
        }  
        string TestParam(string strValue, params object[] objs)  
        {  
            string str = strValue + ":";  
            foreach (var obj in objs)  
                str += obj + ",";  
            return str;  
        }  
    }  
}  
namespace CSharpLike  
{  
    /// <summary>  
    /// 非热更脚本部分(用于热更脚本测试ref out in param)  
    /// </summary>  
    public class SampleNotHotUpdateScript  
    {  
        public static void TestRefOutInStatic(string str1, ref string str2, out int i, in int j)  
        {  
            str2 = str1 + " testStatic";  
            i = str2.Length + j;  
        }  
        public void TestRefOutIn(string str1, ref string str2, out int i, in int j)  
        {  
            str2 = str1 + " test";  
            i = str2.Length * j;  
        }  
        public string TestParam(params object[] objs)  
        {  
            string str = "";  
            foreach (var obj in objs)  
                str += obj + ",";  
            return str;  
        }  
    }  
}  

 本系列文章导读:

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

余额充值