//加载完成的回调 private void OnLoadCompleted() { if (EventOnLoadCompleted != null) { EventOnLoadCompleted.Invoke(); } }
//加载Bundle配置信息 public bool Load() { Clear(); if (!File.Exists(m_ConfigurationPath)) { return(false); } try { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(m_ConfigurationPath); //获取各个节点 XmlNode xmlRoot = xmlDocument.SelectSingleNode("UnityGameFramework"); XmlNode xmlCollection = xmlRoot.SelectSingleNode("AssetBundleCollection"); XmlNode xmlAssetBundles = xmlCollection.SelectSingleNode("AssetBundles"); XmlNode xmlAssets = xmlCollection.SelectSingleNode("Assets"); XmlNodeList xmlNodeList = null; XmlNode xmlNode = null; XmlNode xmlTemp = null; int count = 0; //遍历 AssetBundle信息 xmlNodeList = xmlAssetBundles.ChildNodes; count = xmlNodeList.Count; for (int i = 0; i < count; i++) { if (EventOnLoadingAssetBundle != null) { EventOnLoadingAssetBundle.Invoke(i, count); } //遍历每一个节点 xmlNode = xmlNodeList.Item(i); if (xmlNode.Name != "AssetBundle") { continue; } //bundle名称 string assetBundleName = xmlNode.Attributes.GetNamedItem("Name").Value; //变体名称 xmlTemp = xmlNode.Attributes.GetNamedItem("Variant"); string assetBundleVariant = xmlTemp != null ? xmlTemp.Value : null; //加载类型 int assetBundleLoadType = 0; xmlTemp = xmlNode.Attributes.GetNamedItem("LoadType"); if (xmlTemp != null) { int.TryParse(xmlTemp.Value, out assetBundleLoadType); } //是否打包 bool assetBundlePacked = false; xmlTemp = xmlNode.Attributes.GetNamedItem("Packed"); if (xmlTemp != null) { bool.TryParse(xmlTemp.Value, out assetBundlePacked); } //资源组 xmlTemp = xmlNode.Attributes.GetNamedItem("ResourceGroups"); string[] assetBundleResourceGroups = xmlTemp != null?xmlTemp.Value.Split(',') : new string[0]; //添加 if (!AddAssetBundleInfo(assetBundleName, assetBundleVariant, (AssetBundleLoadType)assetBundleLoadType, assetBundlePacked, assetBundleResourceGroups)) { string assetBundleFullName = assetBundleVariant != null?Utility.Text.Format("{0}.{1}", assetBundleName, assetBundleVariant) : assetBundleName; Debug.LogWarning(Utility.Text.Format("Can not add AssetBundle '{0}'.", assetBundleFullName)); continue; } } //遍历Assets信息 xmlNodeList = xmlAssets.ChildNodes; count = xmlNodeList.Count; for (int i = 0; i < count; i++) { if (EventOnLoadingAsset != null) { EventOnLoadingAsset.Invoke(i, count); } xmlNode = xmlNodeList.Item(i); if (xmlNode.Name != "Asset") { continue; } string assetGuid = xmlNode.Attributes.GetNamedItem("Guid").Value; //资源id string assetBundleName = xmlNode.Attributes.GetNamedItem("AssetBundleName").Value; //资源Bundle名 xmlTemp = xmlNode.Attributes.GetNamedItem("AssetBundleVariant"); string assetBundleVariant = xmlTemp != null ? xmlTemp.Value : null; if (!AssignAssetInfo(assetGuid, assetBundleName, assetBundleVariant)) { string assetBundleFullName = assetBundleVariant != null?Utility.Text.Format("{0}.{1}", assetBundleName, assetBundleVariant) : assetBundleName; Debug.LogWarning(Utility.Text.Format("Can not assign asset '{0}' to AssetBundle '{1}'.", assetGuid, assetBundleFullName)); continue; } } if (EventOnLoadCompleted != null) { EventOnLoadCompleted.Invoke(); } return(true); } catch { File.Delete(m_ConfigurationPath); if (EventOnLoadCompleted != null) { EventOnLoadCompleted.Invoke(); } return(false); } }