我们在完成对xlua的学习后,现在我们在接下来的几天中,将会用一个案例来学习一下xlua的使用。请大家不用担心,这个课件的使用是基于xlua而开发的。因为我们在这个部分是为了使用xlua,所以我们只在已经做到的案例上进行xlua的学习。好了现在就开始吧。
一、补丁版本的开发
1.xlua的迁入与hotfix的环境配置
首先将从网上下下来的xlua文件,把里面的Assets下的文件的plugins和xlua以及伴生脚本全部拖入unity中。接着我们要在unity中的File-BuildSettings-PlayerSetting-other Settings-Scripting Define synbols下输入 HOTFIX_ENABLE 按Enter
我们在xlua-->Generate Code注入 Hotfix Engine Editor生成
复杂到FishingJoy\Assets\XLua\src\Editor就行了。
在unity中创建一个C# HotFixScripts
// Use this for initializationprivate LuaEnv luaEnv;void Start () {luaEnv = new LuaEnv();//添加状态机 luaEnv.AddLoader(MyLoader);luaEnv.DoString("require'fish'");}private byte[] MyLoader(ref string filePath){string absPath = @"D:\unity 3d\lesson1\FishingJoy\Assets\PlayerPackage\" +filePath + ".lua.txt";return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));}private void OnDisable(){luaEnv.DoString("require'fishDispose'"); }private void OnDestroy(){luaEnv.Dispose();}
这个就是我们所说的自定义loader
接下来我们就要修复第一个问题
1.点击宝箱领取的金币钻石太拥挤,分散一点。
UI---Treasour---Btn_Treasour-->c# Treasour 在类前下[Hotfix] 在createPrize()上加如[luacallscharp] 然后注入,生成 打开fish.lua.txt
UnityEngine=CS.UnityEngine --1.点击宝箱领取的金币钻石太拥挤,分散一点。 xlua.hotfix(CS.Treasour,'CreatePrize',function(self)for i=0,4,1 dolocal go=UnityEngine.GameObject.Instantiate(self.gold,self.transform.position+UnityEngine.Vector3(-10+i*40,0,0),self.transform.rotation)go.transform.SetParent(go.transform,self.cavas)local go1=UnityEngine.GameObject.Instantiate(self.diamands,self.transform.position+UnityEngine.Vector3(0,40,0)+UnityEngine.Vector3(-10+i*40,0,0),self.transform.rotation)go1.transform.SetParent(go1.transform,self.cavas)endend )
写上如上代码 在fishDispose.lua.txt上写下 xlua.hotfix(CS.Treasour,'CreatePrize',nil),就行了
2.玩家金币钻石不够时没有相应处理。
player c# Gun 在Attack上加[luacallscharp]在fish.lua.txt上写下:
xlua.private_accessible(CS.Gun) xlua.hotfix(CS.Gun,'Attack',function(self)if(UnityEngine.Input.GetMouseButtonDown(0))thenif(self.gold<1+(self.gunLevel-1)*2 or gold==0)thenreturnendself.bullectAudio.clip=self.bullectAudios[self.gunLevel-1]self.bullectAudio:Play()if(self.Butterfly)thenUnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation*UnityEngine.Quaternion.Enler(0,0,20))UnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation*UnityEngine.Quaternion.Enler(0,0,-20))endUnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation)if(not self.canShootForFree)thenself:GoldChange(-1-(self.gunLevel-1)*2)endself.attackCD=0self.attack=falseend end )
然后在fishDispose.lua.txt,写下xlua.hotfix(CS.Gun,'Attack',nil)就行了,这样这问题就结束了,记得到注册,并加注入。虽然这里写的比较简单,但是这里还是很容易写错的,我就是犯了一些错误。在初学xlua的时候,花式报错是一定的,我们还是要坚持的,一般的错误就是拼写错了。好了今天的部分就结束了,明天继续。