using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using UnityEngine.UI;
using LogPrint;
public delegate void SceneAssetLoadComplete(GameObject go);
public class ResourceManager
{
public class CacheAssetRule
{
//从AB包加载出的资源
public UnityEngine.Object AssetObject;
//AB包
public AssetBundle Bundle;
}
public SceneAssetLoadComplete SceneAssetCallBack;
//场景AB资源缓存
public Dictionary<string, CacheAssetRule> CacheAssetDic = new Dictionary<string, CacheAssetRule>();
//其他AB资源缓存
public Dictionary<string, CacheAssetRule> OtherCacheAssetDic = new Dictionary<string, CacheAssetRule>();
//本地资源缓存
public Dictionary<string, UnityEngine.Object> LocalCacheAssetDic = new Dictionary<string, UnityEngine.Object>();
//图片缓存
private Dictionary<string, Sprite> _dicTexture = new Dictionary<string, Sprite>();
public string platform = null;
public string Platform
{
get
{
return platform;
}
}
private static ResourceManager m_instance;
public static ResourceManager Instance
{
get
{
if (m_instance == null)
{
m_instance = new ResourceManager();
}
return m_instance;
}
}
public string LoadTxt(string path)
{
string m_txtmes = "";
var M_txtAS= Resources.Load<TextAsset>(path);
if (M_txtAS != null)
{
m_txtmes = M_txtAS.text;
}
else
LogSystem.LogError("this path is erorr:" + path);
return m_txtmes;
}
/// <summary>
/// 加载场景资源
/// </summary>
/// <param name="sceneName">场景名</param>
/// <param name="resName">资源名</param>
/// <param name="type">资源类型</param>
/// <returns></returns>
public UnityEngine.Object LoadSceneAsset(string sceneName, string resName)
{
UnityEngine.Object prefabObj = null;
string bundleName = sceneName + "_Prefab";
#if UNITY_EDITOR
if (SceneDataManager.Instance.SceneLocalName.Contains(sceneName))//模擬進入本地场景
{
prefabObj = Resources.Load(“LocalScene/ScenesPrefab” + “/” + bundleName + “/” + resName);
}
else { prefabObj = Resources.Load(“TestScene” + “/” + bundleName + “/” + resName); }
#else
if (SceneDataManager.Instance.SceneLocalName.Contains(sceneName))
{
prefabObj = Resources.Load(“LocalScene/ScenesPrefab” + “/” + bundleName + “/” + resName);
}
else
{
bundleName = bundleName.ToLower();
string path = AssetBundlePath();
//有缓存
if (CacheAssetDic.ContainsKey(resName))
{
return CacheAssetDic[resName].AssetObject;
}
//加载AB
AssetBundle bundle = HasBundle(bundleName);
if (bundle == null)
{
bundle = AssetBundle.LoadFromFile(path + platform + "/" + bundleName);
}
prefabObj = bundle.LoadAsset(resName);
if (prefabObj == null)
{
return null;
}
//缓存对象
if (!CacheAssetDic.ContainsKey(resName))
{
CacheAssetRule rule = new CacheAssetRule();
rule.AssetObject = prefabObj;
rule.Bundle = bundle;
CacheAssetDic.Add(resName, rule);
}
}
#endif
return prefabObj;
}
//加载声音资源
public void LoadAudioAssets(string sceneName)
{
string bundleName = (sceneName + "_Sound").ToLower();
#if UNITY_EDITOR
if (SceneDataManager.Instance.SceneLocalName.Contains(sceneName))
{
ResLoadAudioEdtor(bundleName);
}
else { GameDefault.Instance.StartCoroutine(LoadAudioEdtor(bundleName)); }
#else
if (SceneDataManager.Instance.SceneLocalName.Contains(sceneName))
{
bundleName = (sceneName + "_Sound");
ResLoadAudioEdtor(bundleName);
}
else
{
//LoadAudioEdtor(bundleName);
string path = AssetBundlePath();
// Start
//加载AB
AssetBundle bundle = HasBundle(bundleName);
if (bundle == null)
{
bundle = AssetBundle.LoadFromFile(path + platform + "/" + bundleName);
}
UnityEngine.Object[] obj = bundle.LoadAllAssets();
for (int i = 0; i < obj.Length; ++i)
{
//缓存对象
if (CacheAssetDic.ContainsKey(obj[i].name))
{
continue;
}
CacheAssetRule rule = new CacheAssetRule();
rule.AssetObject = obj[i];
rule.Bundle = bundle;
CacheAssetDic.Add(obj[i].name, rule);
}
}
#endif
}
///
/// 加载本地音效
///
///
///
///
IEnumerator LoadAudioEdtor(string str)
{
string path = string.Empty;
path = Application.dataPath + “/Sound/” + str;
string fullPath = path; //路径
//获取指定路径下面的所有资源文件
if (Directory.Exists(fullPath))
{
DirectoryInfo direction = new DirectoryInfo(fullPath);
FileInfo[] files = direction.GetFiles("*", SearchOption.AllDirectories);
for (int i = 0; i < files.Length; i++)
{
if (files[i].Name.EndsWith(".meta"))
{
continue;
}
WWW www = new WWW(files[i].FullName);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log("www.error.....>>" + www.error);
}
AudioClip clip= www.GetAudioClip();
clip.name = files[i].Name.Split('.')[0];
AddDicAudioClip(clip.name, clip);
}
}
}
/// <summary>
/// 加载 游客本地场景的 音效
/// </summary>
/// <param name="str">场景名字</param>
void ResLoadAudioEdtor(string str)
{
string fullPath = string.Empty;
fullPath = Application.dataPath + "/Resources/LocalScene/Sound/" + str;
UnityEngine.Object[] m_allClip= Resources.LoadAll("LocalScene/Sound/" + str) ;
if (m_allClip.Length > 0)
{
for (int i = 0; i < m_allClip.Length; i++)
{
AudioClip clip = m_allClip[i] as AudioClip;
string str2 = clip.name.Split('.')[0];
AddDicAudioClip(clip.name, clip);
}
}
else
{
LogSystem.Log("ResourceManager.ResLoadAudioEdtor.... m_allClip.Length==0>>>" + fullPath , LogColor.red);
}
}
//加载其他AB资源
public UnityEngine.Object LoadOtherAsset(string bundleName, string resName)
{
UnityEngine.Object prefabObj = null;
//有缓存
if (OtherCacheAssetDic.ContainsKey(resName))
{
return OtherCacheAssetDic[resName].AssetObject;
}
string path = AssetBundlePath();
string bundleLower = bundleName.ToLower();
//加载AB
AssetBundle bundle = HasOtherBundle(bundleLower);
if (bundle == null)
{
Debug.Log(resName);
bundle = AssetBundle.LoadFromFile(path + platform + "/" + bundleLower);
}
prefabObj = bundle.LoadAsset(resName);
if (prefabObj == null)
{
return null;
}
//缓存对象
if (!OtherCacheAssetDic.ContainsKey(resName))
{
CacheAssetRule rule = new CacheAssetRule();
rule.AssetObject = prefabObj;
rule.Bundle = bundle;
OtherCacheAssetDic.Add(resName, rule);
}
return prefabObj;
}
//加载本地资源
public UnityEngine.Object Load(string path)
{
if (path.Contains("Pub_StudyCard")
|| path.Contains("Pub_Debris")
|| path.Contains("Pub_NewDebris"))
{
ClientDataCollection.Instance.AddHandle("", "Scene_010");
}
if (LocalCacheAssetDic.ContainsKey(path))
{
if (LocalCacheAssetDic[path] == null)
{
LocalCacheAssetDic[path] = Resources.Load(path);
}
return LocalCacheAssetDic[path];
}
UnityEngine.Object obj = Resources.Load(path);
if (obj == null)
{
LogSystem.LogError(path + "can not find resource");
}
LocalCacheAssetDic.Add(path, obj);
return obj;
}
#region
public void TextureLoad(string path, string name, Image showImage)
{
string benpath = Application.persistentDataPath + "/" + name + ".png";
if (_dicTexture.ContainsKey(name))
{
showImage.sprite = _dicTexture[name];
return;
}
if (File.Exists(benpath))
{
if (_dicTexture.ContainsKey(name))
{
showImage.sprite = _dicTexture[name];
}
else
{
FileStream fs = new FileStream(benpath, FileMode.Open);
long length = fs.Length;
byte[] data = new byte[length];
fs.Read(data, 0, (int)length);
Texture2D tex = new Texture2D(256, 256);
tex.LoadImage(data);
Sprite _sprit = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
showImage.sprite = _sprit;
fs.Close();
_dicTexture.Add(name, _sprit);
}
return;
}
NetPostManager.Instance.NetLoad(path, P =>
{
Texture2D te = P as Texture2D;
Sprite _sprit = Sprite.Create(te, new Rect(0, 0, te.width, te.height), new Vector2(0.5f, 0.5f));
showImage.sprite = _sprit;
_dicTexture.Add(name, _sprit);
GameDefault.Instance.StartCoroutine(WriteTexture(te, name));
});
}
IEnumerator WriteTexture(Texture2D text, string textname)
{
byte[] data = text.EncodeToPNG();
FileStream fs = new FileStream(Application.persistentDataPath + "/" + textname + ".png", FileMode.OpenOrCreate);
fs.Write(data, 0, data.Length);
fs.Close();
yield return null;
}
#endregion
//卸载资源
public void UnloadAssets()
{
IEnumerator<KeyValuePair<string, CacheAssetRule>> cacheIE = CacheAssetDic.GetEnumerator();
while (cacheIE.MoveNext())
{
AssetBundle bundle = cacheIE.Current.Value.Bundle;
if (bundle != null)
{
bundle.Unload(true);
}
}
CacheAssetDic.Clear();
}
public void UnloadOtherAssets()
{
IEnumerator<KeyValuePair<string, CacheAssetRule>> cacheIE = OtherCacheAssetDic.GetEnumerator();
if (cacheIE == null)
{
return;
}
while (cacheIE.MoveNext())
{
AssetBundle bundle = cacheIE.Current.Value.Bundle;
if (bundle != null)
{
bundle.Unload(true);
}
}
OtherCacheAssetDic.Clear();
}
public string AssetBundlePath()
{
string path = string.Empty;
switch (Application.platform)
{
case RuntimePlatform.Android:
platform = "Android";
path = Application.persistentDataPath + "/StreamingAssets/";
break;
case RuntimePlatform.IPhonePlayer:
platform = "IOS";
path = Application.persistentDataPath + "/StreamingAssets/";
break;
default:
platform = "Android";
path = GameDefault.Instance.Debug ? Application.streamingAssetsPath + "/"
: Application.persistentDataPath + "/StreamingAssets/";
break;
}
return path;
}
private AssetBundle HasBundle(string bundleName)
{
IEnumerator<KeyValuePair<string, CacheAssetRule>> enumerator = CacheAssetDic.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current.Value.Bundle.name == bundleName)
{
return enumerator.Current.Value.Bundle;
}
}
return null;
}
private AssetBundle HasOtherBundle(string bundleName)
{
IEnumerator<KeyValuePair<string, CacheAssetRule>> enumerator = OtherCacheAssetDic.GetEnumerator();
while (enumerator.MoveNext())
{
if (enumerator.Current.Value.Bundle.name == bundleName)
{
return enumerator.Current.Value.Bundle;
}
}
return null;
}
public void AddDicAudioClip(string str, AudioClip Clip)
{
CacheAssetRule cache = new CacheAssetRule();
cache.AssetObject = Clip as UnityEngine.Object;
CacheAssetDic.Add(str, cache);
}
}