ui框架

using LogPrint;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public delegate void OnLoadCallBack(params object[] args);

public class UIBase
{
List ComponentList = new List();

private GameObject m_UIPrefab;
public GameObject UIPrefab
{
    get { return m_UIPrefab; }
}

public bool IsShow()
{
    return m_UIPrefab.gameObject.activeInHierarchy;
}

public void LoadPanel(string name, OnLoadCallBack callBack)
{
    if (string.IsNullOrEmpty(name))
    {
        return;
    }

    GameObject o = ResourceManager.Instance.Load(ResourcesDefine.UIDefine + name) as GameObject;
    if (o == null)
    {
        return;
    }

    m_UIPrefab = GameObject.Instantiate(o);
    m_UIPrefab.transform.SetParent(UIManger.UIRoot);

    RectTransform rect = m_UIPrefab.GetComponent<RectTransform>();
    rect.anchoredPosition3D = Vector2.zero;
    rect.localScale = Vector3.one;
    if (GameCommonManager.GetUIType(rect) == UIRectType.Stretch)
    {
        rect.offsetMax = Vector2.zero;
        rect.offsetMin = Vector2.zero;
    }

    m_UIPrefab.SetActive(true);

    OnStart();

    OnShow();

    if (callBack != null)
    {
        callBack(m_UIPrefab);
    }
}

public virtual void OnStart()
{

}

public virtual void OnShow()
{
    //将对应面板移动到界面最前面
    m_UIPrefab.transform.SetSiblingIndex(UIManger.UIRoot.childCount);
}

//当前界面重现显示到屏幕最前方(之前遮挡的界面关闭后)
public virtual void OnBack()
{

}

public virtual void OnHide()
{

}

public virtual void OnDestroy()
{

}


public void ShowPanel()
{
    m_UIPrefab.SetActive(true);
    OnShow();
}

public void HidePanel()
{
    m_UIPrefab.SetActive(false);

    OnHide();
}

public void DestroyPanel()
{
    ClearComponentList();

    OnHide();

    OnDestroy();

    GameObject.Destroy(m_UIPrefab);
}

public T GetChild<T>(string name) where T : MonoBehaviour
{
    Transform child = m_UIPrefab.transform.Find(name);
    if (child == null)
    {
        LogSystem.LogError(m_UIPrefab.name + " is not child of " + m_UIPrefab.name);
        return null;
    }

    T temp = child.GetComponent<T>();
    if (temp == null)
    {
        LogSystem.LogError(name + " is not has component ");
    }

    if (!ComponentList.Contains(temp.gameObject))
    {
        ComponentList.Add(temp.gameObject);
    }
    return temp;
}

public GameObject GetChild(string name)
{
    Transform child = m_UIPrefab.transform.Find(name);
    if (child == null)
    {
        LogSystem.LogError(m_UIPrefab.name + " is not find child of " + name);
        return null;
    }

    if (!ComponentList.Contains(child.gameObject))
    {
        ComponentList.Add(child.gameObject);
    }
    return child.gameObject;
}


//将按钮都保存起来,用于批量添加UIEventListener脚本
protected List<GameObject> uiBtnList = new List<GameObject>();
public GameObject GetButton(string name)
{
    GameObject btn = GetChild(name);
    if (btn)
    {
        uiBtnList.Add(btn);
    }
    return btn;
}

private void ClearComponentList()
{
    for (int i = 0; i < ComponentList.Count; ++i)
    {
        if (ComponentList[i] == null)
        {
            continue;
        }

        ComponentList[i] = null;
    }

    ComponentList.Clear();
}

}

//-------------------------------------------------------------------

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

public class UIManger
{
public const float ORIGIN_SPRITE_WITH = 1920.0f;
public const float ORIGIN_SPRITE_HEIGHT = 1080.0f;

private float m_withScale = 1;
private float m_heightScale = 1;


private static UIManger instance;
public static UIManger Instance
{
    get
    {
        if (instance == null)
        {
            instance = new UIManger();
        }
        return instance;
    }
}

private static Transform uiRoot;
public static Transform UIRoot
{
    get
    {
        if (uiRoot == null)
        {
            uiRoot = GameObject.Find("UICamera/Canvas").transform;
        }

        return uiRoot;
    }
}

private static Transform cRoot;
public static Transform CRoot
{
    get
    {
        if (cRoot == null)
        {
            cRoot = GameObject.Find("CRoot").transform;
        }

        return cRoot;
    }
}

private static Transform sceneCamera;
public static Transform SceneCamera
{
    get
    {
        if (sceneCamera == null)
        {
            sceneCamera = GameObject.Find("Game/Camera").transform;
        }

        return sceneCamera;
    }
}

private Dictionary<GameObject, UIBase> m_UIDic = new Dictionary<GameObject, UIBase>();

//显示界面
public void Show<T>(System.Action callBack = null) where T : UIBase
{
    UIBase uiBase = GetUI<T>();

    if (uiBase == null)
    {
        uiBase = Create<T>();

        uiBase.LoadPanel(typeof(T).Name, (object[] param) =>
        {
            GameObject o = param[0] as GameObject;
            m_UIDic.Add(o, uiBase);          

            if (callBack != null) callBack();

            lastOpenPanel = uiBase.ToString();
        });
        
        return;
    }

    uiBase.ShowPanel();

    if (callBack != null) callBack();

    lastOpenPanel = uiBase.ToString();
}

//隐藏界面
public void Hide<T>() where T : UIBase
{
    UIBase uiBase = GetUI<T>();
    if (uiBase == null)
    {
        return;
    }

    uiBase.HidePanel();

    CheckUIFront();
}

//摧毁界面
public void Destroy<T>() where T : UIBase
{
    UIBase uiBase = GetUI<T>();
    if (uiBase == null)
    {
        return;
    }
    GameObject panel = null;
    Dictionary<GameObject, UIBase>.Enumerator it = m_UIDic.GetEnumerator();
    while (it.MoveNext())
    {
        if (it.Current.Value is T)
        {
            panel = it.Current.Key;
        }
    }

    m_UIDic.Remove(panel);

    uiBase.DestroyPanel();

    CheckUIFront();
}

//上次打开的UI界面
string lastOpenPanel = "";
//检测当前界面是否是重新回到屏幕最前方
void CheckUIFront()
{
    UIBase panel = null;
    //获取当前屏幕最前方的界面
    if (UIRoot.transform.childCount < 1)
    {
        return;
    }

    for (int i = UIRoot.transform.childCount - 1; i >= 0; --i)
    {
        if (UIRoot.transform.GetChild(i).gameObject.activeInHierarchy == true)
        {                    

            //找到最前方的这个界面了
            if (m_UIDic.ContainsKey(UIRoot.transform.GetChild(i).gameObject))
            {
                panel = m_UIDic[UIRoot.transform.GetChild(i).gameObject];
            }

            //只检测最前面的界面
            break;
        }
    }

    //首次打开界面不触发这个函数,只有在界面已经打开后又被别的界面遮挡同时这个界面还不隐藏才会触发
    if (panel != null && panel.ToString() != lastOpenPanel)
    {
        panel.OnBack();
    }
}

public T GetUI<T>() where T : UIBase
{
    Dictionary<GameObject, UIBase>.Enumerator it = m_UIDic.GetEnumerator();
    while (it.MoveNext())
    {
        if (it.Current.Value is T)
        {
            return it.Current.Value as T;
        }
    }

    return null;
}


public T Create<T>() where T : UIBase
{
    UIBase uiBase = System.Activator.CreateInstance(typeof(T)) as UIBase;
    return uiBase as T;
}


//隐藏其它之前已经打开的所有界面(挂在UICamera/Canvas下面的界面)
public void HideAllPanel(bool isShow = false)
{
    for (int i = 0; i < UIRoot.childCount; i++)
    {
        UIRoot.transform.GetChild(i).gameObject.SetActive(isShow);
    }
}

public void AdaptCameraScale()
{
    if (SceneCamera == null)
    {
        return;
    }

    float heightScale = ORIGIN_SPRITE_WITH / (float)Screen.width;
    if (heightScale >= 1)
    {
        return;
    }

    SceneCamera.transform.localScale = new Vector3(1, heightScale + 0.05f, 1);

    //只有拯救动物比较特殊,背景是一条路,人物必须站在路上
    if (SceneCommonManager.Instance.CurrentScence == SceneType.SaveAnimal)
    {
        CRoot.transform.localScale = new Vector3(1, heightScale + 0.05f, 1);
    }

}

public void AdpatPipiHome(GameObject go)
{
    if (go == null)
    {
        return;
    }

    float heightScale = (float)Screen.height / ORIGIN_SPRITE_HEIGHT ; 
    if (heightScale >= 1)
    {
        return;
    }

    go.transform.localScale = new Vector3(1, heightScale, 1);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值