WWW加载本地assetBundle的文件,通过XML动态修改里边的数值
之前介绍过什么是AssetBundle和PC执行文件的AssetBundle加载方式
传送门
现在通过WWW的方式加载本地的assetbundle文件(WWW已经被 UnityWebRequest替换掉,两种方式都可以加载)
一、webgl加载 AssetBundle 所需要的配置的平台
PC加载的AssetBundle的时候需要打包成Windows,而webgl加载AssetBundle的时候需要打包成webgl环境
如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;
public class AssetBundleWebgl {
[MenuItem("AssetBundle/CreatAssetBundleWebgl")]
static void AssetBundleWebGL()
{
//创建打包成assetbundle所需要的环境
string _path = "Data/Webgl";
//是否有这个文件夹,有的话打包,没有的话就创建
if (!Directory.Exists(_path))
{
Directory.CreateDirectory(_path);
}
//文件的内容,不需要任何解压方式,平台选择webgl,(打包成什么平台就需要选择什么平台)
BuildPipeline.BuildAssetBundles("Data/Webgl/bundle", BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.WebGL);
}
}
二、打包完之后加载
我是以image为例加载的,创建一个image预设体,打包成asset bundle文件然后加载到场景当中去的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using UnityEngine.UI;
using UnityEngine.Networking;
public class AssetBundleWebglLoad : MonoBehaviour
{
//数据用来接受XML内容的
int _rowCount;
int _columnCount;
//创建生成物体的父物体位置
Transform _imageFather;
//ui里边的适配ui的组件
GridLayoutGroup _grid;
void OnEnable()
{
_grid = gameObject.GetComponent<GridLayoutGroup>();
StartCoroutine(LoadXml());
}
void Start()
{
_imageFather = gameObject.GetComponent<Transform>();
}
// Use this for initialization
void AAA()
{
//通过屏幕的宽高比例来 适配Image的大小
float x = _grid.spacing.x;
_grid.cellSize = new Vector2((Screen.width - (_columnCount - 1) * x) / _columnCount, (Screen.height - (_rowCount - 1) * x) / _rowCount);
// StartCoroutine(DownLoadAB());
}
IEnumerator DownLoadAB()
{
//获取bundle的路径,打包成webgl是不需要加上 file:// PC的时候需要加 file://
//通过 Application.dataPath + "/../data"这个路径就是在打包好的文件里边加上data文件夹
//把打包好的bundle传进去 /../是返回 上一个文件夹
string path = Application.streamingAssetsPath + "/Webgl" + "/"+ _imageName;
//string path = "file://" + _fileDataPath + "/" + _imageABName;
// string path = "file://D:/UnityProject/TheFirstProject/Data/Webgl/image.image";
//通过WWW加载这个路径里边的内容,不需要加载缓存的情况(加载缓存的时候一定要记得清理缓存)
WWW _www = new WWW(path);
yield return _www;
if (!string.IsNullOrEmpty(_www.error))
{
yield break;
}
//加载assetbundle文件(原理和Resources是一样的)
AssetBundle _imageAssetBundle = _www.assetBundle;
GameObject _imageObject = _imageAssetBundle.LoadAsset<GameObject>(_imageName);
//通过镶嵌for循环生成多少行多少列
for (int i = 0; i < _rowCount; i++)
{
for (int j = 0; j < _columnCount; j++)
{
Instantiate(_imageObject, _imageFather);
}
}
//加载精灵,给生成的Image进行贴图
for (int i = 0; i < _spritName.Count-1; i++)
{
string _path = Application.streamingAssetsPath + "/Webgl" + "/" + _spritName[i];
// string _path ="file://" + _fileDataPath + "/" + _spiritOneABName;
//string _path = "file://D:/UnityProject/TheFirstProject/Data/Webgl/1.1";
WWW _spWWW = new WWW(_path);
//WWW _spWWW = WWW.LoadFromCacheOrDownload(_path, 1);
yield return _spWWW;
if (!string.IsNullOrEmpty(_spWWW.error))
{
yield break;
}
AssetBundle _spAssetBundle1 = _spWWW.assetBundle;
Sprite _spObject1 = _spAssetBundle1.LoadAsset<Sprite>(_spritName[i]);
_imageFather.transform.GetChild(i).GetComponent<Image>().sprite = _spObject1;
}
#region 固定赋值精灵
Caching.CleanCache();
string _path = "file://" + Application.streamingAssetsPath + "/Webgl" + "/" + _spiritOneName;
string _path = "file://" + _fileDataPath + "/" + _spiritOneABName;
string _path = "file://D:/UnityProject/TheFirstProject/Data/Webgl/1.1";
WWW _spWWW = new WWW(_path);
WWW _spWWW = WWW.LoadFromCacheOrDownload(_path, 1);
yield return _spWWW;
if (!string.IsNullOrEmpty(_spWWW.error))
{
yield break;
}
AssetBundle _spAssetBundle1 = _spWWW.assetBundle;
Sprite _spObject1 = _spAssetBundle1.LoadAsset<Sprite>(_spiritOneName);
_imageFather.transform.GetChild(0).GetComponent<Image>().sprite = _spObject1;
//string _path1 = Application.streamingAssetsPath + "/Webgl" + "/" + _spiritTwoName;
string _path1 = "file://" + _fileDataPath + "/" + _spiritTwoABName;
string _path1 = "file://D:/UnityProject/TheFirstProject/Data/Webgl/2.2";
//WWW _spWWW1 = new WWW(_path1);
WWW _spWWW1 = WWW.LoadFromCacheOrDownload(_path1, 1);
//yield return _spWWW1;
//if (!string.IsNullOrEmpty(_spWWW1.error))
//{
// yield break;
//}
//AssetBundle _spAssetBundle2 = _spWWW1.assetBundle;
//Sprite _spObject2 = _spAssetBundle2.LoadAsset<Sprite>(_spiritTwoName);
//_imageFather.transform.GetChild(1).GetComponent<Image>().sprite = _spObject2;
//string _path3 = Application.streamingAssetsPath + "/Webgl" + "/" + _spritThree;
//WWW _spWWW3 = new WWW(_path3);
//yield return _spWWW3;
//if (!string.IsNullOrEmpty(_spWWW3.error))
//{
// yield break;
//}
//AssetBundle _spAssetBundle3 = _spWWW3.assetBundle;
//Sprite _spObject3 = _spAssetBundle3.LoadAsset<Sprite>(_spritThree);
//_imageFather.transform.GetChild(2).GetComponent<Image>().sprite = _spObject3;
//string _path4 = Application.streamingAssetsPath + "/Webgl" + "/" + _spritFour;
//WWW _spWWW4 = new WWW(_path4);
//yield return _spWWW4;
//if (!string.IsNullOrEmpty(_spWWW4.error))
//{
// yield break;
//}
//AssetBundle _spAssetBundle4 = _spWWW4.assetBundle;
//Sprite _spObject4 = _spAssetBundle4.LoadAsset<Sprite>(_spritFour);
//_imageFather.transform.GetChild(3).GetComponent<Image>().sprite = _spObject4;
//string _path5 = Application.streamingAssetsPath + "/Webgl" + "/" + _spritFive;
//WWW _spWWW5 = new WWW(_path5);
//yield return _spWWW5;
//if (!string.IsNullOrEmpty(_spWWW5.error))
//{
// yield break;
//}
//AssetBundle _spAssetBundle5 = _spWWW5.assetBundle;
//Sprite _spObject5 = _spAssetBundle5.LoadAsset<Sprite>(_spritFive);
//_imageFather.transform.GetChild(4).GetComponent<Image>().sprite = _spObject5;
//string _path6 = Application.streamingAssetsPath + "/Webgl" + "/" + _spritSix;
//WWW _spWWW6 = new WWW(_path6);
//yield return _spWWW6;
//if (!string.IsNullOrEmpty(_spWWW6.error))
//{
// yield break;
//}
//AssetBundle _spAssetBundle6 = _spWWW6.assetBundle;
//Sprite _spObject6 = _spAssetBundle6.LoadAsset<Sprite>(_spritSix);
//_imageFather.transform.GetChild(5).GetComponent<Image>().sprite = _spObject6;
#endregion
}
#region
#endregion
IEnumerator LoadXml()
{
//获取XML文件
string _pathName = Application.streamingAssetsPath + "/Data/XML/AppData.xml";
//string _path = "file://D:/UnityProject/TheFirstProject/Data/XML/AppData.xml";
WWW _xmlWWW = new WWW(_pathName);
yield return _xmlWWW;
#region
//XmlDocument _xml = new XmlDocument();
//_xml.Load(_path);
//XmlNodeList _xmlNode = _xml.SelectSingleNode("Root/Image").ChildNodes;
//foreach (XmlElement item in _xmlNode)
//{
// if (item.Name == "RowCount")
// {
// _rowCount =
// int.Parse(item.InnerText);
// }
// if (item.Name == "ColumnCount")
// {
// _columnCount =
// int.Parse(item.InnerText);
// }
// if (item.Name == "ColumnCount")
// {
// _grid.constraintCount = int.Parse(item.InnerText);
// }
// if (item.Name == "Space")
// {
// _grid.spacing = new Vector2(int.Parse(item.InnerText), int.Parse(item.InnerText));
// }
//Debug.Log(_xmlWWW.error + "" + item.Name);
// }
#endregion
if (_xmlWWW.isDone)
{
System.IO.StringReader stringReader = new System.IO.StringReader(_xmlWWW.text);
// stringReader.Read();
string result = stringReader.ReadToEnd();
stringReader.Close();
ReadXml(result);
}
_xmlWWW.Dispose();
AAA();
yield return DownLoadAB();
}
string _imageName;
string _spiritOneName;
string _spiritTwoName;
string _spritThree;
string _spritFour;
string _spritFive;
string _spritSix;
string _imageABName;
List<string> _spritName =new List<string>();
//解析XML文件里边的内容,通过上方的定义进行赋值
void ReadXml(string path)
{
XmlDocument xml = new XmlDocument();
xml.LoadXml(path);
#region 节点一
XmlNodeList _xmlNode = xml.SelectSingleNode("Root/GridLayoutGroup").ChildNodes;
foreach (XmlElement item in _xmlNode)
{
if (item.Name == "RowCount")
{
_rowCount =
int.Parse(item.InnerText);
}
if (item.Name == "ColumnCount")
{
_columnCount =
int.Parse(item.InnerText);
}
if (item.Name == "ColumnCount")
{
_grid.constraintCount = int.Parse(item.InnerText);
}
if (item.Name == "Space")
{
_grid.spacing = new Vector2(int.Parse(item.InnerText), int.Parse(item.InnerText));
}
}
#endregion
XmlNodeList _xmlNode4 = xml.SelectSingleNode("Root/Image").ChildNodes;
foreach (XmlElement item in _xmlNode4)
{
if (item.Name == "Name")
{
_imageName = item.InnerText;
}
}
XmlNodeList _xmlNode2 = xml.SelectSingleNode("Root/Spirit").ChildNodes;
foreach (XmlElement item in _xmlNode2)
{
_spritName.Add(item.InnerText);
#region 固定的标签
//if (item.Name == "SpiritOne")
//{
// _spiritOneName = item.InnerText;
//}
//if (item.Name == "SpiritTwo")
//{
// _spiritTwoName = item.InnerText;
//}
//if (item.Name == "SpiritThree")
//{
// _spritThree = item.InnerText;
//}
//if (item.Name == "SpiritFour")
//{
// _spritFour = item.InnerText;
//}
//if (item.Name == "Five")
//{
// _spritFive = item.InnerText;
//}
//if (item.Name == "Six")
//{
// _spritSix = item.InnerText;
//}
#endregion
}
}
}