UIFrame代码

1,UIBase脚本:向Ui的功能键(通过功能键名字末尾或者开头判断“-”)添加脚本UiMangager(工具类),UIBahav的方法封装进Base按钮注册事件方法。

2,UIManager脚本:写个容器,单例,初始化操作,获取一个物体

3,UIBahavious脚本:注册进UIManager,写注册按钮事件方法。

4,FriendCtrl脚本:里面有数据层,控制层(继承UIBase),逻辑层(按钮事件的具体方法)。

 

UIBase:

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

using UnityEngine.Events;
public class UIBase : MonoBehaviour {

private void Awake()
{
Transform[] allChild = transform.GetComponentsInChildren<Transform>();
for (int i = 0; i < allChild.Length; i++)
{
//判断后边的—为_Z的都添加一下UiBase
if (allChild[i].name.EndsWith("_Z", System.StringComparison.Ordinal))//执行0
{
allChild[i].gameObject.AddComponent<UIBase>();
}
}
}
public GameObject GetGameObject(string widName)
{
return UIManager._instance.GetChild(transform.name, widName);
}
//这里是But的事件注册
public void AddButtonLister(string widName, UnityAction action)
{
// GameObject tmpObj = GetGameObject(widName);
// if (tmpObj!=null)
// {
// UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
// tmpBehav.AddButtonListen(action);
// }
UIBehaviours tmpBehav = GetBehaviours(widName);
if (tmpBehav != null)
{
tmpBehav.AddButtonListen(action);
}
}
//这里是Imag的
public void ChangeImage(string widName, Sprite tmpsprite)
{
// GameObject tmpObj = GetGameObject(widName);
// if (tmpObj != null)
// {
// UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
// tmpBehav.ChangeImage(tmpsprite);
// }
UIBehaviours tmpBehav = GetBehaviours(widName);
if (tmpBehav != null)
{
tmpBehav.ChangeImage(tmpsprite);
}
}
//简化上面两个方法的重复代码
public UIBehaviours GetBehaviours(string widName)
{
GameObject tmpObj = GetGameObject(widName);
if (tmpObj != null)
{
UIBehaviours tmpBehav = tmpObj.GetComponent<UIBehaviours>();
return tmpBehav;
}
return null;
}
}

——————————————————————————————————————————————————————————————————

UIManager

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

public class UIManager : MonoBehaviour {

public static UIManager _instance;

Dictionary<string, Dictionary<string, GameObject>> allChild;
private void Awake()
{
_instance = this;
allChild = new Dictionary<string, Dictionary<string, GameObject>>();//初始化
}
/// <summary>
/// 初始化 添加操作
/// </summary>
/// <param name="panleName"></param>
/// <param name="widName"></param>
/// <param name="obj"></param>
public void RegistGameObject(string panleName,string widName,GameObject obj)
{
if (!allChild.ContainsKey(panleName))
{
allChild[panleName] = new Dictionary<string, GameObject>();
}
if (!allChild[panleName].ContainsKey(widName))
{
allChild[panleName].Add(widName, obj);
}

}
/// <summary>
/// 获取的是一个物体
/// </summary>
/// <param name="panleName"></param>
/// <param name="widName"></param>
/// <returns></returns>
public GameObject GetChild(string panleName,string widName)
{
if (allChild.ContainsKey(panleName))
{
return allChild[panleName][widName];
}
return null;
}


}

———————————————————————————————————————————————————————————————————————————————

UIBehaviours

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

using UnityEngine.UI;
using UnityEngine.Events;
public class UIBehaviours : MonoBehaviour {


private void Awake()
{
//注册到UIManager
UIBase tempRoot = transform.GetComponentInParent<UIBase>();
UIManager._instance.RegistGameObject(tempRoot.name, transform.name, gameObject);
}

/// <summary>
/// 注册But事件
/// </summary>
/// <param name="action"></param>
public void AddButtonListen(UnityAction action)
{
Button tempBut = GetComponent<Button>();
if (tempBut!=null)
{
tempBut.onClick.AddListener(action);
}

}
/// <summary>
/// 注册Slider事件
/// </summary>
/// <param name="action"></param>
public void AddSliderListen(UnityAction<float> action)
{
Slider tempSli = GetComponent<Slider>();
if (tempSli != null)
{
tempSli.onValueChanged.AddListener(action);
}

}
/// <summary>
/// 注册InputField事件
/// </summary>
/// <param name="action"></param>
public void AddInputFieldListen(UnityAction<string> action)
{
InputField tempInp = GetComponent<InputField>();
if (tempInp != null)
{
tempInp.onValueChanged.AddListener(action);
}

}
/// <summary>
/// Image 加载图片
/// </summary>
/// <param name="tmpsprite"></param>
public void ChangeImage(Sprite tmpsprite)
{
Image tempImg =transform.GetComponent<Image>();
if (tempImg != null)
{
tempImg.sprite = tmpsprite;
}

}
/// <summary>
/// Text 的连接
/// </summary>
/// <param name="_tmpText"></param>
public void ChangeText(string _tmpText)
{
Text tempText = transform.GetComponent<Text>();
if (tempText != null)
{
tempText.text = _tmpText;
}

}

}

————————————————————————————————————————————————————————————————————————

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

public class FriendModle//数据层
{
public string useName;

public string passWord;

}

public class FriendCtrl : UIBase {//控制层


FriendModle friendModle;
FriendLogic friendLogic;

void Start() {
friendModle = new FriendModle();
friendLogic = new FriendLogic();
AddButtonLister("UIBut", friendLogic.OnClick);
Sprite sprite_ = Resources.Load<Sprite>("SpriteName");
ChangeImage("UIBut", sprite_);
}

// Update is called once per frame
void Update () {

}
}
public class FriendLogic//逻辑层
{
public void OnClick()
{
Debug.Log("事件");
}

}

 

posted @ 2020-02-18 22:31  北特  阅读(388)  评论(0编辑  收藏  举报