using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ManageAssets : MonoBehaviour
{
private static ManageAssets instance = null;
public static ManageAssets Instace() { return instance; }
private string url;
private string manifestName;
private Dictionary<string, Object> dicAsset = new Dictionary<string, Object>();
private Dictionary<string, WWW> dicLoadingReq = new Dictionary<string, WWW>();
void Awake()
{
instance = this;
}
void Start()
{
InitializeAssetdataPath();
}
public void Generate(GameObject root)
{
float startTime = Time.realtimeSinceStartup;
List<CombineInstance> combineInstances = new List<CombineInstance>();
List<Material> materials = new List<Material>();
List<Transform> bones = new List<Transform>();
Transform[] transforms = root.GetComponentsInChildren<Transform>();
foreach (SkinnedMeshRenderer smr in root.GetComponentsInChildren<SkinnedMeshRenderer>(true))
{
if (!smr.name.Contains("charactorbase"))
{
foreach (Transform bone in smr.bones)
{
foreach (Transform transform in transforms)
{
if (transform.name != bone.name) continue;
bones.Add(transform);
break;
}
}
CombineInstance ci = new CombineInstance();
ci.mesh = smr.sharedMesh;
combineInstances.Add(ci);
materials.AddRange(smr.materials);
Object.Destroy(smr.gameObject);
}
}
SkinnedMeshRenderer r = root.GetComponent<SkinnedMeshRenderer>();
r.sharedMesh = new Mesh();
r.sharedMesh.CombineMeshes(combineInstances.ToArray(), false, false);
r.materials = materials.ToArray();
r.bones = bones.ToArray();
}
public bool CongigReady(string[] sSps)
{
for (int i = 0; i < sSps.Length; i++)
{
if (!IsResLoaded(sSps[i].Trim()))
{
return false;
}
}
return true;
}
public Object GetResource(string name)
{
Object obj = null;
if (dicAsset.TryGetValue(name, out obj) == false)
{
Debug.Log("<GetResource Failed> Res not exist, res.Name = " + name);
if (dicLoadingReq.ContainsKey(name))
{
Debug.Log("<GetResource Failed> The res is still loading");
}
}
return obj;
}
public void LoadAsync(string name)
{
LoadAsync(name, typeof(GameObject));
}
public void LoadAsync(string name, System.Type type)
{
if (dicAsset.ContainsKey(name))
return;
if (dicLoadingReq.ContainsKey(name))
return;
StartCoroutine(AsyncLoadCoroutine(name, type));
}
private IEnumerator AsyncLoadCoroutine(string name, System.Type types)
{
string assetBundleName = name + ".assetbundle";
WWW www = new WWW(url + assetBundleName);
dicLoadingReq.Add(name, www);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
goPath.text = www.error;
Debug.Log(www.error);
}
else
{
AssetBundle ab = www.assetBundle;
foreach (string n in ab.GetAllAssetNames())
{
AssetBundleRequest req = ab.LoadAssetAsync(n, types);
yield return req;
dicAsset.Add(name, req.asset);
dicLoadingReq.Remove(name);
}
ab.Unload(false);
}
www.Dispose();
}
public bool IsResLoading(string name)
{
return dicLoadingReq.ContainsKey(name);
}
public bool IsResLoaded(string name)
{
if (name != "")
{
return dicAsset.ContainsKey(name);
}
else
{
return true;
}
}
void InitializeAssetdataPath()
{
#if UNITY_EDITOR
url = "file://" + Application.streamingAssetsPath + "/assetbundles/Windows/";
manifestName = "Windows";
#elif UNITY_ANDROID
url = Application.streamingAssetsPath + "/assetbundles/Android/";
manifestName ="assetbundles";
#endif
}
}