一.本地资源一般就是预制体,图片(jpg,png),音乐等文件。如何动态的加载我们需要的资源呢?有哪些方式。
二.加载本地资源的三种方式。
1.采用Resource.Load方法读取。
2.采用WWW类加载资源。
3.采用C#的Image类进行图片的加载。
4.这是一套加载本地资源的架构。
public class LoadPrefebTool : MonoBehaviour
{
public static LoadPrefebTool Instance;
private Queue<LoadPrefebDTO> _loadList = new Queue<LoadPrefebDTO>();
private bool _isWaitting = false;
//private string _assetBundleBasePath;
private string _lockStr = "";
//设置同时异步加载数
//private int _waittingCount, _waittingMax = 1;
void Awake()
{
if (Instance == null)
{
Instance = this;
}
else
{
Destroy(gameObject);
}
DontDestroyOnLoad(gameObject);
//_assetBundleBasePath = Path.Combine(Application.persistentDataPath, ABConfigManager.BaseABPath);
}
public void LoadPrefeb(string path,Action<KeyValuePair<GameObject,object>> callBack, object returnPara = null,bool isCallWithNull = false)
{
//Debug.Log("LoadOnePrefeb:" + path);
var dto = new LoadPrefebDTO
{
Path = path,
ReturnPara = returnPara,
CallBackGameObject = callBack,
Type = LoadPrefabTypeEnum.GameObject,
IsCallWithNull = isCallWithNull,
};
_loadList.Enqueue(dto);
FixedUpdate();
}
public void LoadSprite(string path, Action<KeyValuePair<Sprite, object>> callBack, object returnPara = null, bool isCallWithNull = false)
{
//Debug.Log("LoadSprite:" + path);
var dto = new LoadPrefebDTO
{
Path = path,
ReturnPara = returnPara,
CallBackSprite = callBack,
Type = LoadPrefabTypeEnum.SpritePng,
IsCallWithNull = isCallWithNull,
};
_loadList.Enqueue(dto);
FixedUpdate();
}
public void LoadAtlas(string path, Action<KeyValuePair<SpriteAtlas, object>> callBack, object returnPara = null,
bool isCallWithNull = false)
{
var dto = new LoadPrefebDTO
{
Path = path,
ReturnPara = returnPara,
CallBackAtlas = callBack,
Type = LoadPrefabTypeEnum.Atlas,
IsCallWithNull = isCallWithNull,
};
/*var a = Resources.Load<SpriteAtlas>(path);
dto.CallBackAtlas(new KeyValuePair<SpriteAtlas, object>(a, dto.ReturnPara));*/
_loadList.Enqueue(dto);
FixedUpdate();
}
public void LoadTexture(string path, Action<KeyValuePair<Texture2D, object>> callBack, object returnPara = null, bool isCallWithNull = false)
{
//Debug.Log("LoadTexture:" + path);
var dto = new LoadPrefebDTO
{
Path = path,
ReturnPara = returnPara,
CallBackTexture = callBack,
Type = LoadPrefabTypeEnum.Texture2D,
IsCallWithNull = isCallWithNull,
};
_loadList.Enqueue(dto);
FixedUpdate();
}
public void LoadAudio(string path, Action<KeyValuePair<AudioClip, object>> callBack, object returnPara = null, bool isCallWithNull = false)
{
//Debug.Log("LoadOnePrefeb:" + path);
var dto = new LoadPrefebDTO
{
Path = path,
ReturnPara = returnPara,
CallBackAudio = callBack,
Type = LoadPrefabTypeEnum.Audio,
IsCallWithNull = isCallWithNull,
};
_loadList.Enqueue(dto);
FixedUpdate();
}
void FixedUpdate()
{
lock (_lockStr)
{
if (!_isWaitting && _loadList.Count > 0)
{
_isWaitting = true;
StartCoroutine(LoadPrefabs());
}
}
}
private IEnumerator LoadPrefabs()
{
StartCoroutine(LoadOnePrefeb(_loadList.Dequeue()));
yield return 0;
}
private IEnumerator LoadOnePrefeb(LoadPrefebDTO dto)
{
//Debug.Log("LoadOnePrefeb:" + dto.Path);
Type loadType;
switch (dto.Type)
{
case LoadPrefabTypeEnum.GameObject:
loadType = typeof(GameObject);
break;
case LoadPrefabTypeEnum.Audio:
loadType = typeof(AudioClip);
break;
case LoadPrefabTypeEnum.SpritePng:
loadType = typeof(Sprite);
break;
case LoadPrefabTypeEnum.Texture2D:
loadType = typeof(Texture2D);
break;
case LoadPrefabTypeEnum.Atlas:
loadType = typeof(SpriteAtlas);
break;
default:
loadType = typeof(GameObject);
break;
}
#if UNITY_EDITOR && !DEBUG_ASSETBUNDLE
var r = Resources.LoadAsync(dto.Path, loadType);
#else
var assetBundlePath = Path.Combine(_assetBundleBasePath, dto.Path);
assetBundlePath += ".data";
Debug.Log("Load prefab ab:" + assetBundlePath);
#if UNITY_EDITOR
WWW www = new WWW("file:///" + assetBundlePath);
#elif UNITY_ANDROID
WWW www = new WWW("file://" + assetBundlePath);
#else
WWW www = new WWW("file:///" + assetBundlePath);
#endif
while (!www.isDone && string.IsNullOrEmpty(www.error))
yield return null;
if (!string.IsNullOrEmpty(www.error))
Debug.LogError(www.error);
byte[] prefabAB;
if (ABConfigManager.IsAssetBundleEncrypt)
{
prefabAB = CipherManager.Decrypt(www.bytes, ABConfigManager.EncryptKey, ABConfigManager.EncryptIV,
EnryptType.AES);
}
else
{
prefabAB = www.bytes;
}
var ab = AssetBundle.LoadFromMemoryAsync(prefabAB);
yield return ab;
AssetBundleRequest r = null;
var myLoadedAssetBundle = ab.assetBundle;
if (myLoadedAssetBundle == null && !dto.IsCallWithNull)
{
_isWaitting = false;
Debug.LogError("Failed to load AssetBundle!" + dto.Path);
yield break;
}
else if(myLoadedAssetBundle != null)
{
var fileName = Path.GetFileName(dto.Path);
r = myLoadedAssetBundle.LoadAssetAsync(fileName, loadType);
}
#endif
yield return r;
if (r == null || r.asset == null)
{
if (!dto.IsCallWithNull)
{
_isWaitting = false;
Debug.Log("LoadOnePrefeb文件不存在:" + dto.Path);
yield break;
}
}
object assetObj;
if (r == null)
{
assetObj = null;
}
else
{
assetObj = r.asset;
}
//yield return new WaitForEndOfFrame();
switch (dto.Type)
{
case LoadPrefabTypeEnum.GameObject:
var g = assetObj as GameObject;
try
{
dto.CallBackGameObject(new KeyValuePair<GameObject, object>(g, dto.ReturnPara));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
case LoadPrefabTypeEnum.Audio:
var a = assetObj as AudioClip;
try
{
dto.CallBackAudio(new KeyValuePair<AudioClip, object>(a, dto.ReturnPara));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
case LoadPrefabTypeEnum.SpritePng:
var s = assetObj as Sprite;
try
{
dto.CallBackSprite(new KeyValuePair<Sprite, object>(s, dto.ReturnPara));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
case LoadPrefabTypeEnum.Texture2D:
var t = assetObj as Texture2D;
try
{
dto.CallBackTexture(new KeyValuePair<Texture2D, object>(t, dto.ReturnPara));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
case LoadPrefabTypeEnum.Atlas:
var atlas = assetObj as SpriteAtlas;
try
{
dto.CallBackAtlas(new KeyValuePair<SpriteAtlas, object>(atlas, dto.ReturnPara));
}
catch (Exception e)
{
Debug.LogError(e);
}
break;
}
#if !UNITY_EDITOR || DEBUG_ASSETBUNDLE
if (myLoadedAssetBundle!=null)
{
myLoadedAssetBundle.Unload(false);
}
#endif
//Debug.Log("LoadOnePrefeb:" + dto.Path+" finish");
_isWaitting = false;
}
public void CreateCardPrefab(Action<KeyValuePair<GameObject, object>> action, object returnPara = null)
{
LoadPrefeb("Battle/HandCard/CardInit", action, returnPara);
/* switch (cardTypeEnum)
{
case CardTypeEnum.Attack:
LoadPrefeb(PrefebLoadPath.AttackCardPrefab, action, returnPara);
break;
case CardTypeEnum.Skill:
LoadPrefeb(PrefebLoadPath.SkillCardPrefab, action, returnPara);
break;
case CardTypeEnum.Ability:
LoadPrefeb(PrefebLoadPath.AbilityCardPrefab, action, returnPara);
break;
case CardTypeEnum.Curse:
LoadPrefeb(PrefebLoadPath.CurseCardPrefab, action, returnPara);
break;
case CardTypeEnum.State:
LoadPrefeb(PrefebLoadPath.StateCardPrefab, action, returnPara);
break;
}*/
}
}
public class LoadPrefebDTO
{
public string Path;
public object ReturnPara;
public bool IsCallWithNull;
public Action<KeyValuePair<GameObject, object>> CallBackGameObject;
public Action<KeyValuePair<AudioClip, object>> CallBackAudio;
public Action<KeyValuePair<Sprite, object>> CallBackSprite;
public Action<KeyValuePair<Texture2D, object>> CallBackTexture;
public Action<KeyValuePair<SpriteAtlas, object>> CallBackAtlas;
public LoadPrefabTypeEnum Type;
}
public enum LoadPrefabTypeEnum
{
GameObject,
SpritePng,
SpriteJpg,
Audio,
Texture2D,
Atlas,
}