您的位置:首页 > 编程语言 > Lua

ulua使用笔记

2016-07-04 22:58 260 查看
c#代码:

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

public class ItemC
{
public int id;
public string name;

}

public class ScriptsFromFile : MonoBehaviour {

public TextAsset scriptFile;
public Dictionary<int, ItemC> list = new Dictionary<int,ItemC>();
public LuaTable myTbl;

// Use this for initialization
void Start () {
ItemC it=new ItemC();
it.id = 2;it.name="哈哈";
list.Add(11, it);
ItemC it2 = new ItemC();
it2.id = 5; it2.name = "呜呜";
list.Add(3, it2);

LuaState l = new LuaState();
l["mlist"] = list;
l.DoString(scriptFile.text);
LuaFunction func = l.GetFunction("doMyFunc");
func.Call();
}

// Update is called once per frame
void Update () {

}

}


lua代码:

luanet.load_assembly('UnityEngine')
luanet.load_assembly("Assembly-CSharp")

GameObject = luanet.import_type('UnityEngine.GameObject')
Vector3 = luanet.import_type('UnityEngine.Vector3')
Resources = luanet.import_type('UnityEngine.Resources')
UIBasicSprite = luanet.import_type('UIBasicSprite')
TweenPosition = luanet.import_type('TweenPosition')
UITweener = luanet.import_type('UITweener')

local delegateFunc
function doMyFunc()
local go = GameObject('hello')
go.transform.localPosition = Vector3(1,2,3)
local sun = GameObject('sun')
sun.transform.parent = go.transform

local resObj = Resources.Load('FantasyAtlas')
local atlas = resObj:GetComponent('UIAtlas')

local spr = sun:AddComponent('UISprite')
spr.atlas = atlas
spr.spriteName = 'Glow'
spr.type = UIBasicSprite.Type.Sliced
spr.width = 500
local listener =makeButton(sun)
listener.onClick = onBtnClick

local tp =TweenPosition.Begin(sun,1.5,Vector3(100,100,0))
tp.style = UITweener.Style.PingPong

traverseDic(mlist)

if not checkIfContainsKey(mlist,6) then
print('不存在key值:'..6)
end

--delegateCall(saySomeThing,'haha')
delegateFunc = saySomeThing
if delegateFunc then
delegateFunc('haha')
end
end

--委托
function delegateCall(func,words)
if not func then
func(words)
end
end

function saySomeThing(words)
print('say:'..words)
end

--遍历字典
function traverseDic(dic)
itr = dic:GetEnumerator();
while itr:MoveNext() do
local cur = itr.Current
print('key:'..cur.Key..',id:'..cur.Value.id..',name:'..cur.Value.name);
end
end

--检测字典是否存在key
function checkIfContainsKey(dic,key)
if dic:ContainsKey(key) then
return true
else
return false
end
end

function makeButton(obj)
obj:AddComponent('UIButton')
local box = obj:AddComponent('BoxCollider')
box.size = Vector3(500,100,0)
local listener = obj:AddComponent('UIEventListener')
return listener
end

function onBtnClick(obj)
print('onClick:'..obj.name)
end

print("This is a script from a file 世界")


效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ulua unity