一个不知道对不对的unity C# ui管理器

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值