今天咱们继续学习xlua的案例,咱们发现这个xlua仍是很简单的,你须要要进行简单的操做,而后再根据你的需求就能够完成你想要的效果,可是xlua也有一个很差的地方,就是很容易写错,这个是比较蛋疼的,咱们在写lua语言的时候必定要注意。好了,开始咱们今天的内容。下面是客户提的要求。dom
1.2函数
1.与UI交互时不能发射子弹。*
2.技能扣钻石太多。*
3.boss撞击玩家数值变更同样且不是减小是增长。*学习
1.3ui
1.boss撞击玩家当钻石金币不够时会产生负数。*
2.炮台3太强,且钻石没用处,不削弱,只有氪金才可以使用。*
3.大鱼太多。 *lua
接下来咱们一个一个的解决。spa
1与UI交互时不能发射子弹。code
这个问题其实咱们在之前碰见过,就是siki老师的黑暗之光的课程中,其实这个问题也是很简单的,只要咱们在鼠标按到技能图标的时候,就不作动做。用到的就是这个方法:UnityEngine.EventSystems.EventSystem.current.IsPointerOverGameObject()只不过在lua中咱们要进行变形是UnityEngine.EventSystems.EventSystem.current:IsPointerOverGameObject()blog
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的简单引用,仍是最后一句话,必定要细心,否则会很容易错的。