简单的Lua活动实现
2015-10-21 16:32
344 查看
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="python">Utility = luanet.import_type('Utility') local ActivityLevelPanel = { } local m_LevelRewardConfig local m_Grid local m_ScrollPanel local m_LevelItemPath = 'Chinese/Lua/Prefabs/LuaLevelReward' local ObjectNameTabel = { LevelGird = "Grid" } function ActivityLevelPanel:Awake(obj) FindAllObject(obj) end function ActivityLevelPanel:Start() if m_Grid.transform.childCount == 0 then InitList() else UpdateLevelList() end end function FindAllObject(obj) m_ScrollPanel = obj:GetComponent('UIScrollView') m_Grid = LuaManager.instance:FindChildObject(obj, ObjectNameTabel.LevelGird) end function InitList() Utility.ClearChild(m_Grid.transform) m_LevelRewardConfig = ConfigManager.instance:GetConfig(ConfigTypeEnum.Levelreward) for i = 1, m_LevelRewardConfig.Count do if m_LevelRewardConfig:ContainsKey(i) == true then local obj = GameObject.Instantiate(Resources.Load(m_LevelItemPath)) obj.transform.parent = m_Grid.transform obj.transform.localScale = Vector3.one obj.transform.localPosition = Vector3.zero obj.transform.localEulerAngles = Vector3.zero obj.name = m_LevelRewardConfig[i].level obj:GetComponent('UIDragScrollView').scrollView = m_ScrollPanel local luaComponent = obj:GetComponent('LuaComponent') luaComponent.LuaModule:UpdateData(m_LevelRewardConfig[i]) end end m_Grid:GetComponent('UIGrid'):Reposition() m_ScrollPanel:ResetPosition() end function UpdateLevelList() local objList = { } for i = 1, m_Grid.transform.childCount do objList[i] = m_Grid.transform.GetChild(i).gameObject end m_LevelRewardConfig = ConfigManager.instance:GetConfig(ConfigTypeEnum.Levelreward) for i = 1, m_LevelRewardConfig.Count do if m_LevelRewardConfig:ContainsKey(i) == true then local luaBase = objList[i]:GetComponent('LuaBase') luaBase.LuaModule:UpdateData(m_LevelRewardConfig[i]) end end m_Grid:GetComponent('UIGrid'):Reposition() m_ScrollPanel:ResetPosition() end return ActivityLevelPanel
<pre name="code" class="python">--require "LuaConfig" local luaRect = LuaManager.instance:RunLuaScripts('LuaConfig') ActivityLevelItem = { } EquipInfoTip = luanet.import_type('EquipInfoTip') GetActivityRewardProtocol = luanet.import_type('GetActivityRewardProtocol') GetActivityRewardResponse = luanet.import_type('GetActivityRewardResponse') ProtocolMng = luanet.import_type('ProtocolMng') Globals = luanet.import_type('Globals') local ObjectNameTabel = { RewardBtn = "RewardBtn", LevelLabel = "DiamondsNun", Reward1 = "Reward1", Reward2 = "Reward2", Reward3 = "Reward3", Reward4 = "Reward4", Reward5 = "Reward5", CountLabel = "count", } local LevelRewardType = { Coin = 1, Dimond = 2, Power = 3, TeamExp = 4, Honor = 5, } local IconPath = { kCoinPath = "Chinese/UI/Icon/Consumable/dalianjinbi", kDimondPath = "Chinese/UI/Icon/VipViw/2", kPowerPath = "Chinese/UI/Icon/playerCoin/icon_Spirit", kTeamExpPath = "Chinese/UI/Icon/playerCoin/zhanduiEXP", kHonorPath = "Chinese/UI/Icon/Consumable/Honoricon", kStonePath = "Chinese/UI/Icon/playerCoin/icon_Stone", } local ActivityGetRewardType = { Receive = 1, Received = 2, NotReceive = 3, } local ConstString = { kBtnChildLabelName = "RewardLabel", kMyGameObjectName = "gameObject", kOnRewardBtnClick = "OnRewardBtnClick" } local m_ActivityTplId = 1 local m_RewardBtn local m_LevelLabel local m_Reward1Obj local m_Reward2Obj local m_Reward3Obj local m_Reward4Obj local m_Reward5Obj local m_CountLabel1 local m_CountLabel2 local m_CountLabel3 local m_CountLabel4 local m_CountLabel5 local m_RewardObjList = { } local m_CountLabelList = { } local m_LevelRewardConfig local m_RewardConfig local m_PriVar = { } function ActivityLevelItem:Awake(obj) m_PriVar = { } m_PriVar[ConstString.kMyGameObjectName] = obj FindAllObject() end function ActivityLevelItem:Start() UIEventListener.Get(m_RewardBtn).OnClick = OnRewardBtnClick end function FindAllObject() local m_GameObject = m_PriVar[ConstString.kMyGameObjectName] m_RewardBtn = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.RewardBtn) m_LevelLabel = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.LevelLabel):GetComponent('UILabel') m_Reward1Obj = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.Reward1) m_Reward2Obj = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.Reward2) m_Reward3Obj = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.Reward3) m_Reward4Obj = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.Reward4) m_Reward5Obj = LuaManager.instance:FindChildObject(m_GameObject, ObjectNameTabel.Reward5) m_CountLabel1 = LuaManager.instance:FindChildObject(m_Reward1Obj, ObjectNameTabel.CountLabel):GetComponent('UILabel') m_CountLabel2 = LuaManager.instance:FindChildObject(m_Reward2Obj, ObjectNameTabel.CountLabel):GetComponent('UILabel') m_CountLabel3 = LuaManager.instance:FindChildObject(m_Reward3Obj, ObjectNameTabel.CountLabel):GetComponent('UILabel') m_CountLabel4 = LuaManager.instance:FindChildObject(m_Reward4Obj, ObjectNameTabel.CountLabel):GetComponent('UILabel') m_CountLabel5 = LuaManager.instance:FindChildObject(m_Reward5Obj, ObjectNameTabel.CountLabel):GetComponent('UILabel') m_RewardObjList[1] = m_Reward1Obj m_RewardObjList[2] = m_Reward2Obj m_RewardObjList[3] = m_Reward3Obj m_RewardObjList[4] = m_Reward4Obj m_RewardObjList[5] = m_Reward5Obj m_CountLabelList[1] = m_CountLabel1 m_CountLabelList[2] = m_CountLabel2 m_CountLabelList[3] = m_CountLabel3 m_CountLabelList[4] = m_CountLabel4 m_CountLabelList[5] = m_CountLabel5 end function ActivityLevelItem:UpdateData(levelReardConfig) m_LevelRewardConfig = levelReardConfig m_RewardBtn.name = m_LevelRewardConfig.id m_LevelLabel.text = levelReardConfig.level m_RewardConfig = LuaManager.instance:GetLevelRewardConfig(levelReardConfig.rewardID) local moneyList = Utility.GetRewardInfo(m_RewardConfig.rewardInfo, Utility.RewardInfoType.Money) local itemList = Utility.GetRewardInfo(m_RewardConfig.rewardInfo, Utility.RewardInfoType.Item) m_PriVar[ConstString.kMyGameObjectName].name = levelReardConfig.rewardID InitReward(moneyList, itemList) ShowBtnState() end function GetBtnState() local state if m_LevelRewardConfig.level > UserManager.instance.level then state = ActivityGetRewardType.NotReceive elseif UserManager.instance.activityLevelRewardList:Contains(m_LevelRewardConfig.id) then state = ActivityGetRewardType.Received else state = ActivityGetRewardType.Receive end return state end function UpdateBtn(state) UpdateTargetBtn(state, m_RewardBtn) end function UpdateTargetBtn(state, btn) local text local spriteName if (tonumber(state)) ==(tonumber(ActivityGetRewardType.Receive)) then text = LuaActivityConfig.kRewardReceive spriteName = LuaActivityConfig.kRewardReceiveSprite elseif (tonumber(state)) ==(tonumber(ActivityGetRewardType.Received)) then text = LuaActivityConfig.kRewardReceived spriteName = LuaActivityConfig.kRewardReceivedSprite elseif (tonumber(state)) ==(tonumber(ActivityGetRewardType.NotReceive)) then text = LuaActivityConfig.kRewardNotReceive spriteName = LuaActivityConfig.kRewardNotReceiveSprite end LuaManager.instance:FindChildObject(btn, ConstString.kBtnChildLabelName):GetComponent('UILabel').text = text btn:GetComponent('UISprite').spriteName = spriteName end function ShowBtnState() local state = GetBtnState() UpdateBtn(state) end function InitReward(moneyList, itemList) local path = UIConfig.kEquipMainBgQualityMap[5]; for i = 0, moneyList.Count - 1 do local rewardArr = string.split(moneyList[i], ',') -- lua table start with 1! local rewardType = rewardArr[2] if (tonumber(rewardType) == LevelRewardType.Coin) then Utility.DynamicCreatImageObject(IconPath.kCoinPath, m_RewardObjList[i + 1].transform, 20, 1, 1, 85, 85, "icon"); Utility.DynamicCreatImageObject(path, m_RewardObjList[i + 1].transform, 30, 1.0, 1.0, 96, 96, "bg"); elseif (tonumber(rewardType) == LevelRewardType.Dimond) then Utility.DynamicCreatImageObject(IconPath.kDimondPath, m_RewardObjList[i + 1].transform, 20, 1, 1, 85, 85, "icon"); Utility.DynamicCreatImageObject(path, m_RewardObjList[i + 1].transform, 30, 1, 1, 96, 96, "bg"); elseif (tonumber(rewardType) == LevelRewardType.Honor) then Utility.DynamicCreatImageObject(IconPath.kHonorPath, m_RewardObjList[i + 1].transform, 20, 1, 1, 85, 85, "icon"); Utility.DynamicCreatImageObject(path, m_RewardObjList[i + 1].transform, 30, 1, 1, 96, 96, "bg"); elseif (tonumber(rewardType) == LevelRewardType.Power) then Utility.DynamicCreatImageObject(IconPath.kPowerPath, m_RewardObjList[i + 1].transform, 20, 1, 1, 85, 85, "icon"); Utility.DynamicCreatImageObject(path, m_RewardObjList[i + 1].transform, 30, 1, 1, 96, 96, "bg"); elseif (tonumber(rewardType) == LevelRewardType.TeamExp) then Utility.DynamicCreatImageObject(IconPath.kTeamExpPath, m_RewardObjList[i + 1].transform, 20, 1, 1, 85, 85, "icon"); Utility.DynamicCreatImageObject(path, m_RewardObjList[i + 1].transform, 30, 1, 1, 96, 96, "bg"); end m_CountLabelList[i + 1].text = rewardArr[3] end for j = 0, itemList.Count - 1 do local rewardArr = string.split(itemList[j], ',') local config = LuaManager.instance:GetItemConfig(rewardArr[2]) Utility.DynamicCreatImageObject(config.icon, m_RewardObjList[j + moneyList.Count + 1].transform, 20, 1, 1, 85, 85, "icon") local itemPath = UIConfig.kEquipMainBgQualityMap[config.quality] Utility.DynamicCreatImageObject(itemPath, m_RewardObjList[j + moneyList.Count + 1].transform, 30, 1, 1, 96, 96, "bg") if config.type == 4 then Utility.DynamicCreateImageObject(IconPath.kStonePath, m_RewardObjList[j + moneyList.Count + 1].transform, 45, 1, 1, 96, 96) end UIEventListener.Get(m_RewardObjList[j + moneyList.Count + 1]).onPress = OnItemInfoPress; m_CountLabelList[j + moneyList.Count + 1].text = rewardArr[3] m_RewardObjList[j + moneyList.Count + 1].name = config.id end end function OnRewardBtnClick(obj) local labelText = tostring(LuaManager.instance:FindChildObject(obj, ConstString.kBtnChildLabelName):GetComponent('UILabel').text) if (labelText) ==(tostring(LuaActivityConfig.kRewardReceive)) then RequestActivityReward(obj) elseif (labelText) ==(tostring(LuaActivityConfig.kRewardReceived)) then MessageManager.DispathMsgWindowTipsEnum(ErrorCodeEnum.ERR_ACTIVITY_HAS_GET_REWARD); elseif (labelText) ==(tostring(LuaActivityConfig.kRewardNotReceive)) then MessageManager.DispathMsgWindowTipsEnum(ErrorCodeEnum.ERR_REWARD_SUCCESS); end end function RequestActivityReward(obj) ScreenLockManager.instance:LockScreen() local getActivityRewardProtocol = GetActivityRewardProtocol() getActivityRewardProtocol.GetActivityRewardRequestData.tplID = tonumber(m_ActivityTplId) getActivityRewardProtocol.GetActivityRewardRequestData.param = tonumber(obj.name) Globals.WriteGlobalData(ConstString.kOnRewardBtnClick, obj) getActivityRewardProtocol.ResultProcess:Add(GetActivityRewardProtocolResultProcess) getActivityRewardProtocol:Send(); end function GetActivityRewardProtocolResultProcess(errorString, dataReceive, protocol) local isProcessSuccessed = ProtocolMng.ProcessResultCode(dataReceive.result, true); if isProcessSuccessed == true then ScreenLockManager.instance:UnLockScreen(); MessageManager.DispathMsgWindowTipsEnum(ErrorCodeEnum.ERR_REWARD_FAIL); local obj = Globals.ReadGlobalData(ConstString.kOnRewardBtnClick) Globals.DeleteGlobalData(ConstString.kOnRewardBtnClick) UpdateTargetBtn(ActivityGetRewardType.Received, obj) UserManager.instance.activityLevelRewardList:Add(tonumber(obj.name)) else print(errorString) end end function OnItemInfoPress(obj, isback) if isback == true then local params = { [0] = ViewType.TipsView, [1] = EquipInfoTip.TipsPositionType.DOWN_RIGHT, [2] = EquipInfoTip.TipsType.ITEM_TIPS, [3] = tonumber(obj.name) } MessageManager.Dispatch(MsgType.View.show, params) else local params = { [0] = ViewType.TipsView, } MessageManager.Dispatch(MsgType.View.hide, params) end end return ActivityLevelItem
一个用Lua实现的简单的活动功能,能够做到列表刷新,奖励领取等功能。
相关文章推荐
- Ubuntu12.04 下Lua开发环境安装
- Error filling print... Error evaluating expression : Source text : $F{id}$F{guestId}
- lua c++中的一种回调解决方法
- Lua与C交换
- Lua包管理工具Luarocks详解
- 技术小故事-Activity的Launch Mode引起的动画“疑案”
- lua小结
- Step By Step(Lua字符串库) (转)
- lua
- lua 字符匹配
- 浅析lua异常捕获处理机制
- lua语言和c++比较
- luaunit的使用
- lua-intf的小技巧
- R中创建not-yet-evaluated对象
- Lua 引用外部文件(require)的调试错误
- Lua基础记录,附一个在线测试网址
- 冒泡排序(lua实现)
- torch中narrow的用法
- LUA中的and与or