Unity3D 开发工具系列 UI框架:UI管理UIManager

Unity3D 开发工具系列 UI框架:UI基类UIBase
Unity3D 开发工具系列 UI框架:UI管理UIManager
Unity3D 开发工具系列 UI框架:MVC模式
Unity3D 开发工具系列 UI框架:遮罩管理UIMaskManager
Unity3D 开发工具系列 UI框架:封装接口UIControl
Unity3D 开发工具系列 UI框架:定义设置Defines
Unity3D 开发工具系列 UI框架:案例项目CaraSynthesis

概述

UIManager

代码实现

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

namespace Epitome.UIFrame
{
    public class UIManager : Singleton<UIManager>
    {
        private UIManager() { }

        class UIInfoData
        {
            public string UIType { get; private set; }  // UI类型

            public string UITypeTag { get; private set; } // 当需要加载相同类型时使用

            //public Type ScriptType { get; private set; }

            public string Path { get; private set; }

            public object[] UIParams { get; private set; }

            public UIInfoData(string uiType,string path,params object[] uiParams)
            {
                this.UIType = uiType;
                this.Path = path;
                this.UIParams = uiParams;
                //this.ScriptType = this.GetType();
            }
            public UIInfoData(string uiType, string uiTypeTag, string path, params object[] uiParams)
            {
                this.UIType = uiType;
                this.UITypeTag = uiTypeTag;
                this.Path = path;
                this.UIParams = uiParams;
                //this.ScriptType = this.GetType();
            }
        }

        private Dictionary<string, string> UIPrefab_Paths = null;

        private Dictionary<string, GameObject> UIObject_FatherNodes = null;

        private Dictionary<string, GameObject> UIObject_Pool = null;

        private Dictionary<string, GameObject> UIObject_Pool_Idle = null;

        private Stack<UIInfoData> UIInfoStacks;

        // 可加载多个同类型UI
        private Dictionary<string, List<string>> UIType_Tags = null;


        public override void OnSingletonInit()
        {
            UIPrefab_Paths = new Dictionary<string, string>();
            UIObject_FatherNodes = new Dictionary<string, GameObject>();
            UIObject_Pool = new Dictionary<string, GameObject>();
            UIObject_Pool_Idle = new Dictionary<string, GameObject>();
            UIInfoStacks = new Stack<UIInfoData>();

            UIType_Tags = new Dictionary<string, List<string>>();

            base.OnSingletonInit();
        }

        public TUI GetUI<TUI>(string uiType) where TUI : UIBase
        {
            GameObject tryObj = GetUIObject(uiType);
            if (tryObj != null)
            {
                return tryObj.GetComponent<TUI>();
            }
            return null;
        }
        public GameObject GetUIObject(string uiType)
        {
            GameObject retObj = null;
            if (!UIObject_Pool.TryGetValue(uiType, out retObj))
            {
                throw new Exception("UIObjectPool TryGetValue Failure! uiType:" + uiType);
            }
            return retObj;
        }

        public void PreloadUI(string uiType, string prefabPath)
        {
            PreloadUI(new string[] { uiType }, prefabPath);
        }
        public void PreloadUI(string[] uiTypes, string prefabPath)
        {
            PreloadUI(uiTypes, prefabPath, null);
        }
        public void PreloadUI(string uiType, string prefabPayh, GameObject fatherNode)
        {
            PreloadUI(new string[] { uiType }, prefabPayh, fatherNode);
        }
        public void PreloadUI(string[] uiTypes, string prefabPath, GameObject fatherNode)
        {
            for (int i = 0; i < uiTypes.Length; i++)
            {
                if (UIPrefab_Paths.ContainsKey(uiTypes[i])) continue;

                UIPrefab_Paths.Add(uiTypes[i], prefabPath);
                if (fatherNode != null)
                    UIObject_FatherNodes.Add(uiTypes[i], fatherNode);
            }
        }

        public void OpenUI(string uiType)
        {
            OpenUI(new string[] { uiType });
        }
        public void OpenUI(string[] uiTypes)
        {
            OpenUI(uiTypes, null);
        }
        public void OpenUI(string uiType, params object[] uiParams)
        {
            OpenUI(new string[] { uiType }, uiParams);
        }
        public void OpenUI(string[] uiTypes, params object[] uiParams)
        {
            OpenUI(false, uiTypes, uiParams);
        }
        public void OpenUICloseOthers(string uiType)
        {
            OpenUICloseOthers(new string[] { uiType });
        }
        public void OpenUICloseOthers(string[] uiTypes)
        {
            OpenUICloseOthers(uiTypes, null);
        }
        public void OpenUICloseOthers(string uiType, params object[] uiParams)
        {
            OpenUICloseOthers(new string[] { uiType }, uiParams);
        }
        public void OpenUICloseOthers(string[] uiTypes, params object[] uiParams)
        {
            OpenUI(true, uiTypes, uiParams);
        }

        public void OpenUI(bool isCloseOthers, string[] uiTypes, params object[] uiParams)
        {
            if (isCloseOthers)
            {
                CloseUIAll();
            }

            for (int i = 0; i < uiTypes.Length; i++)
            {
                string uiType = uiTypes[i];

                GameObject uiObj = null;

                if (!UIObject_Pool.TryGetValue(uiType, out uiObj))
                {
                    string path = null;
                    if (!UIPrefab_Paths.TryGetValue(uiType, out path))
                    {
                        throw new Exception(string.Format("Error:on {0} type ui path null", uiType));
                    }
                    else
                    {
                        UIInfoStacks.Push(new UIInfoData(uiType, path, uiParams));
                    }
                }
                else
                {
                    UIObject_Pool_Idle.TryGetValue(uiType, out uiObj);
                    if (uiObj == null) continue;

                    uiObj.SetActive(true);

                    UIBase ui = uiObj.GetComponent<UIBase>();

                    if (ui != null)
                    {
                        ui.SetUIWhenOpening(uiParams);
                        ui.Display();
                    }

                    UIObject_Pool_Idle.Remove(uiType);
                }
            }

            if (UIInfoStacks.Count > 0)
            {
                new Task(AsyncLoadData());
            }
        }

        public void OpenSameKindUI(string uiType, string tag, params object[] uiParams)
        {
            string type = string.Format("{0}_{1}", uiType, tag);

            GameObject uiObj = null;

            if (!UIObject_Pool.TryGetValue(type, out uiObj))
            {
                string path = null;
                if (!UIPrefab_Paths.TryGetValue(uiType, out path))
                {
                    throw new Exception(string.Format("Error:on {0} type ui path null", uiType));
                }
                else
                {
                    UIInfoStacks.Push(new UIInfoData(uiType, tag, path, uiParams));
                }
            }
            else
            {
                UIObject_Pool_Idle.TryGetValue(type, out uiObj);
                if (uiObj == null) return;

                uiObj.SetActive(true);

                UIBase ui = uiObj.GetComponent<UIBase>();

                if (ui != null)
                {
                    ui.SetUIWhenOpening(uiParams);
                    ui.Display();
                }

                UIObject_Pool_Idle.Remove(type);
            }

            if (UIInfoStacks.Count > 0)
            {
                new Task(AsyncLoadData());
            }
        }
        public void CloseSameKindUI(string uiType, string tag)
        {
            CloseUI(string.Format("{0}_{1}", uiType, tag));
        }
        private IEnumerator<int> AsyncLoadData()
        {
            UIInfoData uiInfoData = null;
            UnityEngine.Object prefabObj = null;
            GameObject uiObject = null;

            if (UIInfoStacks != null && UIInfoStacks.Count > 0)
            {
                do
                {
                    uiInfoData = UIInfoStacks.Pop();
                    prefabObj = ResManager.Instance.Load(uiInfoData.Path);

                    if (prefabObj != null)
                    {
                        uiObject = ResManager.Instance.Instantiate(prefabObj) as GameObject;

                        string name = "";
                        if (uiInfoData.UITypeTag == null)
                        {
                            name = uiInfoData.UIType;
                        }
                        else
                        {
                            //List<string> tags;

                            //if (!UIType_Tags.TryGetValue(uiInfoData.UIType, out tags))
                            //    tags = new List<string>();

                            //tags.Add(uiInfoData.UITypeTag);
                            //UIType_Tags.Add(uiInfoData.UIType, tags);

                            name = string.Format("{0}_{1}", uiInfoData.UIType, uiInfoData.UITypeTag);
                        }

                        uiObject.name = name;

                        GameObject fatherNode = null;
                        UIObject_FatherNodes.TryGetValue(uiInfoData.UIType, out fatherNode);
                        if (fatherNode != null)
                        {
                            uiObject.transform.SetParent(fatherNode.transform, false);
                        }
                     
                        UIBase ui = uiObject.GetComponent<UIBase>();

                        if (ui == null)
                        {
                            //ui = uiObject.AddComponent(uiInfoData.ScriptType) as UIBase;
                            throw new Exception("Error:On GetComponent<UIBase> in Instantiate GameObject is null!");
                        }

                        if (ui != null)
                        {
                            ui.SetUIWhenOpening(uiInfoData.UIParams);
                            ui.Display();
                        }

                        UIObject_Pool.Add(name, uiObject);
                    }
                } while (UIInfoStacks.Count > 0);
            }

            yield return 0;
        }



        public void CloseUI(string uiType)
        {
            GameObject uiObj = null;

            if (!UIObject_Pool.TryGetValue(uiType,out uiObj))
            {
                Debug.Log(string.Format("on {0} type UI GameObject is null", uiType.ToString()));
            }

            CloseUI(uiType, uiObj);
        }
        public void CloseUI(string[] uiTypes)
        {
            for (int i = 0; i < uiTypes.Length; i++)
            {
                CloseUI(uiTypes[i]);
            }
        }
        public void CloseUIAll()
        {
            List<string> keyList = new List<string>(UIObject_Pool.Keys.Except(UIObject_Pool_Idle.Keys));
            for (int i = 0; i < keyList.Count; i++)
            {
                Debug.Log("CloseUIAll:" + keyList[i]);
                GameObject uiObj = UIObject_Pool[keyList[i]];
                CloseUI(keyList[i], uiObj);
            }
        }

        public void CloseUI(string uiType,GameObject uiObj)
        {
            if (uiObj == null)
            {
                UIObject_Pool.Remove(uiType);
            }
            else
            {
                UIBase ui = uiObj.GetComponent<UIBase>();
                if (ui != null)
                {
                    ui.Hiding();

                    if (!UIObject_Pool_Idle.ContainsKey(uiType))
                        UIObject_Pool_Idle.Add(uiType, ui.gameObject);
                }
                else
                {
                    GameObject.Destroy(uiObj);
                    UIObject_Pool.Remove(uiType);
                }
            }
        }

        public void CloseUIHandler(object sender,ObjectState newState,ObjectState oldState)
        {
            UIBase ui = sender as UIBase;
            switch (newState)
            {
                case ObjectState.Disabled:
                    Debug.Log("UIObject_Pool_Idle");
                    UIObject_Pool_Idle.Add(ui.gameObject.name, ui.gameObject);
                    ui.StateChanged -= CloseUIHandler;
                    break;
                case ObjectState.Closing:
                    UIObject_Pool.Remove(ui.GetUIType());
                    ui.StateChanged -= CloseUIHandler;
                    break;
            }
        }

        private void OverflowClearing()
        {
            if (UIObject_Pool_Idle.Count >= 10)
            {
                for (int i = 10; i < UIObject_Pool_Idle.Keys.Count; i++)
                {
                    string key = UIObject_Pool_Idle.ElementAt(i).Key;

                    UIBase ui = UIObject_Pool_Idle[key].GetComponent<UIBase>();
                    if (ui != null)
                    {
                        ui.StateChanged += CloseUIHandler;
                        ui.Release();
                    }
                    else
                    {
                        GameObject.Destroy(ui);
                        UIObject_Pool.Remove(key);
                        UIObject_Pool_Idle.Remove(key);
                    }
                }
            }
        }
    }
}
  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
你可以使用Unity3DUI系统来显示信息。首先,你需要创建一个UI元素,例如Text或Image,来显示文本或图像信息。然后,你可以通过脚本来更新UI元素的内容。 下面是一个简单的示例代码,演示如何在UI中显示信息: ```csharp using UnityEngine; using UnityEngine.UI; public class UIManager : MonoBehaviour { public Text infoText; // UI文本元素 // 更新UI文本内容 public void UpdateInfoText(string text) { infoText.text = text; } } ``` 在上面的代码中,我们创建了一个名为UIManager的脚本,并在其中声明了一个名为infoText的Text类型的公共变量。然后,我们定义了一个名为UpdateInfoText的公共方法,用于更新UI文本内容。 接下来,你可以将这个UIManager脚本附加到一个空物体上,并将UI文本元素指定给infoText变量。然后,在其他脚本中,你可以通过获取UIManager实例并调用UpdateInfoText方法来更新UI文本内容。 例如,在另一个脚本中: ```csharp using UnityEngine; public class GameLogic : MonoBehaviour { private UIManager uiManager; private void Start() { uiManager = FindObjectOfType<UIManager>(); // 获取UIManager实例 } private void Update() { // 假设有一些需要显示的信息 string info = "当前分数:" + ScoreManager.Instance.GetScore(); // 更新UI文本内容 uiManager.UpdateInfoText(info); } } ``` 在上面的代码中,我们使用FindObjectOfType方法获取UIManager实例,并在Update方法中调用UpdateInfoText方法来更新UI文本内容。 这样,你就可以在Unity3D中使用UI系统来显示信息了。记得在场景中创建相应的UI元素,并将其连接到UIManager脚本中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JIQIU.YANG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值