XLua官方Examples 09_GenericMethod 泛化函数支持的演示

 

1. C# Foo

using System;
using UnityEngine;
using XLua;

namespace XLuaTest
{
    [LuaCallCSharp]
    public class Foo1Parent
    {
    }

    [LuaCallCSharp]
    public class Foo2Parent
    {
    }

    [LuaCallCSharp]
    public class Foo1Child : Foo1Parent
    {
    }

    [LuaCallCSharp]
    public class Foo2Child : Foo2Parent
    {
    }

    [LuaCallCSharp]
    public class Foo
    {
        #region Supported methods
        public void Test1<T>(T a) where T : Foo1Parent
        {
            Debug.Log(string.Format("Test1<{0}>", typeof(T)));
        }

        public T1 Test2<T1, T2>(T1 a, T2 b, GameObject c) where T1 : Foo1Parent where T2 : Foo2Parent
        {
            Debug.Log(string.Format("Test2<{0},{1}>", typeof(T1), typeof(T2)), c);
            return a;
        }
        #endregion

        #region Unsupported methods
        /// <summary>
        /// 不支持生成lua的泛型方法(没有泛型约束)
        /// </summary>
        public void UnsupportedMethod1<T>(T a)
        {
            Debug.Log("UnsupportedMethod1");
        }

        /// <summary>
        /// 不支持生成lua的泛型方法(缺少带约束的泛型参数)
        /// </summary>
        public void UnsupportedMethod2<T>() where T : Foo1Parent
        {
            Debug.Log(string.Format("UnsupportedMethod2<{0}>", typeof(T)));
        }

        /// <summary>
        /// 不支持生成lua的泛型方法(泛型约束必须为class)
        /// </summary>
        public void UnsupportedMethod3<T>(T a) where T : IDisposable
        {
            Debug.Log(string.Format("UnsupportedMethod3<{0}>", typeof(T)));
        }
        #endregion
    }

    [LuaCallCSharp]
    public static class FooExtension
    {
        public static void PlainExtension(this Foo1Parent a)
        {
            Debug.Log("PlainExtension");
        }

        public static T Extension1<T>(this T a) where T : Foo1Parent
        {
            Debug.Log(string.Format("Extension1<{0}>", typeof(T)));
            return a;
        }

        public static T Extension2<T>(this T a, GameObject b) where T : Foo1Parent
        {
            Debug.Log(string.Format("Extension2<{0}>", typeof(T)), b);
            return a;
        }

        public static void Extension2<T1, T2>(this T1 a, T2 b) where T1 : Foo1Parent where T2 : Foo2Parent
        {
            Debug.Log(string.Format("Extension2<{0},{1}>", typeof(T1), typeof(T2)));
        }

        public static T UnsupportedExtension<T>(this GameObject obj) where T : Component
        {
            return obj.GetComponent<T>();
        }
    }
}

2.C# GenericMethodExample

using UnityEngine;
using XLua;

namespace XLuaTest
{
    public class GenericMethodExample : MonoBehaviour
    {
        private const string script = @"
        local foo1 = CS.XLuaTest.Foo1Child()
        local foo2 = CS.XLuaTest.Foo2Child()

        local obj = CS.UnityEngine.GameObject()
        foo1:PlainExtension()
        foo1:Extension1()
        foo1:Extension2(obj) -- overload1
        foo1:Extension2(foo2) -- overload2
        
        local foo = CS.XLuaTest.Foo()
        foo:Test1(foo1)
        foo:Test2(foo1,foo2,obj)
";
        private LuaEnv env;

        private void Start()
        {
            env = new LuaEnv();
            env.DoString(script);
        }

        private void Update()
        {
            if (env != null)
                env.Tick();
        }

        private void OnDestroy()
        {
            env.Dispose();
        }
    }
}

3.调试

挂载到场景上运行即可

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
 热更新框架设计系列课程总体介绍:        本系列课程由《热更新框架设计之Xlua基础》、《热更新框架设计之热更流程与热补丁技术》、《热更新框架设计之游戏客户端框架》三套课程组成。 三套课程是一个不可分割有机的整体,笔者带领大家由浅入深逐级深入 ,在领悟热更精髓的基础之上,通过高端架构设计,设计出“低耦合”、“低侵入”、“高复用”性的游戏(VR/AR)客户端热更框架。                           温馨提示:            本套课程需要具备一定的框架理解与驾驭能力,为了更好的理解本作,强烈推荐广大学员首先学完必要的前导课程:“UI客户端框架设计”、“AssetBundle 框架设计”、“lua基础与中级篇”。《热更新框架设计之Xlua基础》课程介绍:       本课程为系列课程的xlua基础部分,本课程主要就xlua的优势、特点、环境搭建、以及lua文件加载、C#调用lua的各种方式、lua调用C#的各种技巧展开讨论与讲解,学后基本掌握xlua中常用的技术基础,为后续课程打下坚实的基础。 热更新系列(技术含量:中高级):A:《lua热更新技术中级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118C:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132D:《热更新框架设计之客户端热更框架(中)》https://edu.csdn.net/course/detail/27135E:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jack Yan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值