Unity XLua(七)lua调用C#带可变参数方法+结构体参数方法+接口参数方法+委托参数方法

20 篇文章 3 订阅

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
using System.Text;
using UnityEngine.UI;

public class TestXLua : MonoBehaviour
{
    LuaEnv lua;

    void Start()
    {
        lua = new LuaEnv();

        //加载Lua
        lua.AddLoader(LoadLua);
        lua.DoString("require 'TestLua'");
    }

    byte[] LoadLua(ref string file)
    {
        Debug.Log(file);
        string slua = File.ReadAllText(Application.dataPath + "/Lua/" + file + ".lua");
        byte[] bytes = new UTF8Encoding().GetBytes(slua);
        
        return bytes;
    }

    void OnDestroy()
    {
        lua.Dispose();
    }
}

可变参数

C# 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player
{
    public bool PFunction(int a,float b, params string[]srtArr) 
    {
        Debug.Log("PFunction:" + a);
        Debug.Log("PFunction:" + b);
        foreach (string item in srtArr)
        {
            Debug.Log(item);
        }

        return true;
    }
}

 Lua

player = CS.Player()

bool = player:PFunction(2,3.6,"aa","bb","cc")

print(bool)

结构体参数

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public struct TheStruct
{
    public int i;
    public string s;
}
public class Player
{
    public void PFunction(TheStruct theStruct) 
    {
        Debug.Log(theStruct.i);
        Debug.Log(theStruct.s);
    }
}
player = CS.Player()
structTable = {i = 1,s = "a"}
player:PFunction(structTable)

 

 接口参数

C# 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

[CSharpCallLua]
public interface TheInterface
{
    int i { get; set; }
    string s { get; set; }
    void Play();
}

public class Player
{
    public void PFunction(TheInterface theInterface) 
    {
        Debug.Log(theInterface.i);
        Debug.Log(theInterface.s);
        theInterface.Play();
    }
}

Lua

player = CS.Player()

TheInterface = {
	i = 1,
	s = "a",
	Play = function ()
		print("PLAY")
	end
}

player:PFunction(TheInterface)

委托参数

C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;

[CSharpCallLua]
public delegate void TheDelegate(int i);

public class Player
{
    public void PFunction(TheDelegate theInterface) 
    {
        Debug.Log("DELEGATE");
        theInterface.Invoke(26);
    }
}

 Lua

player = CS.Player()

Play = function (i)
		print("PLAY"..i)
	end

player:PFunction(Play)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程总体目标:     本中级篇面向的学员不再是完全的编程“小白”,而是具备一定C#编程经验,需要进一步查漏补缺、或者需要进一步全面完善自己C#编程知识体系的广大Unity学员。相信通过本中级篇的学习,可以使得Unity初中级开发人员对于编程语言的掌握更进一步;对于开发中大型游戏项目,在编程语言这一层级进一步打下坚实的语言基础。 “中级篇”课程讲解特点:       本中级篇面向初中级游戏研发人员,以及Unity中高级学习者。为了更加深入的刨析各个语法的本质,我们采用反编译解读IL中间语言的方式,来解构语法难点,使得学员最短时间掌握语法本质。 本课程讲解内容:       C#(for Unity)中级篇 在“C#入门”、“基础篇”的基础之上,从以下四个方面着重研究我们游戏开发(包含软件开发)过程中,C#最重要、最实用的技能模块,使得广大游戏研发人员,对于C#这门Unity脚本有进一步更加完善的认识。一:.Net 框架讲解。    A) .Net 发展历史。    B)  IL  中间语言。 CLR  公共语言运行时。    C) 多维数据(常用二维数组)与交错数组。    D) 可变参数 Params    E) 进一步解释“实参”,“形参”。    F) 类的实例化内存分配机制。二:深入学习对象类型    A)  里氏替换原则(LSP)    B)  类的属性极其本质特性    C)  IS ,AS 关键字    D)  深入学习字符串理论        1] 字符串的“驻留性” 原理。        2] 字符串==与Equals() 的本质区别        3] 更多字符串常用方法学习。    E)  枚举类型以及适用场合。三:深入学习集合特性    A)  什么是索引器,以及索引器的适用范围。    B)  学习自定义集合类,以及深入了解Foreach 语句的原理。    C)  深入学习 ArrayList,了解内部存储机制以及原理。    D)  深入学习 HashTable,了解内部存储机制以及原理。    E)  为什么学习泛型集合?    F)  泛型集合与普通集合的性能测试对比实验。    G)  学习“泛型约束”,以及“泛型约束”的适用条件。四:委托与事件        A)  什么是委托,先从讲故事学习起:“老板来啦”!    B)  反编译掌握委托的本质。    C)  委托的四大开发步骤。    D)  什么是事件,以及委托与事件的区别。    E)  事件的常用使用方式。 温馨提示:       本C# for Unity 使用Virtual Studio2012,进行开发与讲解。(学员使用更高版本,对学习没有任何影响) 一、热更新系列(技术含量:中高级):A:《lua热更新技术中级篇》https://edu.csdn.net/course/detail/27087B:《热更新框架设计之Xlua基础视频课程》https://edu.csdn.net/course/detail/27110C:《热更新框架设计之热更流程与热补丁技术》https://edu.csdn.net/course/detail/27118D:《热更新框架设计之客户端热更框架(上)》https://edu.csdn.net/course/detail/27132E:《热更新框架设计之客户端热更框架(中)》https://edu.csdn.net/course/detail/27135F:《热更新框架设计之客户端热更框架(下)》https://edu.csdn.net/course/detail/27136二:框架设计系列(技术含量:中级): A:《游戏UI界面框架设计系列视频课程》https://edu.csdn.net/course/detail/27142B:《Unity客户端框架设计PureMVC篇视频课程(上)》https://edu.csdn.net/course/detail/27172C:《Unity客户端框架设计PureMVC篇视频课程(下)》https://edu.csdn.net/course/detail/27173D:《AssetBundle框架设计_框架篇视频课程》https://edu.csdn.net/course/detail/27169三、Unity脚本从入门到精通(技术含量:初级)A:《C# For Unity系列之入门篇》https://edu.csdn.net/course/detail/4560B:《C# For Unity系列之基础篇》https://edu.csdn.net/course/detail/4595C: 《C# For Unity系列之中级篇》https://edu.csdn.net/course/detail/24422D:《C# For Unity系列之进阶篇》https://edu.csdn.net/course/detail/24465四、虚拟现实(VR)与增强现实(AR):(技术含量:初级)A:《虚拟现实之汽车仿真模拟系统 》https://edu.csdn.net/course/detail/26618五、Unity基础课程系列(技术含量:初级) A:《台球游戏与FlappyBirds—Unity快速入门系列视频课程(第1部)》 https://edu.csdn.net/course/detail/24643B:《太空射击与移动端发布技术-Unity快速入门系列视频课程(第2部)》https://edu.csdn.net/course/detail/24645 C:《Unity ECS(二) 小试牛刀》https://edu.csdn.net/course/detail/27096六、Unity ARPG课程(技术含量:初中级):A:《MMOARPG地下守护神_单机版实战视频课程(上部)》https://edu.csdn.net/course/detail/24965B:《MMOARPG地下守护神_单机版实战视频课程(中部)》https://edu.csdn.net/course/detail/24968C:《MMOARPG地下守护神_单机版实战视频课程(下部)》https://edu.csdn.net/course/detail/24979

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值