017-热更新只FishingJoy二
今天我们继续学习xlua的案例,我们发现这个xlua还是很简单的,你需要要进行简单的操作,然后再根据你的需求就可以完成你想要的效果,但是xlua也有一个不好的地方,就是很容易写错,这个是比较蛋疼的,我们在写lua语言的时候一定要注意。好了,开始我们今天的内容。下面是客户提的要求。
1.2
1.与UI交互时不能发射子弹。*
2.技能扣钻石太多。*
3.boss撞击玩家数值变动一样且不是减少是增加。*
1.3
1.boss撞击玩家当钻石金币不够时会产生负数。*
2.炮台3太强,且钻石没用处,不削弱,只有氪金才可使用。*
3.大鱼太多。 *
接下来我们一个一个的解决。
1与UI交互时不能发射子弹。
这个问题其实我们在以前遇见过,就是siki老师的黑暗之光的课程中,其实这个问题也是很简单的,只要我们在鼠标按到技能图标的时候,就不做动作。用到的就是这个方法:UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()只不过在lua中我们要进行变形是UnityEngine.EventSystems.EventSystem.current:IsPointerOverGameObject()
if UnityEngine.EventSystems.EventSystem.current:IsPointerOverGameObject() then return end
在进行技能的开始的时候加入这句话就行了,就是fish.lua.txt中的第二个函数。
2技能扣钻石太多。
这个问题我们可以在项目中发现管理技能的三个脚本,ButterFly Fire Ice,在三个类中,找到要改的方法,注入,注册,这个写法也是很简单的,如下:
xlua.private_accessible(CS.Fire) xlua.hotfix(CS.Fire,'Start',function(self) self.reduceDiamands=8 end ) xlua.private_accessible(CS.Ice) xlua.hotfix(CS.Ice,'Start',function(self) self.reduceDiamands=8 end ) xlua.private_accessible(CS.ButterFly) xlua.hotfix(CS.ButterFly,'Start',function(self) self.reduceDiamands=5 end )
注意一点的是要在fishDispose.lua.txt中进行反注册
xlua.hotfix(CS.Fire,'Start',nil) xlua.hotfix(CS.Ice,'Start',nil) xlua.hotfix(CS.ButterFly,'Start',nil)
3.boss撞击玩家数值变动一样且不是减少是增加
在这个问题中我们发现其实我们要改的boss类中的指定方法有很多的代码是有用的,我们只要改其中的一点点内容,所以我们就要用到了新方法util,就是将xlua中的util赋值到fish.lua.txt同一级目录,然后就可以用如下的写法了
local util=require 'util' xlua.private_accessible(CS.Boss) util.hotfix_ex(CS.Boss,'Start',function(self) self.Start(self) self.m_reduceGold=self.m_reduceGold-20 end ) xlua.private_accessible(CS.DeffendBoss) util.hotfix_ex(CS.DeffendBoss,'Start',function(self) self.Start(self) self.m_reduceGold=self.m_reduceGold-30 end ) xlua.private_accessible(CS.InvisibleBoss) util.hotfix_ex(CS.InvisibleBoss,'Start',function(self) self.Start(self) self.m_reduceDiamond=self.m_reduceDiamond-5 end )
记得反注册,在这里我们还要注意的一点是hotfixScripts中的Start()中的方法,写到Awake中。因为在Start中写的话,编译的顺序就不一致的,lua的工作要在所有的工作的之前,这样才不会报错。
4boss撞击玩家当钻石金币不够时会产生负数
这个问题如上面的一样如下:
util.hotfix_ex(CS.Gun,'GoldChange',function(self,number) --只需要在负数的时候为0就行,其他方法不变 self.GoldChange(self,number) if(self.gold<-number)then self.gold=0 return end end )
5炮台3太强,且钻石没用处,不削弱,只有氪金才可使用。
这个问题就是限制炮台3的使用限制,必须使用砖石才行。这样我们就知道我们应该在炮台选择的方法去修改。在fish.lua.txt的第二个方法中写如下:
--炮台3太强,且钻石没用处,不削弱,只有氪金才可使用。 if self.gunLevel==3 and self.diamands<3 then return elseif self.gunLevel ~=3 then if(self.gold<1+(self.gunLevel-1)*2 or gold==0)then return end end
在进行选择的操作,我们还要进行砖石的扣除,如下图:
if(not self.canShootForFree)then if(self.gunLevel==3)then self:DiamandsChange(-3) else self:GoldChange(-1-(self.gunLevel-1)*2) end end
6大鱼太多。这个就是我们在写脚本的时候,没有索引用变量写上,这样的话,拓展性将会很差,我们就重写方法:
xlua.private_accessible(CS.CreateFish) xlua.hotfix(CS.CreateFish,'Update',function(self) self:CreateALotOfFish() --大鱼太多。 if (self.ItemtimeVal >= 0.5)then --//位置随机数 self.num = UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0, 4)) --//游戏物体随机数 self.ItemNum = UnityEngine.Mathf.Floor(UnityEngine.Random.Range(1, 101)) local halfLength=self.fishList.Length/2 --定义小鱼的产生概率 local littlefishTypeIndex=UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0,halfLength)) --定义大鱼的生产概率 local bigfishTypeIndex=UnityEngine.Mathf.Floor(UnityEngine.Random.Range(halfLength+1,self.fishList.Length)) --定义物品的生产概率 local itemTypeIndex=UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0,self.item.Length)) --产生气泡 if (self.ItemNum < 20)then self:CreateGameObject(self.item[3]); self:CreateGameObject(self.fishList[6]); end --贝壳10% 85-94 --第一种鱼42% 42 if (self.ItemNum <= 42)then --3种小鱼 for i=0,2,1 do self:CreateGameObject(self.fishList[littlefishTypeIndex]) end self:CreateGameObject(self.item[itemTypeIndex]) --第二种鱼30% 43-72 elseif (self.ItemNum >= 43 and self.ItemNum < 72)then --两种大鱼 for i=0,1,1 do self:CreateGameObject(self.fishList[bigfishTypeIndex]) end self:CreateGameObject(self.item[itemTypeIndex]) elseif(self.ItemNum>=84 and self.ItemNum<=86)then self.CreateGameObject(self,self.boss) elseif(self.ItemNum>=87 and self.ItemNum<=88)then self.CreateGameObject(self,self.boss2) elseif(self.ItemNum==100)then self.CreateGameObject(self,self.boss3) else self:CreateGameObject(self.item[0]) end self.ItemtimeVal = 0; else self.ItemtimeVal =self.ItemtimeVal+UnityEngine.Time.deltaTime end end)
这样些话的,我们就可以完成最后一个要求的。我们发现本部分的内容计较简单,这就说明xlua的简单引用,还是最后一句话,一定要细心,不然会很容易错的。