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 Dictionarylua代码: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 () { }}
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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月04日 03时57分45秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
2021年不可错过的17种JS优化技巧(一)
2019-04-30
2021年不可错过的17种JS优化技巧(二)
2019-04-30
月薪15~20k的前端面试问什么?
2019-04-30
一文学会使用Vue3
2019-04-30
我是如何看Vue源码的
2019-04-30
在 Vue 中用 Axios 异步请求API
2019-04-30
LVM逻辑卷------基础命令详解(三分钟入门)
2019-04-30
LVM逻辑卷------详细操作过程(三分钟上手)
2019-04-30
mysql——介绍及安装与基本用法
2019-04-30
MySQL数据库之索引
2019-04-30
MYSQL——事务操作+视图+存储引擎
2019-04-30
Mysql——完全备份+增量备份+备份恢复
2019-04-30
MySQL进阶查询(SELECT 语句高级用法)
2019-04-30
Mysql 之主从复制
2019-04-30
LVS负载均衡------NAT模式
2019-04-30
squid代理-----透明代理模式
2019-04-30
squid代理介绍----ACL控制应用+sarg日志分析+反向代理
2019-04-30
redis集群之主从模式+哨兵模式
2019-04-30