当前位置: 首页 > news >正文

水果机,夺宝动画实现

效果(动画不流畅是gif帧率低造成的)

GIF 2025-7-27 16-47-46

 

 入口代码

Assets/FruitMachine/FruitMachineDemo.cs

这个脚本挂在Layer节点上

public class FruitMachineDemo : MonoBehaviour
{private LuaEnv m_LuaEnv;private RectTransform m_Layer;private List<Action> m_Actions = new List<Action>();void Awake(){m_Layer = (RectTransform)transform;}void Start(){m_LuaEnv = new LuaEnv();m_LuaEnv.AddLoader((ref string filePath) =>{filePath = filePath.Replace('.', '/');var assetFilePath = $"Assets/{filePath}.lua.txt";
#if UNITY_EDITORfilePath = Path.GetFullPath(assetFilePath);
#endifvar txtAsset = AssetDatabase.LoadAssetAtPath<TextAsset>(assetFilePath);return Encoding.UTF8.GetBytes(txtAsset.text);});m_LuaEnv.Global.Set("FruitMachineDemoInst", this); //在lua中增加一个全局变量,可以在lua中用FruitMachineDemoInst来访问c#对象m_LuaEnv.DoString("require('FruitMachine.Main')");}void OnDestroy(){m_Actions.Clear();m_Actions = null;if (null != m_LuaEnv){m_LuaEnv.Dispose();m_LuaEnv = null;}}public RectTransform GetLayerRoot(){return m_Layer;}public void AddAction(Action action){if (null != action)m_Actions.Add(action);}public void RemoveAction(Action action){if (null != action)m_Actions.Remove(action);}void Update(){if (m_Actions.Count > 0){foreach (var act in m_Actions){act();}}}}

 

Assets/FruitMachine/Main.lua.txt

require("Lua.LuaCoMgr")
local Type_Button = typeof(CS.UnityEngine.UI.Button)---@type FruitMachineAnim
local m_FruitMachineAnim = require("FruitMachine.FruitMachineAnim").new()-- //////////local Inner = {}local m_RootTrans = FruitMachineDemoInst:GetLayerRoot()local m_ItemUIs = {}
local m_CurItemIndex = 1local m_BlinkCoId = 0-- //////////m_RootTrans:Find("btn_Start"):GetComponent(Type_Button).onClick:AddListener(function()if 0 == m_BlinkCoId thenm_FruitMachineAnim:StartForResult(5)end
end)for i=1,8 dolocal itemTrans = m_RootTrans:Find("item_"..i)m_ItemUIs[i] = {selFrame = itemTrans:Find("selFrame").gameObject,}
endm_FruitMachineAnim:Prepare(8, function(flag, sender)local oldItemIndex = m_CurItemIndexm_CurItemIndex = m_FruitMachineAnim:GetCurIndex()
m_ItemUIs[oldItemIndex].selFrame:SetActive(false)m_ItemUIs[m_CurItemIndex].selFrame:SetActive(true)if "ItemChange" == flag thenelseif "ShowResult" == flag thenm_BlinkCoId = LuaCoMgr.Start(function()for i=1,3 doLuaCoMgr.WaitForSeconds(0.1)m_ItemUIs[m_CurItemIndex].selFrame:SetActive(false)LuaCoMgr.WaitForSeconds(0.1)m_ItemUIs[m_CurItemIndex].selFrame:SetActive(true)endLuaCoMgr.WaitForSeconds(1)m_BlinkCoId = 0m_FruitMachineAnim:StartForIdle()end)end
end)
m_FruitMachineAnim:StartForIdle()function Inner.Update()LuaCoMgr.OnUpdate()m_FruitMachineAnim:OnUpdate()
endFruitMachineDemoInst:AddAction(Inner.Update)

 

LuaCoMgr看这边:xlua - lua协程模拟Unity协程

 

动画代码

 Assets/FruitMachine/FruitMachineAnim.lua.txt

local _Time = CS.UnityEngine.Time---@class FruitMachineAnim
local FruitMachineAnim = {}
FruitMachineAnim.__index = FruitMachineAnimlocal Inner = {}function FruitMachineAnim.new()local obj = {}setmetatable(obj, FruitMachineAnim)obj:ctor()return obj
endfunction FruitMachineAnim:ctor()self.m_RunningFlag = 0
endfunction FruitMachineAnim:GetCurIndex()return self.m_CurIndex
endfunction FruitMachineAnim:Prepare(cnt, cb)if 0 ~= self.m_RunningFlag then--要在运行前调用returnendself.m_Cnt = cntself.m_Callback = cbself.m_ElapseTime = 0self.m_CurIndex = 0
endfunction FruitMachineAnim:StartForIdle()if 0 ~= self.m_RunningFlag then--播放动画中returnendself.m_RunningFlag = 1self.m_ResultIndex = -1self.m_ItemChangeInterval = 0.7
endfunction FruitMachineAnim:StartForResult(resultIndex)if 2 == self.m_RunningFlag then--播放抽奖动画中returnendself.m_RunningFlag = 2self.m_ResultIndex = resultIndexself.m_ShowResultFlag = falseLuaCoMgr.Start(Inner.DoSpeedUpAndDown, self)
endfunction FruitMachineAnim:StopForIdle()if 2 == self.m_RunningFlag then--播放抽奖动画中, 不能强制停止returnendself.m_RunningFlag = 0
endfunction FruitMachineAnim:IsPlayIdle()return 1 == self.m_RunningFlag
endfunction FruitMachineAnim:IsPlayResult()return 2 == self.m_RunningFlag
end---@param self FruitMachineAnim
function Inner.DoSpeedUpAndDown(self)self.m_ItemChangeInterval = 0.7-- ///// 加速local initInterval = 0.7 --初始切换间隔local speedUpInterval = 0.03 --加速到最快时的切换间隔local speedUpCnt = 10 -- 加速次数local speedUpTotalTime = 0.5 --加速总共用时local speedUpStep = (initInterval - speedUpInterval) / speedUpCntlocal speedUpPerTime = speedUpTotalTime / speedUpCntfor i=1,speedUpCnt doLuaCoMgr.WaitForSeconds(speedUpPerTime)self.m_ItemChangeInterval = self.m_ItemChangeInterval - speedUpStependlocal speedUpDur = 2 --达到最快后持续时长
    LuaCoMgr.WaitForSeconds(speedUpDur)-- /////-- ///// 减速local speedDownInterval = 0.2 --减速到最慢时的切换间隔local speedDownCnt = 10 --减速次数local speedDownTotalTime = 2 --减速总共用时local speedDownStep = (speedDownInterval - speedUpInterval) / speedDownCntlocal speedDownPerTime = speedDownTotalTime / speedDownCntfor i=1,speedDownCnt doLuaCoMgr.WaitForSeconds(speedDownPerTime)self.m_ItemChangeInterval = self.m_ItemChangeInterval + speedDownStependlocal speedDownDur = 0.5LuaCoMgr.WaitForSeconds(speedDownDur)-- /////
self.m_ShowResultFlag = true
endfunction FruitMachineAnim:OnUpdate()if 0 == self.m_RunningFlag thenreturnendself.m_ElapseTime = self.m_ElapseTime + _Time.unscaledDeltaTimeif self.m_ElapseTime >= self.m_ItemChangeInterval thenself.m_ElapseTime = self.m_ElapseTime - self.m_ItemChangeIntervalself.m_CurIndex = self.m_CurIndex + 1if self.m_CurIndex > self.m_Cnt thenself.m_CurIndex = 1endif self.m_ShowResultFlag then --结果出来了if self.m_CurIndex == self.m_ResultIndex thenself.m_RunningFlag = 0self.m_ShowResultFlag = falseif self.m_Callback thenself.m_Callback("ShowResult", self)endelseif self.m_Callback thenself.m_Callback("ItemChange", self)endendelse --结果还没出来if self.m_Callback thenself.m_Callback("ItemChange", self)endendend
endreturn FruitMachineAnim

 

http://www.wuyegushi.com/news/586.html

相关文章:

  • DMP学习路线之进阶
  • 关于逆元目前的两种求法以及证明
  • [Record] 计数选讲 20250727
  • 7/27
  • 大数据之路:阿里巴巴大数据实践——大数据领域建模综述
  • POLIR-Laws-民法典: 第三编 合同 : 第二分编 典型合同: 21.保管、22.仓储、23.委托、24.物业服务、25.行纪、26.中介
  • 记录个IAR程序下载后硬件复位不运行,必须断电复位才运行的问题
  • 操作系统 - 浪矢
  • Qt布局管理
  • 最小树形图:朱刘算法
  • 基于YOLOv8的边坡排水沟堵塞检测与识别项目|完整源码数据集+PyQt5界面+完整训练流程+开箱即用!
  • POLIR-Laws-民法典: 第三编 合同 : 第二分编 典型合同: 20.技术合同 : 1)一般规定、2)技术开发、3)技术转让 和 技术许可、4)技术咨询 和 技术服务
  • hybrid口
  • 利用Transformer模型提升产品检索效果
  • 第二十天
  • 《恶意代码实战分析》笔记
  • POLIR-Laws-民法典: 第三编 合同 : 第二分编 典型合同: 19.运输合同 : 1)一般规定、2)客运合同、3)货运合同、4)多式联运合同
  • 《大道至简》读后感
  • @GetMapping、@PostMapping、@PutMapping、@DeleteMapping
  • 建模神器草图大师!SketchUp 2025 安装激活全流程,新手也能玩转!
  • 【最新专业评测】PDF Reducer专业版:85%超高压缩率的PDF压缩神器|Windows最佳PDF压缩工具推荐
  • @RequestMapping
  • DMP学习路径之入门
  • 第一篇随笔
  • 旋转链表 - 商商
  • 匀速二阶贝塞尔曲线
  • Redis原理
  • HTTP POST请求:初学者指南与示范
  • @Autowired 自动依赖注入
  • 基于接口划分vlan