ulua使用笔记
发布日期:2021-06-30 19:38:39 浏览次数:2 分类:技术文章

本文共 2690 字,大约阅读时间需要 8 分钟。

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
list = new Dictionary
(); 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 delegateFuncfunction 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')	endend--委托function delegateCall(func,words)	if not func then		func(words)	endendfunction 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);	endend--检测字典是否存在keyfunction checkIfContainsKey(dic,key)	if dic:ContainsKey(key) then		return true	else		return false		endendfunction makeButton(obj)	obj:AddComponent('UIButton')	local box = obj:AddComponent('BoxCollider')	box.size = Vector3(500,100,0)	local listener = obj:AddComponent('UIEventListener')	return listenerendfunction onBtnClick(obj)	print('onClick:'..obj.name)endprint("This is a script from a file 世界")

效果

转载地址:https://linxinfa.blog.csdn.net/article/details/51824500 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:unity自制延迟定时回调
下一篇:ulua与unity互传数组

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月04日 03时57分45秒