using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using Object = UnityEngine.Object;
///
/// UI管理
/// 作者:徐振升
/// 最后更新:2018-07-17 15:30
/// 联系方式:QQ:359059686
/// 项目没有UI管理,此脚本目的为了方便UI管理,目前管理注册中的页面,请勿随意修改。
///
public static class UIManager
{
private static Dictionary templates = new Dictionary();
private static Dictionary Views = new Dictionary();
private static Dictionary UIPath = new Dictionary();
static UIManager()
{
//①手动注册UI //RegisterUI("UI/XLogUIView");
//②自动注册UI //请把UIView名称与预设名称相同,放在Resource文件根目录下。如自动注册XLogUIView=Resource/XLogUIView
//私聊界面
RegisterUI("UI/UIViewPrivateChat");
//公聊界面
RegisterUI("UI/UIViewPublicChat");
//器材库界面
RegisterUI("UI/UIViewEquipLib");
//数量选择界面
RegisterUI("UI/UIViewNumberSelect");
//背包界面
RegisterUI("UI/UIViewBag");
//建议界面
RegisterUI("UI/UIViewSuggest");
//角色报告
RegisterUI("UI/UIViewReport");
//问卷调查
RegisterUI("UI/UIViewQuestionnaire");
//评分记录
RegisterUI("UI/UIViewScore");
//队伍分析
RegisterUI("UI/UIViewAnalyze");
}
// 注册UI
public static void RegisterUI(string prefabPath)
where T : UIView
{
if (!UIPath.ContainsKey(typeof(T).ToString()))
{
UIPath.Add(typeof(T).ToString(), prefabPath);
}
}
public static string Normalize(string name)
{
int num = name.IndexOf('.');
if (num < 0)
{
return name;
}
return name.Substring(0, num);
}
// 加载UI
private static void LoadView(string viewName)
where T : UIView
{
string tempKey = typeof(T).ToString();
if (!UIPath.ContainsKey(tempKey))
{
// 没有注册先注册
RegisterUI(typeof(T).ToString());
}
string tempPath = Normalize(UIPath[tempKey]);
WeakReference value;
GameObject gameObject;
if (templates.TryGetValue(tempPath, out value) && value.IsAlive)
{
gameObject = (GameObject)value.Target;
}
else
{
gameObject = Resources.Load(tempPath);
if (gameObject != null)
{
gameObject.SetActive(false);
templates[tempPath] = new WeakReference(gameObject);
}
else
{
// Resource根目录没有发现UIView的Prefab,请添加。
throw new Exception(string.Format("<<{0}>> prefab not found in Resource folder.", tempPath));
}
}
if (gameObject != null && gameObject.GetComponent() != null)
{
//实例化
GameObject ui = GameObject.Instantiate(gameObject);
ui.name = gameObject.name;
//记录初始大小
Vector3 anchorPos3D = ui.GetComponent().anchoredPosition3D;
Vector3 anchorPos = ui.GetComponent().anchoredPosition;
Vector3 sizeDel = ui.GetComponent().sizeDelta;
Vector3 scale = ui.GetComponent().localScale;
//设置父对象
T view = ui.GetComponent();
Transform parent = GetUICanvas().transform;
ui.transform.SetParent(parent);
//还原初始大小
ui.GetComponent().anchoredPosition3D = anchorPos3D;
ui.GetComponent().anchoredPosition = anchorPos;
ui.GetComponent().sizeDelta = sizeDel;
ui.GetComponent().localScale = scale;
view.ViewName = viewName;
Views.Add(viewName, view);
}
}
private static GameObject GetUICanvas()
{
GameObject gameObject = GameObject.Find("Canvas");
if (gameObject == null || gameObject.GetComponent