游戏蛮牛学习群(纯技术交流,不闲聊):539178957
游戏蛮牛 手机端
开启辅助访问
 找回密码
 注册帐号

扫一扫,访问微社区

首页   >   博客   >   UnityTerminator

游戏框架-UI管理器 热度 3

个人分类: MMO游戏设计框架 | 2018-7-5 20:15
标签:UIManager
4 个评论 | 阅读 138 | 收藏 | 举报

[code]csharpcode:

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

/// <summary>
/// 所有界面的基类
/// </summary>
public abstract class BaseWnd
{
    protected Transform _transform;

    /// <summary>
    /// 打开窗口
    /// </summary>
    /// <param name="canvas"></param>
    /// <param name="wndName"></param>
    public void Open(Transform canvas,string wndName)
    {
        _transform = UnityEngine.Object.Instantiate(Resources.Load("UI/" + wndName) as GameObject).transform;
        _transform.parent = canvas;
        _transform.localPosition = Vector3.zero;
        _transform.localScale = Vector3.one;
        _transform.name = wndName;
    }

    /// <summary>
    /// 关闭窗口
    /// </summary>
    public virtual void Close()
    {
        if(_transform != null && _transform.gameObject != null)
        {
            GameObject.Destroy(_transform.gameObject);
        }
    }

    public virtual void Update(float dt)
    {

    }
}

public class WindowManager : Singleton<WindowManager>
{
    private Transform _canvas;

    /// <summary>
    /// 保存所有打开的窗口
    /// </summary>
    private Dictionary<string, BaseWnd> _windows = new Dictionary<string, BaseWnd>();

    public Camera uiCamera;

    /// <summary>
    /// 初始化
    /// </summary>
    public void Initialize()
    {
        MonoBehaviour.DontDestroyOnLoad(GameObject.Find("UI"));
        _canvas = GameObject.Find("UI/Canvas").transform;
        uiCamera = GameObject.Find("UI/Camera").GetComponent<Camera>();
    }

    /// <summary>
    /// 打开界面
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T Open<T>() where T : BaseWnd, new()
    {
        string wndName = typeof(T).Name;
        if (_windows.ContainsKey(wndName))
        {
            return _windows[wndName] as T;
        }
        else
        {
            T wnd = new T();
            wnd.Open(_canvas, wndName);
            _windows.Add(wndName, wnd);
            return wnd;
        }
    }

    /// <summary>
    /// 关闭界面
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public void Close<T>() where T : BaseWnd
    {
        string wndName = typeof(T).Name;
        if (_windows.ContainsKey(wndName))
        {
            _windows[wndName].Close();
            _windows.Remove(wndName);
        }
    }

    /// <summary>
    /// 获得界面
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <returns></returns>
    public T GetWnd<T>() where T : BaseWnd
    {
        string wndName = typeof(T).Name;
        if (_windows.ContainsKey(wndName))
        {
            return _windows[wndName] as T;
        }
        else
        {
            return null;
        }
    }

    public void Update(float dt)
    {
        foreach (BaseWnd wnd in _windows.Values)
        {
            wnd.Update(dt);
        }
    }

    public void Clear()
    {
        _windows.Clear();
    }
}

3 0

作者的其他最新博客

评论 (4 个评论)

回复 3084 2018-7-6 13:41
很好很强大
回复 liuzhaoyun 2018-7-10 09:23
一次更新这么多
回复 liuzhaoyun 2018-7-10 09:23
一次更新这么多
回复 liuzhaoyun 2018-7-10 09:23
一次更新这么多
回复 z2857059 2018-7-11 10:26
很好很强大

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册帐号

个人分类

标签

阅读排行

评论排行

推荐博客

最新博客

关闭

站长推荐 上一条 /1 下一条

返回顶部