C#中加载AB包与获取AB包中Object的静态方法
public static Dictionary<string, GameObject> prefabDict = new Dictionary<string, GameObject>();//根据对象名称存储对象的静态字典集合
[LuaCallCSharp]
public static void LoadResource(string resName,string filePath)
{
AssetBundle ab = AssetBundle.LoadFromFile(@"D:\715\XluaProjects\FishingJoy\AssetBundles\"+filePath);//本AB地资源路径
GameObject gameObject = ab.LoadAsset<GameObject>(resName);
prefabDict.Add(resName, gameObject);//键值存到字典集合
}
[LuaCallCSharp]
public static GameObject GetGameObject(string goName)
{
return prefabDict[goName];//根据打包前的对象名获取字典中对应的对象
}
/////////////////////////////////////////////////////////////////////////////
--****************************************************
--6.0Fish生成方式
--利用C#类中的静态方法加载AB资源(此处静态方法放在HotFixScript热更新脚本中)
xlua.hotfix(CS.CreateFish,'Start',function(self)//更新HotFixScript中的Start方法
CS.HotFixScript.LoadResource('level3fish3','gamaobjectenemy.ab')//加载AB包将新的对象加载放入字典集合
end)
xlua.private_accessible(CS.CreateFish)
xlua.hotfix(CS.CreateFish,'Update',function(self)
self:CreateALotOfFish()
--单种鱼的生成
if self.ItemtimeVal >= 0.5 then
--Lua中生成随机数的处理
--位置随机数
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])
--加载本地AB包中的资源********
--2.0新的鱼/AB包
elseif self.ItemNum >= 73 and self.ItemNum < 83 then
newFish = CS.HotFixScript.GetGameObject('level3fish3')--Lua中无泛型类,不需指定类型,直接声明字段接收静态方法返回的对象
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)