L u a

该文展示了Lua脚本在Unity3D中的应用,包括闭包、迭代器的使用,C#命名空间下的对象操作,如查找游戏物体、加载资源和动态添加组件。同时,还涉及了字符串处理函数、事件监听以及用户输入处理的代码示例。
摘要由CSDN通过智能技术生成

--闭包 (static)
--[[
function Get()
    a=1
    return function()
        a=a+1
        print(a)
    end
end
GetValue=Get()
GetValue()
GetValue()


function Get(n)
    local function Get1()
        print(n)
    end
    local function Get2()
        n=n+10
    end
    return Get1,Get2
end
Get1,Get2=Get(2023)
Get1()
Get2()
Get1()

--迭代器
function IE(array)
    i=0
    return function()
        i=i+1
        return array[i]
    end
end
array={1,2,3,4,5}
Get=IE(array)
for i=1,5 do
    print(Get())
end
]]--


table={}
function Split(str)--字符串截取
    local s1,s2="",""
    for i=1,string.len(str) do
        if    string.sub(str,i,i)=="@" then
            s1=string.sub(str,1,i-1)
            s2=string.sub(str,i+1,#str)
            return s1,s2
        end
    end
end
a="sa@as"
s1,s2= Split(a)
--print(s1.."\t"..s2)
table[0]=a

for    i=0,#table do
    s1,s2=Split(table[i])
    if    s1=="sad" then
        print("HasRegister")
    else
        print("RegisterSuccessfully")
    end
    --print(s1.."\t"..s2)
end
 

--C#命名空间
l=CS.UnityEngine
--创建空物体
GameObject = CS.UnityEngine.GameObject("Lua")
--查找游戏物体
--Camera=CS.UnityEngine.GameObject.Find("Main Camera")
Camera=CS.UnityEngine.GameObject.FindWithTag("MainCamera")
if Camera ~=nil then
    print("Found")
else
    print("Not Found")
end
--动态加载资源
sphere=l.Resources.Load("Sphere")
--克隆游戏物体
sphereClone=l.GameObject.Instantiate(sphere)
--[[
While Camera ~= nil do
    if l.Input.GetMouseButtonDown(0) then
        print("111")
    end
end
]]--
--实例化对象
lua=CS._3_29()--若类继承MonoBehaviour则不能使用new 创建实例对象
--调用方法(普通方法:调用,静态方法.调用)
lua:Get()
CS.Test.Get2()
--添加组件
sphereClone:AddComponent(typeof(l.Rigidbody))
sphereClone:AddComponent(typeof(l.AI.NavMeshAgent))--PS:命名空间
--访问枚举值
print(CS.Week.Monday)
--按钮监听事件
--[[
function Init()
    btn=CS.UnityEngine.GameObject.Find("Button (Legacy)"):GetComponent(typeof(l.UI.Button))
    --btn.onClick:AddListener(Click)
    btn.onClick:AddListener(
    function()
        print(inputfield.text)
    end
    )
    inputfield=l.GameObject.Find("InputField (Legacy)"):GetComponent(typeof(l.UI.InputField))
end
function Click()
    print(inputfield.text)
end
]]--
--HomeWork
--拓展
array={}
function Split(str)
    local s1,s2="",""
    for i=0,string.len(str) do
        if    string.sub(str,i,i)=="@" then
            s1=string.sub(str,1,i-1)
            s2=string.sub(str,i+1,#str)
            return s1,s2
        end
    end
end
--初始化
function Init()
    username=l.GameObject.Find("UserName"):GetComponent(typeof(l.UI.InputField))
    password=l.GameObject.Find("Password"):GetComponent(typeof(l.UI.InputField))
    register=l.GameObject.Find("Register"):GetComponent(typeof(l.UI.Button))
    login=l.GameObject.Find("Login"):GetComponent(typeof(l.UI.Button))
    register.onClick:AddListener(Register)
    login.onClick:AddListener(Login)
end
--注册
function Register()
    --username=l.GameObject.Find("UserName"):GetComponent(typeof(l.UI.InputField))
    --password=l.GameObject.Find("Password"):GetComponent(typeof(l.UI.InputField))
    if    #array>0 then
        for    i=0,#array do
            print(array[i])
            s1,s2=Split(array[i])
            if    s1==username.text then
                print("the Account has Register")
            else
                table.insert (array,username.text.."@"..password.text)
                --array[#array]=username.text.."@"..password.text
                print("RegisterSuccessfully")
            end
        end
    else
        table.insert (array,username.text.."@"..password.text)
        --array[#array]=username.text.."@"..password.text
        print(#array)
    end    
end
--登录
function Login()
    --username=l.GameObject.Find("UserName"):GetComponent(typeof(l.UI.InputField))
    --password=l.GameObject.Find("Password"):GetComponent(typeof(l.UI.InputField))
    for    i=0,#array do
        --print(array[i])
        if array[i] ~=nil then
            s1,s2=Split(array[i])
            if    s1==username.text and s2==password.text then
                print("LoginSuccessfully")
            else
                print("LoginFailed")
            end
        end
    end

end

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
/*
*创建者:
*创建时间:
*描述:
*版本:
*/
public enum Week 
{
    Monday,
    Sunday
}
[CSharpCallLua]//C#调用Lua特性
public class _3_29 : MonoBehaviour
{
    public delegate void Init();//Lua不能直接添加事件监听,将lua中的方法注册进委托进行初始化
    // Start is called before the first frame update
    void Start()
    {
        
        LuaEnv le = new LuaEnv();//lua环境
        le.DoString("require('HomeWork')");

        Init init = le.Global.Get<Init>("Init");
        init();
        //le.Dispose();
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
    public void Get()
    {
        print("Get()");
    }
    public static void Get2()
    {
        print("Get2()");
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值