Unity tolua 常用方法

1.Json操作

local cjson = require "cjson"

//解析json
local sampleJson = '{"name":"abc","age":"23","obj":{"array":[1,2,3]}}';
local data = cjson.decode(sampleJson);
print('name=' .. data["name"]);
print('array[1]=' .. data["obj"]["array"][1]);

//创建json
local tjson = {};
tjson["abc"] = 1;
print("jsondata:" .. cjson.encode(tjson));


2.字符串操作

-- 字符串分割
-- 参数str是你的字符串,比如"a|b|c"
-- 参数sep是分隔符,比如"|"
-- 返回值为["a","b","c"]
function SplitString(str, sep)
    local sep = sep or " "
    local result = {};
    local pattern = string.format("([^%s]+)", sep)
    local rs = string.gsub(str, pattern, function(c) result[#result + 1] = c end);
    return result
end

--字符串替换操作
> string.gsub("aaaa","a","z",3);
zzza    3

-- 等等其他用法

3.table操作

序号方法 & 用途
1table.concat (table [, sep [, start [, end]]]):

concat是concatenate(连锁, 连接)的缩写. table.concat()函数列出参数中指定table的数组部分从start位置到end位置的所有元素, 元素间以指定的分隔符(sep)隔开。

2table.insert (table, [pos,] value):

在table的数组部分指定位置(pos)插入值为value的一个元素. pos参数可选, 默认为数组部分末尾.

3table.maxn (table)

指定table中所有正数key值中最大的key值. 如果不存在key值为正数的元素, 则返回0。(Lua5.2之后该方法已经不存在了,本文使用了自定义函数实现)

4table.remove (table [, pos])

返回table数组部分位于pos位置的元素. 其后的元素会被前移. pos参数可选, 默认为table长度, 即从最后一个元素删起。

5table.sort (table [, comp])

对给定的table进行升序排序。

fruits = {"banana","orange","apple"}
-- 返回 table 连接后的字符串
print("连接后的字符串 ",table.concat(fruits))

-- 指定连接字符
print("连接后的字符串 ",table.concat(fruits,", "))

-- 指定索引来连接 table
print("连接后的字符串 ",table.concat(fruits,", ", 2,3))


连接后的字符串     bananaorangeapple
连接后的字符串     banana, orange, apple
连接后的字符串     orange, apple


-- table最大值
function table_maxn(t)
  local mn=nil;
  for k, v in pairs(t) do
    if(mn==nil) then
      mn=v
    end
    if mn < v then
      mn = v
    end
  end
  return mn
end
tbl = {[1] = 2, [2] = 6, [3] = 34, [26] =5}
print("tbl 最大值:", table_maxn(tbl))
print("tbl 长度 ", #tbl)
-- 结果
tbl 最大值:    34
tbl 长度     3

--当我们获取 table 的长度的时候无论是使用 # 还是 table.getn 其都会在索引中断的地方停止计数,而导致无法正确取得 table 的长度。

--可以使用以下方法来代替:
function table_leng(t)
  local leng=0
  for k, v in pairs(t) do
    leng=leng+1
  end
  return leng;
end

4.创建lua类

__newindex 元方法用来对表更新,__index则用来对表访问 。

--声明,这里声明了类名还有属性,并且给出了属性的初始值。
LuaClass = {x = 0, y = 0}

--这句是重定义元表的索引,就是说有了这句,这个才是一个类。
LuaClass.__index = LuaClass

--构造体,构造体的名字是随便起的,习惯性改为New()
function LuaClass:New(x, y) 
    local self = {};    --初始化self,如果没有这句,那么类所建立的对象改变,其他对象都会改变
    setmetatable(self, LuaClass);  --将self的元表设定为Class
    self.x = x;
    self.y = y;
    return self;    --返回自身
end

--测试打印方法--
function LuaClass:test() 
    logWarn("x:>" .. self.x .. " y:>" .. self.y);
end

--创建LuaClass 对象
 local lc1 = LuaClass:New(1, 2);
 lc1:test();
 local lc2 = LuaClass:New(3, 4);
 lc2:test();

__newindex元方法:

mymetatable = {}
mytable = setmetatable({key1 = "value1"}, { __newindex = mymetatable })

print(mytable.key1)

mytable.newkey = "新值2"
print(mytable.newkey,mymetatable.newkey)

mytable.key1 = "新值1"
print(mytable.key1,mymetatable.key1)

--以上实例执行输出结果为:
value1
nil    新值2
新值1    nil

5.lua中.:的区别

--冒号:在定义时省略了self
--点号:在定义时不省略self

Class = {}
Class.__index = Class
   
function Class.new(x,y)
    local cls = {}
    setmetatable(cls, Class)
    cls.x = x
    cls.y = y
    return cls
end

-- 实例方法

function Class:test()
    print(self.x,self.y)
end
 -- 等价于 
function Class.test(self)
    print(self.x,self.y)
end

function Class.testStatic() --类似于静态方法
    print('abc')--(self无法识别:静态方法里不是访问非静态)
end

object = Class.new(10,20)
  
object:test()
-- 等价于
object.test(object)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值