此处在本地执行,实际应从服务器中下在Lua补丁程序
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using XLua;
using System.IO;
public class HotFixScript : MonoBehaviour
{
private LuaEnv LuaEnv;
// Start is called before the first frame update
void Awake()//修改C#类的Start方法时,此处应在Awake中调用
{
LuaEnv = new LuaEnv();
LuaEnv.AddLoader(MyLoader);
LuaEnv.DoString("require'fish'");//fish中的Lua程序包含所有要更新的方法逻辑代码(Lua补丁程序)
}
private byte[] MyLoader(ref string filePath)
{
string absPath = @"D:\715\XluaProjects\PlayerGamePackage\" + filePath + ".lua.txt";
return System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(absPath));
}
private void OnDisable()
{
//在销毁前将委托置为空(避免报错)
//(Hotfix底层实际已委托形式实现,将Lua的方法注册到委托中,委托不为空时执行Lua中更新的方法)
LuaEnv.DoString("require'fishDispose'");
}
private void OnDestroy()
{
LuaEnv.Dispose();
}
}
/////////////////////////////////////////////////////////////
//fish.lua.txt
--1.修复CL间距
local UnityEngine=CS.UnityEngine
xlua.hotfix(CS.Treasour,'CreatePrize',function(self)
for i=0,4,1 do
local go = UnityEngine.GameObject.Instantiate(self.gold,self.transform.position+UnityEngine.Vector3(-10+i40,0,0),self.transform.rotation)
go.transform.SetParent(go.transform,self.cavas)
local go = UnityEngine.GameObject.Instantiate(self.diamands,self.transform.position+UnityEngine.Vector3(0,40,0)+UnityEngine.Vector3(-10+i40,0,0),self.transform.rotation)
go.transform.SetParent(go.transform,self.cavas)
end
end)
--****************************************************************
--2.修复Attack方法
xlua.private_accessible(CS.Gun)--要访问类中的私有变量时
xlua.hotfix(CS.Gun,'Attack',function(self)
if UnityEngine.Input.GetMouseButtonDown(0) then
if UnityEngine.EventSystems.EventSystem.current:IsPointerOverGameObject() then
return
end
--[[
if self.gold<1+(self.gunLevel-1)*2 or gold ==0 then
return
end
--]]
--削弱Level3
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
self.bullectAudio.clip = self.bullectAudios[self.gunLevel-1]
--self.bullectAudio.Play(self)成员变量调用内部方法方式1
self.bullectAudio:Play()--方式2
if self.Butterfly then
UnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation*UnityEngine.Quaternion.Euler(0,0,20))
UnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation*UnityEngine.Quaternion.Euler(0,0,-20))
end
UnityEngine.GameObject.Instantiate(self.Bullects[self.gunLevel-1],self.attackPos.position,self.attackPos.rotation)
if not self.canShootForFree then
if self.gunLevel==3 then
self:DiamandsChange(-3)
else
self:GoldChange(-1,-(self.gunLevel-1)*2)
end
end
self.attackCD = 0
self.attack = false
end
end)
--*************************************************************
--3.金币扣除双修改
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)
--*******************************************************************
--4.Prace
local util = require 'util'--工具库
xlua.private_accessible(CS.Boss)
util.hotfix_ex(CS.Boss,'Start',function(self)--在C#脚本的基础上附加代码(谨慎使用次饭饭执行慢)
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_reduceDiamond = self.m_reduceDiamond-5
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)
--************************************
--5.0处理CL产生负值
util.hotfix_ex(CS.Gun,'GoldChange',function(self,number)--修改存在参数的方法时,将自身与参数都写入函数
self.GoldChange(self,number)
if self.gold<-number then
self.gold = 0
return
end
end)
util.hotfix_ex(CS.Gun,'DiamandsChange',function(self,number)
self.DiamandsChange(self,number)
if self.diamands<-number then
self.diamands = 0
return
end
end)
--****************************************************
--6.0Fish生成方式
--利用C#类中的静态方法加载AB资源
local canCreateNewFish = true
local changeMapTimeval = 0
xlua.hotfix(CS.CreateFish,'Start',function(self)
self.hotFixScript:LoadResource('level3fish3','gamaobjectenemy.ab')
self.hotFixScript:LoadResource('SeaWave','gameobjectwave.ab')
end)
xlua.private_accessible(CS.CreateFish)
xlua.hotfix(CS.CreateFish,'Update',function(self)
--生成海浪
if canCreateNewFish then
if changeMapTimeval>=5 then
go = CS.HotFixScript.GetGameObject("SeaWave")
UnityEngine.GameObject.Instantiate(go)
canCreateNewFish = false
changeMapTimeval = 0
else
changeMapTimeval=changeMapTimeval+UnityEngine.Time.deltaTime
end
else
return
end
self:CreateALotOfFish()
--单种鱼的生成
if self.ItemtimeVal >= 0.5 then
--位置随机数
self.num = UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0, 4))--Lua只有一种数值类型此处取浮点型下界
--游戏物体随机数
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,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])
end
if self.ItemNum <= 42 then
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])
--2.0新的鱼/AB包
elseif self.ItemNum >= 73 and self.ItemNum < 83 then
newFish = CS.HotFixScript.GetGameObject('level3fish3')
self:CreateGameObject(newFish)
elseif self.ItemNum >= 84 and self.ItemNum < 86 then
self:CreateGameObject(self.boss)
elseif self.ItemNum >= 87 and self.ItemNum <= 88 then
self:CreateGameObject(self.boss2)
elseif self.ItemNum ==100 then
self:CreateGameObject(self.boss3)
else
self:CreateGameObject(self.item[0]);
end
self.ItemtimeVal = 0;
else
self.ItemtimeVal = self.ItemtimeVal + UnityEngine.Time.deltaTime;
end
end)
--************************************************************
--7.0重写捕捉成功概率
xlua.private_accessible(CS.Fish)
xlua.hotfix(CS.Fish,'TakeDamage',function(self,attackValue)
if CS.Gun.Instance.Fire then
attackValue = attackValue*2
end
local catchValue = UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0,100))
if catchValue<=(50-(self.hp-attackValue))/2 then
self.isDead = true;
for i=0,8,1 do
UnityEngine.GameObject.Instantiate(self.pao, self.transform.position, UnityEngine.Quaternion.Euler(self.transform.eulerAngles + UnityEngine.Vector3(0, 45 * i, 0)))
end
self.gameObjectAni:SetTrigger("Die");
self:Invoke("Prize", 0.7);
end
end)
--Boss Cath概率
xlua.hotfix(CS.Boss,'TakeDamage',function(self,attackValue)
if CS.Gun.Instance.Fire then
attackValue = attackValue*2
end
local catchValue = UnityEngine.Mathf.Floor(UnityEngine.Random.Range(0,100))
if catchValue <= (attackValue*3-self.hp/10) then
UnityEngine.GameObject.Instantiate(self.deadEeffect, self.transform.position, self.transform.rotation)
CS.Gun.Instance:GoldChange(self.GetGold * 10);
CS.Gun.Instance:DiamandsChange(self.GetDiamands * 10)
for i=0,10,1 do
local itemGo = UnityEngine.GameObject.Instantiate(self.gold, self.transform.position, UnityEngine.Quaternion.Euler(self.transform.eulerAngles + UnityEngine.Vector3(0, 18 + 36 * (i - 1), 0)))
itemGo:GetComponent('Gold').bossPrize = true
end
for i=0,10,1 do
local itemGo1 = UnityEngine.GameObject.Instantiate(self.diamands, self.transform.position, UnityEngine.Quaternion.Euler(self.transform.eulerAngles + UnityEngine.Vector3(0, 36 + 36 * (i - 1), 0)))
itemGo1:GetComponent('Gold').bossPrize = true
end
UnityEngine.Object.Destroy(self.gameObject)
end
end)
--*************************************************************
--8.0Gun控制方式修改
xlua.hotfix(CS.Gun,'RotateGun',function(self)
if UnityEngine.Input.GetKey(UnityEngine.KeyCode.A) then
self.transform:Rotate(UnityEngine.Vector3.forward * self.rotateSpeed);
elseif UnityEngine.Input.GetKey(UnityEngine.KeyCode.D) then
self.transform:Rotate(-UnityEngine.Vector3.forward * self.rotateSpeed);
end
self:ClampAngle()
end)
xlua.private_accessible(CS.GunImage)
xlua.hotfix(CS.GunImage,'RotateGun',function(self)
if UnityEngine.Input.GetKey(UnityEngine.KeyCode.A) then
self.transform:Rotate(UnityEngine.Vector3.forward * self.rotateSpeed);
elseif UnityEngine.Input.GetKey(UnityEngine.KeyCode.D) then
self.transform:Rotate(-UnityEngine.Vector3.forward * self.rotateSpeed);
end
self:ClampAngle()
end)
--*************************************************************
--9.0新的类
xlua.private_accessible(CS.HotFixEmpty)
xlua.hotfix(CS.HotFixEmpty,'Start',function(self)
self:Invoke("BehaviourMethod",8)
end)
xlua.hotfix(CS.HotFixEmpty,'Update',function(self)
self.transform:Translate(-self.transform.right4UnityEngine.Time.deltaTime,UnityEngine.Space.World)
end)
xlua.hotfix(CS.HotFixEmpty,'OnTriggerEnter',function(self,other)
if other.tag~="Untagged" and other.tag~="Wall" then
UnityEngine.Object.Destroy(other.gameObject)
end
end)
xlua.hotfix(CS.HotFixEmpty,'BehaviourMethod',function(self)
CS.Gun.Instance.level = CS.Gun.Instance.level+1
if CS.Gun.Instance.level==4 then
CS.Gun.Instance.level=1
end
canCreateNewFish = true
CS.Gun.Instance.changeAudio = true
UnityEngine.Object.Destroy(self.gameObject)
end)
\\\\\\\\\\\\\\\\\\\\\\\\\\
\fishDispose.lua.txt
xlua.hotfix(CS.Treasour,'CreatePrize',nil)
xlua.hotfix(CS.Gun,'Attack',nil)