using System.Collections;
using System;
public class Singleton<T> :IDisposable where T : new()
{
private static T instance;
public static T Instance
{
get {
if (instance == null)
{
instance=new T();
}
return instance;
}
}
public virtual void Dispose()
{
}
}
using UnityEngine;
using System.Collections;
using System.Text;
public class ResourceManager : Singleton<ResourceManager>
{
public enum ResourceType //资源类型
{
UIScene, //场景UI
UIWindow, //窗口
Role, //角色
Effect, //特效
UIOther //其他
}
private Hashtable ht; //预设的列表
public ResourceManager()
{
ht = new Hashtable();
}
#region Load 加载资源
/// <summary>
/// 加载资源
/// </summary>
/// <param name="type">资源类型</param>
/// <param name="path">短路径</param>
/// <param name="cache">是否放入缓存</param>
/// <returns>预设克隆体</returns>
public GameObject Load(ResourceType type, string path, bool cache)
{
GameObject obj = null;
if (ht.Contains(path))
{
obj = ht[path] as GameObject;
}
else
{
StringBuilder sbu = new StringBuilder();
switch (type)
{
case ResourceType.UIScene:
sbu.Append("UIPrefab/UIScene/");
break;
case ResourceType.UIWindow:
sbu.Append("UIPrefab/UIWindow/");
break;
case ResourceType.Role:
sbu.Append("RolePrefab/");
break;
case ResourceType.Effect:
sbu.Append("EffectPrefab/");
break;
case ResourceType.UIOther:
sbu.Append("UIPrefab/UIOther/");
break;
}
sbu.Append(path);
obj = Resources.Load(sbu.ToString()) as GameObject;
if (cache)
{
ht.Add(path, obj);
}
}
return GameObject.Instantiate(obj) as GameObject;
}
#endregion
#region Dispose 释放资源
/// <summary>
/// 释放资源
/// </summary>
public override void Dispose()
{
base.Dispose();
ht.Clear();
//把未使用的资源进行释放
Resources.UnloadUnusedAssets();
}
#endregion
}
using UnityEngine;
using System.Collections;
public class SceneUIManager : Singleton<SceneUIManager>
{
public enum SceneType {
Login,
Loading,
City
}
public GameObject LoadScene(SceneType type) {
GameObject obj=null;
switch (type) {
case SceneType.Login:
obj= ResourceManager.Instance.Load(ResourceManager.ResourceType.UIScene, "UIRoot", false);
break;
case SceneType.Loading:
break;
case SceneType.City:
break;
}
return obj;
}
}
using UnityEngine;
using System.Collections;
public class LoginSceneCtl : MonoBehaviour {
GameObject obj;
void Start()
{
//obj = ResourceManager.Instance.Load(ResourceManager.ResourceType.UIScene, "UIRoot", true);
SceneUIManager.Instance.LoadScene(SceneUIManager.SceneType.Login);
}
//void Update () {
// if(Input.GetKeyUp(KeyCode.D)){
// Destroy(obj);
// }
// else if (Input.GetKeyUp(KeyCode.C)) {
// obj = ResourceManager.Instance.Load(ResourceManager.ResourceType.UIScene, "UIRoot", true);
// }
//}
}
using UnityEngine;
using System.Collections;
public class WindowUIManager :Singleton<WindowUIManager>
{
public enum WindowType //窗口类型
{
Login, //登录窗口
Reg //注册窗口
}
#region LoadWindow 加载窗口
/// <summary>
/// 加载窗口
/// </summary>
/// <param name="type">资源类型</param>
/// <returns>预设克隆体</returns>
public GameObject LoadWindow(WindowType type)
{
GameObject obj = null;
switch (type)
{
case WindowType.Login:
//窗口需要加载缓存
obj = ResourceManager.Instance.Load(ResourceManager.ResourceType.UIWindow, "Login", true);
break;
case WindowType.Reg:
obj = ResourceManager.Instance.Load(ResourceManager.ResourceType.UIWindow, "Reg", true);
break;
}
return obj;
}
#endregion
}
using UnityEngine;
using System.Collections;
public class UISceneLoginManager : MonoBehaviour
{
public Transform container;
void Start()
{
GameObject go = WindowUIManager.Instance.LoadWindow(WindowUIManager.WindowType.Login);
go.transform.parent = container;
go.transform.localPosition = Vector3.zero;
go.transform.localScale = Vector3.one;
}
void Update()
{
}
}