目录为:Assets/Scripts/ConfigReader/目录下
ReadPreLoadConfig.cs
这个文件专门预加载特效的
对应配置文件:
Assets/Resources/Config/PreLoad.xml
部分如下
<PreloadEffect>
<Roles>
<Role id ="10001">
<Effect>effect/skill/release/sakuraline</Effect>
<Effect>effect/skill/buff/chenmo</Effect>
<Effect>effect/skill/release/sakuraline_ex</Effect>
<Effect>effect/skill/buff/sakuraarea</Effect>
<Effect>effect/skill/buff/Prohibited_weapon</Effect>
<Effect>effect/skill/buff/sakuraarea_ex</Effect>
<Effect>effect/skill/hit/Hitsakura</Effect>
<Effect>effect/skill/hit/Hitsakura</Effect>
</Role>
<Role id ="10002">
<Effect>effect/skill/release/sword</Effect>
<Effect>effect/skill/buff/stun</Effect>
<Effect>effect/skill/release/sword_ex</Effect>
<Effect>effect/skill/release/landcut</Effect>
<Effect>effect/skill/buff/jiansu_buff</Effect>
<Effect>effect/skill/release/landcut_ex</Effect>
<Effect>effect/skill/hit/Hitcutlight</Effect>
<Effect>effect/skill/hit/Hitblood</Effect>
<Effect>effect/skill/hit/Hitliedizhan</Effect>
</Role>
ReadPreLoadConfig.cs
using System;
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
//这里全是加载Effect的
public class ReadPreLoadConfig: Singleton<ReadPreLoadConfig>
{
//role files
private Dictionary<int, List<string>> roleFile = new Dictionary<int, List<string>>();
//Scene files
private List<string> sceneFile = new List<string>();
//所有需要预加载的场景以及影响资源信息
private List<string> allFiles = new List<string>();
//所有需要预加载的场景UITips
private List<string> uiTips = new List<string>();
XmlDocument xmlDoc = null;
public ReadPreLoadConfig()
{
}
//初始角色预加载信息
public void Init ()
{
LoadConfig ("Config/Preload");
}
//清除预加载信息
public void Clear()
{
allFiles.Clear ();
}
//进入场景后预加载特效
public void PreLoad()
{
//预加载特效
AddPreLoadScene ();
foreach (string file in allFiles)
{
//加载Scene里要用的特效
GameObject preObj = GameObjectPool.Instance.GetGO(file);
//把Object加入pool
GameObjectPool.Instance.ReleaseGO (file, preObj, PoolObjectType.POT_Effect);
}
//预加载uiTip
List<GameObject> uiTipObjs = new List<GameObject> ();
foreach (string tipName in uiTips)
{
GameObject tipObj = GameObjectPool.Instance.GetGO (tipName);
tipObj.name = tipName;
uiTipObjs.Add (tipObj);
}
foreach (GameObject tipObj in uiTipObjs)
{
//把Object加入pool
GameObjectPool.Instance.ReleaseGO (tipObj.name, tipObj, PoolObjectType.POT_UITip);
}
}
public void AddPreLoadRoleEffect(int heroId)
{
if (!roleFile.ContainsKey(heroId))
{
Debug.LogError("PreLoadRoleEffect hero is not exist id = " + heroId);
return;
}
List<string> files = roleFile [heroId];
foreach (string file in files)
{
//加载role要用到的effect
GameObject preObj = GameObjectPool.Instance.GetGO (file);
//把Object加入pool
GameObjectPool.Instance.ReleaseGO (file, preObj, PoolObjectType.POT_Effect);
}
}
//添加预加载场景特效包括所有吸附技能特效
public void AddPreLoadScene()
{
foreach (string file in sceneFile)
{
allFiles.Add (file);
}
}
public List<string> GetScene()
{
return sceneFile;
}
private void LoadConfig(string xmlFilePath)
{
ResourceUnit xmlfileUnit = ResourcesManager.Instance.loadImmediate (xmlFilePath, ResourceType.ASSET);
TextAsset xmlfile = xmlfileUnit.Asset as TextAsset;
xmlDoc = new XmlDocument ();
xmlDoc.LoadXml (xmlfile.text);
XmlNode preLoad = xmlDoc.SelectSingleNode ("PreloadEffect");
if (preLoad != null)
{
//开始加载Role部分
XmlNode roles = preLoad.SelectSingleNode ("Roles");
if (roles != null)
{
XmlNodeList roleList = roles.ChildNodes;
if (roleList != null)
{
foreach (XmlNode role in roleList)
{
int roleId = Convert.ToInt32 (role.Attributes ["id"].Value);
List<string> list = new List<string> ();
XmlNodeList effects = role.ChildNodes;
if (effects != null)
{
foreach (XmlNode effect in effects)
{
string path = effect.InnerText;
list.Add (path);
}
}
roleFile.Add (roleId, list);
}
}
}
//开始加载Scene部分
XmlNode scene = preLoad.SelectSingleNode ("Scene");
if (scene != null)
{
XmlNodeList objects = scene.ChildNodes;
if (objects != null)
{
foreach (XmlNode obj in objects)
{
string objs = obj.InnerText;
sceneFile.Add (objs);
}
}
}
//开始加载UITip部分
//UITip
XmlNode tips = preLoad.SelectSingleNode ("UITips");
if (tips != null)
{
XmlNodeList tipObjs = tips.ChildNodes;
if (tipObjs != null)
{
foreach (XmlNode tipObj in tipObjs)
{
string tipName = tipObj.InnerText;
uiTips.Add (tipName);
}
}
}
}
}
}