UI框架

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
/// <summary>
/// C层,通过管理器拿到obj,调用V层的通用函数对obj进行操作
/// </summary>
public class UIBase : MonoBehaviour {

 void Awake () {
    
}
/// <summary>
/// 找到并收集要用的子控件(这里标记为“_N,_S”),为子控件添加行为脚本
/// </summary>
public void AddBehavioursInChildren()
{
    Transform[] childrenTrans = transform.GetComponentsInChildren<Transform>();
    print("进入Base");
    foreach (var v in childrenTrans)
    {
        if (v.name.EndsWith("_N"))
        {
            v.gameObject.AddComponent<UIBehaviours>();
        }
        else if (v.name.EndsWith("_S"))
        {
            v.gameObject.AddComponent<UISubManager>();
        }
    }
}
public GameObject GetOBJ(string widgeName)
{
    return UIManager.instance.GetChild(transform.name,widgeName);
}

public void AddButLister(string widgeName,UnityAction action)
{
GameObject tmpObj = GetOBJ(widgeName);
if (tmpObj != null)
{
UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
tmpBehav.AddButListen(action);
}
}

public void ChangeImage(string widgeName, Sprite tmpSprit)
{
    //GameObject tmpObj = GetOBJ(widgeName);
    //if (tmpObj != null)
    //{
    //    UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
    //    tmpBehav.ImageChange(tmpSprit);
    //}
    UIBehaviours tmpBehaviours = GetBehaviours(widgeName);
    if (tmpBehaviours != null)
    {
        tmpBehaviours.ImageChange(tmpSprit);
    }

}
/// <summary>
/// sub.API
/// </summary>
/// <param name="cellName"></param>
/// <param name="widgeName"></param>
/// <param name="tmpSprit"></param>
public void ChangeImage(string cellName, string widgeName, Sprite tmpSprit)
{

    GameObject obj = GetOBJ(cellName);
    if (obj != null)
    {
        UISubManager subManager = obj.GetComponent<UISubManager>();

        if (subManager != null)
        {
            subManager.ImageChange(widgeName, tmpSprit);
        }
    }
   

}
public UIBehaviours GetBehaviours(string widgeName)
{
    GameObject tmpObj = GetOBJ(widgeName);
    //UIBehaviours tmpBehav;
    if (tmpObj != null)
    {
         UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
         return tmpBehav;
    }
    return null;
}
/// <summary>
/// 注册自己
/// </summary>
/// <param name="pageName">自己的名字</param>
/// <param name="Name">自己的名字</param>
/// <param name="obj">自己的obj</param>
public void RegistPage(string pageName,string Name,GameObject obj)
{
    UIManager.instance.RegistGameObject(name, name,gameObject);
}

public void AddComponent(Type myComponent,string widgeName)
{
    UIBehaviours tmpBehaviours = GetBehaviours(widgeName);
    if (tmpBehaviours != null)
    {
        tmpBehaviours.AddComponent(myComponent);
    }
}

private void OnDestroy()
{
    UIManager.instance.UnRegistPanel(transform.name);
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// UI管理器,存储需要使用的obj,此类需要先加载
/// </summary>
public class UIManager : MonoBehaviour {

public static UIManager instance;

public Dictionary<string, Dictionary<string, GameObject>> allChildren;

void Awake () {
    instance = this;
    allChildren = new Dictionary<string, Dictionary<string, GameObject>>();

}

/// <summary>
/// 注册obj
/// </summary>
/// <param name="panleName"></param>
/// <param name="widgeName"></param>
/// <param name="obj"></param>
public void RegistGameObject(string panleName,string widgeName,GameObject obj)
{
if (!allChildren.ContainsKey(panleName))
{
allChildren[panleName] = new Dictionary<string, GameObject>();

    }
    if (!allChildren[panleName].ContainsKey(widgeName))
    {
        allChildren[panleName].Add(widgeName, obj);
    }
    else
    {
        Debug.Log("has regist obj==" + widgeName + "panleName" + panleName);
    }
}
/// <summary>
/// 对外提供的获取gameobject的函数
/// </summary>
/// <param name="panleName"></param>
/// <param name="widgeName"></param>
/// <returns></returns>
public GameObject GetChild(string panleName,string widgeName)
{
    if (allChildren.ContainsKey(panleName))
    {
        return allChildren[panleName][widgeName];
    }
    return null;
}
/// <summary>
/// 清理obj
/// 清理时先把obj赋值给临时变量,在清理内存,而后清理字典
/// </summary>
/// <param name="panelNmae"></param>
public void UnRegistPanel(string panelNmae)
{
    if (allChildren.ContainsKey(panelNmae))
    {
        Dictionary<string, GameObject> panelChild = allChildren[panelNmae];
        panelChild.Clear();
        allChildren.Remove(panelNmae);
    }
   
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
/// <summary>
/// V层
/// </summary>
public class UIBehaviours : MonoBehaviour {

void Awake () {
    UIBase tmpRoot= transform.GetComponentInParent<UIBase>();
    UIManager.instance.RegistGameObject(tmpRoot.name, transform.name, gameObject);
}


public void AddButListen(UnityAction action)
{
    Button tmpBut = transform.GetComponent<Button>();
    if (tmpBut != null)
    {
        tmpBut.onClick.AddListener(action);
    }
}

public void AddSliderListen(UnityAction<float> action)
{
    Slider tmpBut = transform.GetComponent<Slider>();
    if (tmpBut != null)
    {
        tmpBut.onValueChanged.AddListener(action);
    }
}

public void AddInputValueChangeListen(UnityAction<string> action)
{
    InputField tmpBut = transform.GetComponent<InputField>();
    if (tmpBut != null)
    {
        tmpBut.onValueChanged.AddListener(action);
    }
}

public void AddComponent(Type tmpType)
{
    gameObject.AddComponent(tmpType);
}
public void ImageChange(Sprite tmpSprit)
{
    Image tmpImage = transform.GetComponent<Image>();
    if (tmpImage != null)
    {
        tmpImage.sprite = tmpSprit;
    }
}

public void ChangeLable(string content)
{
    Text tmpText = transform.GetComponent<Text>();
    if (tmpText != null)
    {
        tmpText.text = content; 
    }
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
/// <summary>
/// 二级管理单位
/// 在出现同名时需使用二级管理
/// </summary>
public class UISubManager : MonoBehaviour {

Dictionary<string, GameObject> allChild;

private void Awake()
{
    UIBase tmpBase = transform.GetComponentInParent<UIBase>();
    UIManager.instance.RegistGameObject(tmpBase.name, name, gameObject);
    allChild = new Dictionary<string, GameObject>();
    Transform[] tmpAll = GetComponentsInChildren<Transform>();
    foreach(var v in tmpAll)
    {
        if (v.name.EndsWith("_M"))
        {
            allChild.Add(v.name, v.gameObject);
        }
    }
    
}

public GameObject GetChild(string widgeName)
{
    return allChild[widgeName];
}

public void AddButListen(string widgeName, UnityAction action)
{
    GameObject child = GetChild(widgeName);
    if (child != null)
    {
        Button but = child.GetComponent<Button>();
        if (but != null)
        {
            but.onClick.AddListener(action);
        }
    }
    
}

public void AddSliderListen(string widgeName, UnityAction<float> action)
{
    GameObject child = GetChild(widgeName);
    if (child != null)
    {
        Slider slider = child.GetComponent<Slider>();
        if (slider != null)
        {
            slider.onValueChanged.AddListener(action);
        }
    }

}
public void ImageChange(string widgeName, Sprite tmpSprit)
{
    GameObject child = GetChild(widgeName);
    if (child != null)
    {
        Image image = child.GetComponent<Image>();
        if (image != null)
        {
            image.sprite = tmpSprit;
        }
    }

}
private void OnDestroy()
{
    if (allChild != null)
    {
        allChild.Clear();
    }
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
///绑定层, 继承C层的管理类,以panle为单位管理游戏时,此脚本挂载到panle上
/// 由于继承了C层,我们直接调用即可。
/// </summary>
public class PanleCtrl : UIBase {

PageLogic pageLogic;
PageModle pageModle;

void Awake () {
    AddBehavioursInChildren();
    pageLogic = new PageLogic();
    pageModle = new PageModle();
    RegistPage(name, name, gameObject);
    print(UIManager.instance.GetChild(name, name).name);
}
private void Start()
{
    print("进入STRAT");
    print(pageLogic.ResourceImage());
    ChangeImage("Content_S", "Image_M", pageLogic.ResourceImage());
}

}


using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 界面切换,继承UIBase,
/// </summary>
public class ChangePageCtrl : UIBase {
PageLogic pageLogic;
PageModle pageModle;
private void Awake()
{
AddBehavioursInChildren();

}
void Start () {
    pageLogic = new PageLogic();
    pageModle = new PageModle();
    InitChangePage();
}

public void InitChangePage()
{
    print("进入初始化");
    if (pageLogic.RegistPage())
    {
            pageLogic.InitPage("Panel");
    }
    AddButLister(ConstantManager.changePageBut, ()=>SwitchCurrentPage(ConstantManager.page));
    AddButLister(ConstantManager.changePageBut_01, () => SwitchCurrentPage(ConstantManager.page_01));
    AddButLister(ConstantManager.changePageBut_02, () => SwitchCurrentPage(ConstantManager.page_02));
}
public void SwitchCurrentPage(string pageName)
{
    pageLogic.PageSwitch(pageName);
    print("切换到"+pageName);
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// M层,数据管理
/// </summary>
public class PageModle : MonoBehaviour {
public Sprite s;
public Sprite s1;
public Sprite s2;
/// <summary>
/// 页面
/// </summary>
public GameObject Page;
public GameObject Page_01;
public GameObject Page_02;
public List<GameObject> pageArray;
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 逻辑层,为绑定层服务
/// </summary>
public class PageLogic : MonoBehaviour {
public PageModle pageModle;
/// <summary>
/// 界面注册是否完成
/// </summary>
/// <returns></returns>
public bool RegistPage()
{
if(UIManager.instance.GetChild(ConstantManager.page, ConstantManager.page) !=null&&
UIManager.instance.GetChild(ConstantManager.page_01, ConstantManager.page_01) != null&&
UIManager.instance.GetChild(ConstantManager.page_02, ConstantManager.page_02) != null
)
{
return true;
}
return false;
}
public void OnClick()
{
Debug.Log("but coming");
}

public void PageSwitch(string pageName)
{
    for(int i = 0; i < pageModle.pageArray.Count;i++)
    {
        if (pageModle.pageArray[i].name.Equals(pageName))
        {
            pageModle.pageArray[i].SetActive(true);
        }
        else
        {
            pageModle.pageArray[i].SetActive(false);
        }
    }       
}

public void InitPage(string mainPage)
{
    pageModle = new PageModle();
    pageModle.pageArray = new List<GameObject>();
    print("进入初始化" );
    print("初始化panle"+UIManager.instance.GetChild("Panel", "Panel"));
    pageModle.pageArray.Add(UIManager.instance.GetChild("Panel", "Panel"));
    pageModle.pageArray.Add(UIManager.instance.GetChild("Panel_01", "Panel_01"));
    pageModle.pageArray.Add(UIManager.instance.GetChild("Panel_02", "Panel_02"));

    foreach (var v in pageModle.pageArray)
    {
        print("主界面"+mainPage);
       if (v.name.Equals(mainPage))
       {
           v.SetActive(true);
            print("启用主界面"+"+"+v.name);
       }
      else
      {
           v.SetActive(false);
            print("禁用其他界面"+ "+" + v.name);
       }
    }
}

public Sprite ResourceImage()
{
    print("进入加载图片");
     return Resources.Load<Sprite>("1534238263(1)");
}

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 常量类
/// </summary>
public static class ConstantManager {

public const string page = "Panel";

public const string page_01 = "Panel_01";

public const string page_02 = "Panel_02";

public const string changePageBut = "Button_N";

public const string changePageBut_01 = "Button (1)_N";

public const string changePageBut_02 = "Button (2)_N";

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 功能测试脚本
/// </summary>
public class FunctionTest : MonoBehaviour {

void Start () {
    
}

void Update () {
    
}

}


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,133评论 6 497
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,682评论 3 390
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,784评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,508评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,603评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,607评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,604评论 3 415
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,359评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,805评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,121评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,280评论 1 344
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,959评论 5 339
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,588评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,206评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,442评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,193评论 2 367
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,144评论 2 352

推荐阅读更多精彩内容

  • 1、搭建UI界面 1.1准备工作 1、调整Game视图中的分辨率800*480 2、在 Edit --Projec...
    Sir_浅林阅读 8,425评论 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,695评论 0 3
  • 学习与分享: 奇怪的大冒险简单部分的UI框架分为三种: 一、综合管理类(UIManager); 1、先写一个单例使...
    歆辙阅读 277评论 0 0
  • 哈喽,欢迎回来!我是吾叔。接上一篇文章的话题,我继续和你分享玉石行业的第二个机会:工艺创新。 在返回办工厂第二年我...
    吾叔阅读 225评论 0 0
  • 小朋友对长大这件事有一种迷之执念,就像成年人对长不大也要坚持到底一样。 ■我长大了 天气渐渐的变凉,屋子里的气温有...
    梅栀子阅读 304评论 0 0