跑马灯就一个脚本 简单的思路 十二张奖品图片存入一个表里 找到父类并且打印出他的子类使用transform.childCount - 1的方法 把他存入一个表中因为我是用json 解析的数据需要把json里面的数据存入一个表中` 十秒钟之后会刷新一下数据会用到math.random这个随机函数 ```先看一下效果
*require("UnityUtil")
jsons = require("cjson")
local todraw = {}
local floor = 0
local times = 0
local itemscount = nil
local picitems = {}
local Objects = {}
--频率
local speeds = 0.5
function clicks()
print(13)
MaskImage.gameObject:SetActive(true)
MaskImage.fillAmount = 1
floor = 1
end
function todraw:OnStart()
DrawButton = GameObject.Find("lotteriesButton"):GetComponent(typeof(UI.Button))
DrawButton.onClick:AddListener(clicks)
MaskImage = self.MaskImage.transform:GetComponent(typeof(UI.Image))
itemscount = GameObject.Find("itemsCount")
Backgrounds = GameObject.Find("Background")
--获取子节点
for i = 1, itemscount.transform.childCount - 1 do
table.insert(picitems, itemscount.transform:GetChild(i).gameObject)
end
for i = 1, #picitems do
print(picitems[i].name)
end
--开始进行json解析
local jsonPath = System.IO.File.ReadAllText(UnityEngine.Application.dataPath .. "/Lua/jottery.json")
local Analysis = jsons.decode(jsonPath)
print(#Analysis.img)
--把json里面的数据存入表中
for i, v in pairs(Analysis.img) do
table.insert(Objects, v.img)
end
for i = 1, #picitems do
picitems[i]:GetComponent(typeof(UI.Image)).sprite = Resources.Load(Objects[math.random(1, #Objects)], typeof(UnityEngine.Sprite))
end
end
--indexnum指的是下标索引
local indexnum = 0
local timedata = 0
function todraw:OnUpdate()
times = times + Time.deltaTime
--这里的speed是指的时频率并不是速度**
**if floor == 1 then
if times>7 then
speeds = 100
elseif times > 5 then
--and times<=5.1 then
speeds = 0.2
elseif times >= 3 then
speeds = 0.075
elseif times >= 2 then
speeds = 0.2
print(speeds)
end
--改变要跑的图片的下标
timedata = timedata + Time.deltaTime
--根据索引依次累加
if timedata >= speeds then
indexnum = indexnum + 1
Backgrounds.transform.position = picitems[indexnum].transform.position
if indexnum == #picitems then
indexnum = 0
end
timedata = 0
end
--大于三十秒技能冷却
if times > 30 then
MaskImage.fillAmount = MaskImage.fillAmount - 0.01
if MaskImage.fillAmount == 0 then
MaskImage.gameObject:SetActive(false)
times=0
end
--大于10刷新数据
elseif times > 10 and times <= 11 then
for i = 1, #picitems do
picitems[i]:GetComponent(typeof(UI.Image)).sprite = Resources.Load(Objects[math.random(1, #Objects)], typeof(UnityEngine.Sprite))
end
end
end
end
return todraw**
```这里的数据需要使用json 需要引入json=require("cjson")的模块