ToLua实现简单的跑马灯效果

8 篇文章 0 订阅

跑马灯就一个脚本 简单的思路 十二张奖品图片存入一个表里 找到父类并且打印出他的子类使用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")的模块
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值