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