ulua使用笔记

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 世界")

效果



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林新发

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

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

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

打赏作者

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

抵扣说明:

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

余额充值