ulua使用笔记
2016-07-04 22:58
260 查看
c#代码:
lua代码:
效果
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 世界")
效果
相关文章推荐
- unity实现多点触控代码
- 在Unity中实现动画的正反播放代码
- Unity UGUI教程之实现滑页效果
- unity实现摄像头跟随
- Unity3D上路_01-2D太空射击游戏
- Unity3D上路_02-第一视角射击游戏
- Unity3D上路_03-塔防游戏
- Unity3D上路_04-基础资源介绍
- Unity3D上路_05-网络相关
- Unity3D-5.3.5发布VR项目到Android
- unity 新UI草稿
- [软件资讯]Unity已支持将3D游戏导出成Flash
- ShaderLab: Pass
- Unity 武器拖尾效果
- unity常见问题之20题
- Unity3D动画存储插件
- unity 赛车相机跟随物体移动
- uLua中遇到的问题
- Unity3d 协程、调用函数、委托
- Unity3d摄像机Camera参数详解