You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace HJDFrameWork
{
public abstract class ViewBase : MonoBehaviour, IView
{
private Dictionary<string, List<UIBehaviour>> _ViewDataList = new Dictionary<string, List<UIBehaviour>>();
public virtual void Hide()
{
this.gameObject.SetActive(false);
}
public virtual void Init()
{
//或的控件并保存在字典中可根据自己UI搭载的控件更改
FinChildControl<Button>();
FinChildControl<Image>();
FinChildControl<Scrollbar>();
FinChildControl<Text>();
FinChildControl<Toggle>();
FinChildControl<RawImage>();
//注册到ViewManager
ViewManager.Instance.Register(this.name, this);
}
public virtual void Show()
{
this.gameObject.SetActive(true);
}
//返回名为controlName的UI身上的T控件《若没有则返回null
public T GetViewData<T>(string controlName) where T : UIBehaviour
{
if (_ViewDataList.ContainsKey(controlName))
{
for (int i = 0; i < _ViewDataList[controlName].Count; i++)
{
if (_ViewDataList[controlName][i] is T)//使用is判断是否为T控件
return _ViewDataList[controlName][i] as T;
}
}
return null;
}
private void FinChildControl<T>() where T : UIBehaviour
{
T[] controls = this.GetComponentsInChildren<T>();//注意用的是GetComents,返回的是一组
string objName;//使用名字对控件按物体分类
if (controls.Length < 0) return;
for (int i = 0; i < controls.Length; i++)
{
objName = controls[i].gameObject.name;
if (_ViewDataList.ContainsKey(objName))
_ViewDataList[objName].Add(controls[i]);//注意controlDic中存储的类型为<string, List<UIBehaviour>,所以我们的同一个UI的所有控件都在这个链表中
else
_ViewDataList.Add(objName, new List<UIBehaviour>() { controls[i] });//添加UI到字典中
}
Debug.Log(this.name + "查找了" + controls.Length + typeof(T).Name+"组件");
}
protected void ButtonAddlisten(Button button, Action action)
{
if (button != null)
{
button.onClick.AddListener(() => { action(); });
}
else
{
Debug.Log(button.name + "按钮功能注册失败");
}
}
protected ItemSign GetItem(string Name)
{
ItemSign item = null;
if (ItemManager.Instance != null)
{
item= ItemManager.Instance.GetItem(Name);
}
if (item != null) return item;
return null;
}
}
}