using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PrefabMgr
{
	private Dictionary<string, Object> m_PrefabData;

	private IList<GameObject> m_AddedGameObj = new List<GameObject>();

	public PrefabMgr()
	{
		m_PrefabData = new Dictionary<string, Object>();
	}

	public void Load(string str)
	{
		if (!m_PrefabData.ContainsKey(str) && !string.IsNullOrEmpty(str))
		{
			m_PrefabData.Add(str, Resources.Load(str));
		}
	}

	public IEnumerator LoadAync(string str)
	{
		if (!m_PrefabData.ContainsKey(str) && !string.IsNullOrEmpty(str))
		{
			ResourceRequest loadRequest = Resources.LoadAsync(str);
			while (!loadRequest.isDone)
			{
				yield return null;
			}
			Object asset = loadRequest.asset;
			m_PrefabData.Add(str, asset);
		}
	}

	public void UnLoad(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			m_PrefabData.Remove(str);
			Resources.UnloadUnusedAssets();
		}
	}

	public void AllUnLoad()
	{
		m_PrefabData.Clear();
		Resources.UnloadUnusedAssets();
	}

	public GameObject CreateIns(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return Object.Instantiate(m_PrefabData[str]) as GameObject;
		}
		return null;
	}

	public GameObject Get(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return (GameObject)m_PrefabData[str];
		}
		return null;
	}

	public Texture GetTexture(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return (Texture)m_PrefabData[str];
		}
		return null;
	}

	public Sprite GetSprite(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return (Sprite)m_PrefabData[str];
		}
		return null;
	}

	public AudioClip GetAudio(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return (AudioClip)m_PrefabData[str];
		}
		return null;
	}

	public Object GetObj(string str)
	{
		if (m_PrefabData.ContainsKey(str))
		{
			return m_PrefabData[str];
		}
		return null;
	}

	public void DebugDraw()
	{
		foreach (string key in m_PrefabData.Keys)
		{
			_ = key;
		}
	}

	public Dictionary<string, Object> GetPrefabData()
	{
		return m_PrefabData;
	}

	public void DisposeAllClonedObject()
	{
		for (int i = 0; i < m_AddedGameObj.Count; i++)
		{
			Object.DestroyImmediate(m_AddedGameObj[i]);
			m_AddedGameObj[i] = null;
		}
		m_AddedGameObj.Clear();
	}

	public GameObject CloneObjectToParent(string str, GameObject parentObject)
	{
		GameObject gameObject = NGUITools.AddChild(parentObject, GameMgr.GetIns().GetPrefabMgr().Get(str));
		m_AddedGameObj.Add(gameObject);
		return gameObject;
	}

	public GameObject CloneObjectToParent(GameObject gobj, GameObject parentObject)
	{
		GameObject gameObject = NGUITools.AddChild(parentObject, gobj);
		m_AddedGameObj.Add(gameObject);
		return gameObject;
	}
}
