Unity 工具类 之 简单的UI管理框架/仅通过UIWidget(UI元素组件)名即可添加UI对应事件(简单的MVC思路管理UI与逻辑)
目录
Unity 工具类 之 简单的UI管理框架/仅通过UIWidget(UI元素组件)名即可添加UI对应事件(简单的MVC思路管理UI与逻辑)
一、简单介绍
二、什么是 MVC
三、简单使用说明
四、实现原理
五、注意事项
六、效果预览
七、实现步骤
八、关键脚本
一、简单介绍
Unity 工具类,自己整理的一些游戏开发可能用到的模块,单独独立使用,方便游戏开发。
一个简单的UI管理框架管理类,管理各个UIPanel、和UI Widget 元素,只要传一个UIWidget名字即可给对应UIWidget 添加对应事件(按钮、Slider、Drag 、PointerClick 等)或者修改上面的内容(文本、图片等)。
二、什么是 MVC
MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑、数据、界面 显示分离的方法组织代码,将业务逻辑聚集到一个部件里面,在改进和个性化定制界面及用户交互的同时,不需要重新编写业务逻辑。MVC被独特的发展起来用于 映射传统的输入、处理和输出功能在一个逻辑的图形化用户界面的结构中。
MVC 框架简单如下图
换种形式的 MVC 业务流程
- Model(模型)是应用程序中用于处理应用程序数据逻辑的部分。 通常模型对象负责在数据库中存取数据。
- View(视图)是应用程序中处理数据显示的部分。 通常视图是依据模型数据创建的。
- Controller(控制器)是应用程序中处理用户交互的部分。 通常控制器负责从视图读取数据,控制用户输入,并向模型发送数据。
三、简单使用说明
1、UIPanel 各个UI面板 继承 UIPanelBase
2、UIPanel 中类似如下通过UI元素的名称进行相关事件绑定,Text 或者 Image.Sprite 更新等等
AddButtonListener(UIWidegetNameString.Button_N,tmpPanelLogic.Button_N);
AddInputFieldValueChangedListener(UIWidegetNameString.InputField_N, tmpPanelLogic.InputField_N);
AddSliderListener(UIWidegetNameString.Slider_N, tmpPanelLogic.Slider_N);
ChangeTextContent(UIWidegetNameString.Text_N, "Test Text_N Click : " + mTmpPanelModel.ClickCount1);
........
3、本案例 TmpPanelModel,TmpPanelLogic,TmpPanel 使用 MVC 将每部分独立成块
4、GameStart 作为程序入口,初始化 UIPanelManager,既可以运行起来整个框架
四、实现原理
1、GameStart 启动 UIPanelManager;
2、UIPanelManager 启动 UIPanelBase;
3、UIPanelBase 获取所有子物件,并且给子物件名称 _N 结尾的添加 UIBehaviour 脚本,并且给子物件名称 _C 结尾的添加 UISubPanelManager脚本;
4、UIBehaviour / UISubPanelManager 把自己注册到 UIPanelManager 字典中;
5、UISubPanelManager 给子物件名称 _S 结尾的添加 UIBehaviour 脚本,并且这里的 UISubPanelManager 管理 _S 子物件 ;
6、各个 UIPanel (继承 UIPanelBase)通过 _N / _S UI Widget 名称 访问该 UI Widget ,添加相关的事件等;
7、UIPanelBase / UIBehaviour / UISubPanelManager 可以根据自己需要扩展更多功能;
8、UIPanelManager 的框架大概如下:
五、注意事项
1、_N,_C, _S 的尾部特征可以根据自己的需要自行设定定义
2、UIPanel 之间的名称一定不一样,且 同一 UIPanel 下的 _N UI Widget 名称一定不能重复,不同 UIPanel 下的 _N UI Widget 名称可重复;
3、_C 的名称一定也不一样 (其实 _C 的管理类似 UIPanel), 同一 _C 下的 _S UI Widget 名称一定不能重复,不同 _C 下的 _S UI Widget 名称可重复;
六、效果预览
七、实现步骤
1、打开Unity,新建一个空工程
2、在场景中,UI布局,如下图,把对应的要用到的UI Widget 添加 _N,并把 Item 作为可重用的 item,对应下面要用到的UI Widget 添加 _S,同时把 TmpPanel 添加 TmpPanel 脚本,和 Item 一起作为预制体,保存到 工程中,如下图
3、在工程中新建脚本,APP 文件夹中的脚本是不同的应用中涉及到的一些变化较大的脚本,UIFramework 文件夹中的脚本是UI框架中,实际应用变化较小的脚本,是UI的关键脚本
4、把 GameStart 脚本挂载到场景中,是应用的启动脚本,同时Canvas 以作为预制体的 TmpPanel(注意:TmpPanel预制体挂载了TmpPanel脚本) 删除
5、运行场景,效果如上
八、关键脚本
1、GameStart
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameStart : MonoSingleton<GameStart>
{
public TmpPanelModel mTmpPanelModel;
internal UIPanelManager mUIPanelManager;
protected override void Awake()
{
base.Awake();
DontDestroyOnLoad(gameObject);
mTmpPanelModel = new TmpPanelModel(0,1,2,3,"",0);
mUIPanelManager = UIPanelManager.Instance;
}
// Start is called before the first frame update
void Start()
{
mUIPanelManager.Init(()=> {
GameObject tmpPrefab = Resources.Load<GameObject>(ResourcesPath.TmpPanel);
GameObject tmpPanel = GameObject.Instantiate(tmpPrefab);
tmpPanel.transform.SetParent(mUIPanelManager.MainCanvas, false);
mUIPanelManager.RegisterUIPanel(tmpPanel.name, tmpPanel.GetComponent<UIPanelBase>());
});
}
}
2、UIPanelManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class UIPanelManager : MonoSingleton<UIPanelManager>
{
private Transform mainCanvas;
// panel 名字, UI Widget 名字,UI Widget 实体
Dictionary<string, Dictionary<string, GameObject>> allUIWidgetsDic;
// panel 名字, 各个 UIPanelBase
Dictionary<string, UIPanelBase> allUIPanelsDic;
public Transform MainCanvas {
get {
if (mainCanvas==null)
{
mainCanvas = GameObject.Find("Canvas").transform;
}
return mainCanvas;
}
}
/// <summary>
/// 注册 UI Panel
/// </summary>
/// <param name="panelName"></param>
/// <param name="panelBase"></param>
public void RegisterUIPanel(string panelName, UIPanelBase panelBase) {
if (allUIPanelsDic.ContainsKey(panelName)==false)
{
allUIPanelsDic.Add(panelName,panelBase);
}
}
/// <summary>
/// 注册 UI Widget
/// </summary>
/// <param name="panelName"></param>
/// <param name="uiWidgetNAme"></param>
/// <param name="uiWidgetGameobject"></param>
public void RegisterUIWidgetGameObject(string panelName,string uiWidgetNAme, GameObject uiWidgetGameobject) {
if (allUIWidgetsDic.ContainsKey(panelName)==false)
{
allUIWidgetsDic[panelName] = new Dictionary<string, GameObject>();
}
allUIWidgetsDic[panelName].Add(uiWidgetNAme,uiWidgetGameobject);
}
/// <summary>
/// 获取 UI Widget
/// </summary>
/// <param name="panelName"></param>
/// <param name="uiWidgetName"></param>
/// <returns></returns>
public GameObject GetUIWidgetGameObject(string panelName,string uiWidgetName) {
if (allUIWidgetsDic.ContainsKey(panelName)==true)
{
return allUIWidgetsDic[panelName][uiWidgetName];
}
return null;
}
public void UnRegisterUIWidgetGameObject(string panelName, string uiWidgetName) {
if (allUIWidgetsDic.ContainsKey(panelName)==true)
{
if (allUIWidgetsDic[panelName].ContainsKey(uiWidgetName)==true)
{
allUIWidgetsDic[panelName].Remove(uiWidgetName);
}
}
}
public void UnRegisterPanelWidget(string panelName)
{
if (allUIWidgetsDic.ContainsKey(panelName) == true)
{
allUIWidgetsDic[panelName].Clear();
allUIWidgetsDic[panelName] = null;
}
}
public void UnRegisterPanel(string panelName)
{
if (allUIPanelsDic.ContainsKey(panelName) == true)
{
allUIWidgetsDic.Remove(panelName);
}
}
public void UnRegisterPanelAll()
{
if (allUIPanelsDic == null)
{
allUIWidgetsDic.Clear();
allUIWidgetsDic = null;
}
}
protected override void Awake()
{
base.Awake();
allUIWidgetsDic = new Dictionary<string, Dictionary<string, GameObject>>();
allUIPanelsDic = new Dictionary<string, UIPanelBase>();
}
/// <summary>
/// 根据需要变动
/// </summary>
public void Init(UnityAction action)
{
action();
}
}
3、UIPanelBase
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class UIPanelBase : MonoBehaviour
{
private void Awake()
{
Transform[] allChildTrans = transform.GetComponentsInChildren<Transform>();
for (int i = 0; i < allChildTrans.Length; i++)
{
if (allChildTrans[i].name.EndsWith("_N"))
{
UIBehaviour tmp = allChildTrans[i].gameObject.AddComponent<UIBehaviour>();
tmp.RegisterSelf();
}
if (allChildTrans[i].name.EndsWith("_C"))
{
allChildTrans[i].gameObject.AddComponent<UISubPanelManager>();
}
}
}
public void AddUISubPanelManagerComponent(GameObject obj) {
obj.AddComponent<UISubPanelManager>();
}
public GameObject InstantiateGridItems(GameObject itemPrefab,string itemName, Transform parent) {
GameObject tmpObj = GameObject.Instantiate(itemPrefab);
tmpObj.name = itemName;
tmpObj.transform.SetParent(parent, false);
AddUISubPanelManagerComponent(tmpObj);
return tmpObj;
}
public GameObject GetUIWidgetGameObject(string uiWidgetName) {
//Debug.Log("GetUIWidgetGameObject() :" + );
return UIPanelManager.Instance.GetUIWidgetGameObject(this., uiWidgetName);
}
public UIBehaviour GetUIWidgetUIBehaviour(string uiWidgetName) {
GameObject obj = GetUIWidgetGameObject(uiWidgetName);
if (obj !=null)
{
return obj.GetComponent<UIBehaviour>();
}
return null;
}
public UISubPanelManager GetUISubPanelManager(string itemName) {
GameObject tmpObj = GetUIWidgetGameObject(itemName);
if (tmpObj != null)
{
return tmpObj.GetComponent<UISubPanelManager>();
}
return null;
}
private void OnDestroy()
{
Debug.Log("OnDestroy :" + );
if (UIPanelManager.Instance != null)
{
UIPanelManager.Instance.UnRegisterPanelWidget();
}
if (UIPanelManager.Instance != null)
{
UIPanelManager.Instance.UnRegisterPanel();
}
}
#region UI Widget Operation
public void AddButtonListener(string uiWidgetName, UnityAction action) {
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.AddButtonListener(action);
}
}
public void AddButtonListener(string itemName, string uiWidgetName, UnityAction action)
{
UISubPanelManager subPanelManager = GetUISubPanelManager(itemName);
if (subPanelManager != null)
{
subPanelManager.AddButtonListener(uiWidgetName, action);
}
}
public void AddSliderListener( string uiWidgetName, UnityAction<float> action)
{
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.AddSliderListener(action);
}
}
public void AddInputFieldEndEditListener(string uiWidgetName, UnityAction<string> action)
{
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.AddInputFieldEndEditListener(action);
}
}
public void AddInputFieldValueChangedListener(string uiWidgetName, UnityAction<string> action)
{
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.AddInputFieldValueChangedListener(action);
}
}
public void ChangeTextContent(string uiWidgetName, string content) {
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.ChangeTextContent(content);
}
}
public void ChangeTextContent(string itemName, string uiWidgetName, string content)
{
UISubPanelManager subPanelManager = GetUISubPanelManager(itemName);
if (subPanelManager != null)
{
subPanelManager.ChangeTextContent(uiWidgetName, content);
}
}
#endregion
}
4、UIBehaviour
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIBehaviour : MonoBehaviour
{
public void RegisterSelf() {
UIPanelBase parentUIBase = transform.GetComponentInParent<UIPanelBase>();
UIPanelManager.Instance.RegisterUIWidgetGameObject(parentUIBase.name, this., this.gameObject);
}
#region normal UI Envents
public void AddButtonListener(UnityAction action) {
Button tmp = transform.GetComponent<Button>();
if (tmp != null)
{
tmp.onClick.AddListener(action);
}
}
public void AddSliderListener(UnityAction<float> action)
{
Slider tmp = transform.GetComponent<Slider>();
if (tmp != null)
{
tmp.onValueChanged.AddListener(action);
}
}
public void AddInputFieldEndEditListener(UnityAction<string> action)
{
InputField tmp = transform.GetComponent<InputField>();
if (tmp != null)
{
tmp.onEndEdit.AddListener(action);
}
}
public void AddInputFieldValueChangedListener(UnityAction<string> action)
{
InputField tmp = transform.GetComponent<InputField>();
if (tmp != null)
{
tmp.onValueChanged.AddListener(action);
}
}
public void ChangeTextContent(string content) {
Text tmp = transform.GetComponent<Text>();
if (tmp != null)
{
tmp.text = content;
}
}
public void ChangeImageSprite(Sprite sprite)
{
Image tmp = transform.GetComponent<Image>();
if (tmp != null)
{
tmp.sprite = sprite;
}
}
#endregion
#region 动态添加监听接口事件 修改 EventTriggerType
public void AddDragInterface(UnityAction<BaseEventData> action) {
EventTrigger trigger = gameObject.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = gameObject.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.Drag;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(action);
trigger.triggers.Add(entry);
}
public void AddPointerClickInterface(UnityAction<BaseEventData> action)
{
EventTrigger trigger = gameObject.GetComponent<EventTrigger>();
if (trigger == null)
{
trigger = gameObject.AddComponent<EventTrigger>();
}
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback = new EventTrigger.TriggerEvent();
entry.callback.AddListener(action);
trigger.triggers.Add(entry);
}
#endregion
}
5、UISubPanelManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class UISubPanelManager : MonoBehaviour
{
Dictionary<string, Transform> allChildrenDic;
private void Awake()
{
UIPanelBase parentUIBase = transform.GetComponentInParent<UIPanelBase>();
UIPanelManager.Instance.RegisterUIWidgetGameObject(parentUIBase.name, this., this.gameObject);
allChildrenDic = new Dictionary<string, Transform>();
Transform[] tmpChildren = transform.GetComponentsInChildren<Transform>();
for (int i = 0; i < tmpChildren.Length; i++)
{
if (tmpChildren[i].name.EndsWith("_S"))
{
tmpChildren[i].gameObject.AddComponent<UIBehaviour>();
allChildrenDic.Add(tmpChildren[i].name, tmpChildren[i]);
}
}
}
public Transform GetChildTransformByName(string uiWidgetName) {
return allChildrenDic[uiWidgetName];
}
public UIBehaviour GetUIWidgetUIBehaviour(string uiWidgetName)
{
Transform obj = GetChildTransformByName(uiWidgetName);
if (obj != null)
{
return obj.GetComponent<UIBehaviour>();
}
return null;
}
#region UI Widget Operation
public void AddButtonListener(string uiWidgetName, UnityAction action)
{
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.AddButtonListener(action);
}
}
public void ChangeTextContent(string uiWidgetName, string content)
{
UIBehaviour uiBehaviour = GetUIWidgetUIBehaviour(uiWidgetName);
if (uiBehaviour != null)
{
uiBehaviour.ChangeTextContent(content);
}
}
#endregion
private void OnDestroy()
{
if (allChildrenDic!=null)
{
allChildrenDic.Clear();
allChildrenDic = null;
}
}
}
6、TmpPanel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TmpPanel : UIPanelBase
{
TmpPanelLogic tmpPanelLogic;
TmpPanelModel mTmpPanelModel;
string[] itemNameArray;
private void Start()
{
mTmpPanelModel = GameStart.Instance.mTmpPanelModel;
tmpPanelLogic = new TmpPanelLogic(this, mTmpPanelModel);
InitAndAddListener();
}
void InitAndAddListener() {
tmpPanelLogic.InitAndBindEvents();
GameObject itemPrefab = Resources.Load<GameObject>(ResourcesPath.Item);
itemNameArray = new string[3];
for (int i = 0; i < 3; i++)
{
Transform parent = GetUIWidgetGameObject(UIWidegetNameString.Grid_N).transform;
string itemName = UIWidegetNameString.ItemHeader + i + UIWidegetNameString.ItemTailer;
itemNameArray[i] = itemName;
GameObject tmpObj = InstantiateGridItems(itemPrefab, itemName, parent);
Item item = tmpObj.AddComponent<Item>();
item.index = i;
tmpPanelLogic.InitTextItemButton_S(itemNameArray,i);
AddButtonListener(itemNameArray[item.index], UIWidegetNameString.ItemButton_S, ()=> {
tmpPanelLogic.ItemButton_S(itemNameArray, item.index);
});
}
AddButtonListener(UIWidegetNameString.Button_N,tmpPanelLogic.Button_N);
AddInputFieldValueChangedListener(UIWidegetNameString.InputField_N, tmpPanelLogic.InputField_N);
AddSliderListener(UIWidegetNameString.Slider_N, tmpPanelLogic.Slider_N);
}
}
7、TmpPanelLogic
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TmpPanelLogic
{
private TmpPanelModel mTmpPanelModel;
private TmpPanel mTmpPanel;
public TmpPanelLogic(TmpPanel mTmpPanel, TmpPanelModel mTmpPanelModel)
{
this.mTmpPanel = mTmpPanel;
this.mTmpPanelModel = mTmpPanelModel;
}
public void InitAndBindEvents() {
mTmpPanel.ChangeTextContent(UIWidegetNameString.Text_N, "Test Text_N Click : " + mTmpPanelModel.ClickCount1);
mTmpPanel.ChangeTextContent(UIWidegetNameString.InputField_Text_N, mTmpPanelModel.InputFieldStr);
mTmpPanel.ChangeTextContent(UIWidegetNameString.Slider_Text_N, "value : " + mTmpPanelModel.SliderValue);
mTmpPanelModel.onClickCount1 = (count) => { mTmpPanel.ChangeTextContent(UIWidegetNameString.Text_N, "Test Text_N Click : " + count); };
mTmpPanelModel.onInputFieldStr = (content) => { mTmpPanel.ChangeTextContent(UIWidegetNameString.InputField_Text_N, content); };
mTmpPanelModel.onSliderValue = (value) => { mTmpPanel.ChangeTextContent(UIWidegetNameString.Slider_Text_N, "value : " + value); };
}
public void InitTextItemButton_S(string[] itemNameArray, int index) {
switch (index)
{
case 0:
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + mTmpPanelModel.ClickCount2);
break;
case 1:
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + mTmpPanelModel.ClickCount3);
break;
case 2:
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + mTmpPanelModel.ClickCount4);
break;
}
}
public void Button_N() {
Debug.Log("Button_N click");
mTmpPanelModel.ClickCount1++;
}
public void InputField_N(string str)
{
Debug.Log("InputField_N :" + str);
mTmpPanelModel.InputFieldStr= str;
}
public void Slider_N(float value)
{
Debug.Log("Slider_N :" + value);
mTmpPanelModel.SliderValue = value;
}
public void ItemButton_S(string[] itemNameArray, int index) {
switch (index)
{
case 0:
Debug.Log("i : " + 0);
mTmpPanelModel.onClickCount2 = (count) =>
{
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + count);
};
mTmpPanelModel.ClickCount2++;
break;
case 1:
Debug.Log("i : " + 1);
mTmpPanelModel.onClickCount3 = (count) =>
{
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + count);
};
mTmpPanelModel.ClickCount3++;
break;
case 2:
Debug.Log("i : " + 2);
mTmpPanelModel.onClickCount4 = (count) =>
{
mTmpPanel.ChangeTextContent(itemNameArray[index], UIWidegetNameString.ItemText_S, "Test " + itemNameArray[index] + " : " + count);
};
mTmpPanelModel.ClickCount4++;
break;
default:
break;
}
}
}
8、TmpPanelModel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class TmpPanelModel
{
private int clickCount1;
private int clickCount2;
private int clickCount3;
private int clickCount4;
private string inputFieldStr;
private float sliderValue;
public UnityAction<int> onClickCount1;
public UnityAction<int> onClickCount2;
public UnityAction<int> onClickCount3;
public UnityAction<int> onClickCount4;
public UnityAction<string> onInputFieldStr;
public UnityAction<float> onSliderValue;
public TmpPanelModel(int clickCount1, int clickCount2, int clickCount3, int clickCount4, string inputFieldStr, float sliderValue)
{
this.clickCount1 = clickCount1;
this.clickCount2 = clickCount2;
this.clickCount3 = clickCount3;
this.clickCount4 = clickCount4;
this.inputFieldStr = inputFieldStr;
this.sliderValue = sliderValue;
}
public int ClickCount1
{
get => clickCount1;
set
{
clickCount1 = value;
if (onClickCount1 != null)
{
onClickCount1(clickCount1);
}
}
}
public int ClickCount2 { get => clickCount2; set
{
clickCount2 = value;
if (onClickCount2 != null)
{
onClickCount2(clickCount2);
}
}
}
public int ClickCount3 { get => clickCount3; set
{
clickCount3 = value;
if (onClickCount3 != null)
{
onClickCount3(clickCount3);
}
}
}
public int ClickCount4 { get => clickCount4; set
{
clickCount4 = value;
if (onClickCount4 != null)
{
onClickCount4(clickCount4);
}
}
}
public string InputFieldStr { get => inputFieldStr; set
{
inputFieldStr = value;
if (onInputFieldStr != null)
{
onInputFieldStr(inputFieldStr);
}
}
}
public float SliderValue { get => sliderValue; set
{
sliderValue = value;
if (onSliderValue != null)
{
onSliderValue(sliderValue);
}
}
}
}
9、Item
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item : MonoBehaviour
{
public int index;
}
10、MonoSingleton
using UnityEngine;
public abstract class MonoSingleton<T> : MonoBehaviour where T : MonoBehaviour
{
private static T instance = null;
private static readonly object locker = new object();
private static bool bAppQuitting;
public static T Instance
{
get
{
if (bAppQuitting)
{
instance = null;
return instance;
}
lock (locker)
{
if (instance == null)
{
// 保证场景中只有一个 单例
T[] managers = Object.FindObjectsOfType(typeof(T)) as T[];
if (managers.Length != 0)
{
if (managers.Length == 1)
{
instance = managers[0];
instance.gameObject.name = typeof(T).Name;
return instance;
}
else
{
Debug.LogError("Class " + typeof(T).Name + " exists multiple times in violation of singleton pattern. Destroying all copies");
foreach (T manager in managers)
{
Destroy(manager.gameObject);
}
}
}
var singleton = new GameObject();
instance = singleton.AddComponent<T>();
= "(singleton)" + typeof(T);
singleton.hideFlags = HideFlags.None;
DontDestroyOnLoad(singleton);
}
instance.hideFlags = HideFlags.None;
return instance;
}
}
}
protected virtual void Awake()
{
bAppQuitting = false;
}
protected virtual void OnDestroy()
{
bAppQuitting = true;
}
}
11、UIWidegetNameString
public class UIWidegetNameString
{
// UIWideget 名字
public const string Button_N = "Button_N";
public const string Text_N = "Text_N";
public const string InputField_N = "InputField_N";
public const string Slider_N = "Slider_N";
public const string Grid_N = "Grid_N";
public const string ItemText_S = "ItemText_S";
public const string ItemButton_S = "ItemButton_S";
public const string InputField_Text_N = "InputField_Text_N";
public const string Slider_Text_N = "Slider_Text_N";
// 代码生成的 Item UIWideget 命名相关字符串
public const string ItemHeader = "Item";
public const string ItemTailer = "_C";
}
12、ResourcesPath
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourcesPath
{
public const string Item = "Prefabs/UI/Item/Item";
public const string TmpPanel = "Prefabs/UI/Panel/TmpPanel";
}