using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
//using SingLeton;
public class OpenData
{
}
public class UIManager : MonoSingleton<UIManager>
{
public class Info
{
public string Name;
public string Path;
public object[] Objparams;
public OpenData _OpenData;
public Info(string name, string path, OpenData openData, object[] uiobjparams)
{
Name = name;
Path = path;
_OpenData = openData;
Objparams = uiobjparams;
}
}
private static string uiPanelPath = "UIPanel/";
//Dictionary<string, GameObject> allOpenUIObjects = new Dictionary<string, GameObject>();
List<GameObject> allOpenUIObject = new List<GameObject>();
Stack<Info> UIStack = new Stack<Info>();
public int nextCanvasSortingOrder = -1;
public void OpenUI(string name)
{
this.OpenUI(name, uiPanelPath, null, null);
}
public void OpenUI(string name, OpenData openData = null)
{
this.OpenUI(name, uiPanelPath, openData, null);
}
public void OpenUI(string name, string path, OpenData openData, params object[] uiObjparams)
{
//if (!allOpenUIObjects.ContainsKey(name))
//{
UIStack.Push(new Info(name, path + name, openData, uiObjparams));
//}
AynccallBackOpenUI();
}
void AynccallBackOpenUI()
{
Info uiinfo = null;
UnityEngine.Object prefab = null;
GameObject uiObject = null;
if (UIStack != null && UIStack.Count > 0)
{
// foreach (var info in UIStack)
// {
//移除并返回位于stack顶部的对象 后进先出
uiinfo = UIStack.Pop();
prefab = Resources.Load(uiinfo.Path) as UnityEngine.Object;
if (prefab == null)
{
Debug.LogError(string.Format("null name+{0},path+{1}", uiinfo.Name, uiinfo.Path));
}
nextCanvasSortingOrder++;
uiObject = GameObject.Instantiate(prefab, GameManager.Instance.UIRoot) as GameObject;
//uiObject.GetComponent<RectTransform>().SetSiblingIndex(nextCanvasSortingOrder);
// if (uiObject.GetComponent<type>())
Caller.CallMethod(uiObject, "OnOpen", uiinfo._OpenData);
// allOpenUIObjects.Add(name, uiObject);
allOpenUIObject.Add(uiObject);
// }
}
}
UnityEngine.Object getUiObjectResource(string path)
{
string[] names = path.Split('/');
// if (allOpenUIObjects.ContainsKey(names[names.Length - 1]))
// return allOpenUIObjects[names[names.Length - 1]];
return Resources.Load(path) as UnityEngine.Object;
}
public void CloseUI()
{
//Info info = UIStack.Pop();
//GameObject uiObject = null;
if (allOpenUIObject.Count <= 0) return;
//uiObject = allOpenUIObject[allOpenUIObject.Count - 1];
GameObject.DestroyImmediate(allOpenUIObject[allOpenUIObject.Count - 1]);
allOpenUIObject.RemoveAt(allOpenUIObject.Count - 1);
nextCanvasSortingOrder--;
}
}
public static class Caller
{
public static void CallMethod(GameObject obj, string funcname, params object[] args)
{
Component[] comps = obj.GetComponents<Component>();
for (int i = 0; i < comps.Length; i++)
{
if (Invok(GetMethods(comps[i].GetType(), funcname), comps[i], args))
{
break;
}
}
}
static bool Invok(List<MethodInfo> method, System.Object obj, System.Object[] args)
{
for (int i = 0; i < method.Count; i++)
{
if (InvokMethod(method[i], obj, args))
{
return true;
}
}
return false;
}
static bool InvokMethod(MethodInfo method, System.Object obj, System.Object[] args)
{
method.Invoke(obj, args);
return true;
}
static List<MethodInfo> GetMethods(System.Type type, string func_name, BindingFlags flags = BindingFlags.Public | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static)
{
List<MethodInfo> methods = new List<MethodInfo>();
MethodInfo[] ms = type.GetMethods(flags);
for (int i = 0; i < ms.Length; i++)
{
if (ms[i].Name == func_name)
{
methods.Add(ms[i]);
}
}
return methods;
}
}
#region 单例的基类,继承就可实现包括带Monobehaviour的
public class Singleton<T> where T : new()
{
private static T _instance;
public static T Instance
{
get
{
if (_instance == null)
_instance = new T();
return _instance;
}
}
}
public class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
static string MonoSingletonName = "MonoSingletonNameRoot";
static GameObject MonoSingletonNameRoot;
static T _instance;
public static T Instance
{
get
{
if (MonoSingletonNameRoot == null)
{
MonoSingletonNameRoot = GameObject.Find(MonoSingletonName);
if (MonoSingletonNameRoot == null)
{
MonoSingletonNameRoot = new GameObject
{
name = MonoSingletonName
};
DontDestroyOnLoad(MonoSingletonNameRoot);
}
}
if (_instance == null)
{
_instance = MonoSingletonNameRoot.GetComponent<T>();
if (_instance == null)
{
_instance = MonoSingletonNameRoot.AddComponent<T>();
MonoSingletonNameRoot.name = _instance.name;
}
}
return _instance;
}
}
}
#endregion