NGUI自定义事件(MonoBehaviour)&& 界面控件基类
来源:互联网 发布:淘宝客手机版插件 编辑:程序博客网 时间:2024/06/15 02:09
自定义事件(MonoBehaviour),让它处理更多的事件
/// <summary>
/// 界面控件基类
/// </summary>
public class UISceneWidget : MonoBehaviour {DateTime OnClickTime;public float Throughtime = 0.5f;/// - OnHover (isOver) 悬停,悬停时传入true,移出时传入falsepublic delegate void onMouseHover (UISceneWidget eventObj, bool isOver);public onMouseHover OnMouseHover = null;void OnHover (bool isOver){if (OnMouseHover != null) OnMouseHover(this, isOver);}/// - OnPress (isDown)按下时传入true,抬起时传入falsepublic delegate void onMousePress (UISceneWidget eventObj, bool isDown);public onMousePress OnMousePress = null;void OnPress (bool isDown){if (OnMousePress != null) OnMousePress(this, isDown); }/// - OnSelect 相似单击,区别在于选中一次以后再选中将不再触发OnSelectpublic delegate void onMouseSelect (UISceneWidget eventObj, bool selected);public onMouseSelect OnMouseSelect = null;void OnSelect (bool selected){if (OnMouseSelect != null) OnMouseSelect(this, selected); }/// - OnClick 单击 Throughtime点击间隔时间public delegate void onMouseClick (UISceneWidget eventObj);public onMouseClick OnMouseClick = null;void OnClick (){if (Throughtime > (float)(DateTime.UtcNow - OnClickTime).TotalSeconds){return;}OnClickTime = DateTime.UtcNow;if (OnMouseClick != null)OnMouseClick(this);}/// - OnDoubleClick 双击(双击间隔小于0.25秒)时触发。public delegate void onMouseDoubleClick(UISceneWidget eventObj);public onMouseDoubleClick OnMouseDoubleClick = null;void OnDoubleClick (){if (OnMouseDoubleClick != null) OnMouseDoubleClick(this);}/// - OnDrag 按下并移动时触发,delta为传入的位移public delegate void onMouseDrag (UISceneWidget eventObj, Vector2 delta);public onMouseDrag OnMouseDrag = null;void OnDrag (Vector2 delta){if (OnMouseDrag != null) OnMouseDrag(this, delta); }public delegate void onMouseDrop (UISceneWidget eventObj, GameObject dropObject);public onMouseDrop OnMouseDrop = null;void OnDrop (GameObject dropObject){if (OnMouseDrop != null) OnMouseDrop(this, dropObject); }/// - OnInput (text) is sent when typing (after selecting a collider by clicking on it)./// - OnTooltip (show) is sent when the mouse hovers over a collider for some time without moving./// - OnScroll (float delta) is sent out when the mouse scroll wheel is moved./// - OnKey (KeyCode key) is sent when keyboard or controller input is used.}
界面管理父类
using UnityEngine;using System.Collections;using System.Collections.Generic;public enum ESceneType{Main = 0,Nomal = 1,Popup = 2,Announce = 3,}public class UIScene : MonoBehaviour {protected string mUIName = "";private Dictionary<string , UISceneWidget> mUIWidgets = new Dictionary<string, UISceneWidget>();public UIAnchor.Side side = UIAnchor.Side.Center;public ESceneType type = ESceneType.Nomal;protected virtual void Start(){this.FindChildWidgets(gameObject.transform);}protected virtual void Update(){}public virtual bool IsVisible(){return gameObject.activeSelf;}public virtual void SetVisible(bool visible){gameObject.SetActive(visible);}protected UISceneWidget GetWidget (string name){// If allready find out, return if (mUIWidgets.ContainsKey(name))return mUIWidgets[name];// Find out widget with name and add to dictionaryTransform t = gameObject.transform.FindChild(name);if (t == null) return null;UISceneWidget widget = t.gameObject.GetComponent<UISceneWidget>();if (widget != null){mUIWidgets.Add(widget.gameObject.name, widget);}return t.gameObject.GetComponent<UISceneWidget>();}protected T GetWidget<T> (string name) where T : Component{// Find out widget with name and add to dictionaryGameObject go = GameObject.Find(name);if (go == null) return null;T widget = go.GetComponent<T>();return widget;}private void FindChildWidgets(Transform t){UISceneWidget widget = t.gameObject.GetComponent<UISceneWidget>();if (widget != null){//Debug.LogWarning("FindChildWidgets Parent[" + t.name + "] " + t.gameObject.name);string name = t.gameObject.name;if (!mUIWidgets.ContainsKey(name)){mUIWidgets.Add(name , widget);}else{//Debug.LogWarning("Scene[" + this.transform.name + "]UISceneWidget[" + name + "]is exist!");}}for(int i = 0; i < t.childCount ; ++i){Transform child = t.GetChild(i);FindChildWidgets(child);}}}
例子:界面管理子类
using UnityEngine;using System.Collections;public class UIMenu : UIScene {private UISceneWidget mButton_Role;private UISceneWidget mButton_SKill;private UISceneWidget mButton_Backpack;protected override void Start () {base.Start();mButton_Role = GetWidget("Button_Charactor");if(mButton_Role != null)mButton_Role.OnMouseClick = this.ButtonRoleOnClick;mButton_SKill = GetWidget("Button_Skill");if(mButton_SKill != null)mButton_SKill.OnMouseClick = this.ButtonSKillOnClick;mButton_Backpack = GetWidget("Button_Backpack");if(mButton_Backpack != null)mButton_Backpack.OnMouseClick = this.ButtonBackpackOnClick;}private void ButtonRoleOnClick(UISceneWidget eventObj){//UIManager.Instance.SetVisible(UIName.UIRole, true);UIRole.Instance.SetRoleInfo();}private void ButtonSKillOnClick(UISceneWidget eventObj){UIManager.Instance.SetVisible(UIName.UISkillSelect, true);}private void ButtonBackpackOnClick(UISceneWidget eventObj){UIManager.Instance.SetVisible(UIName.UIBackpack, true);}}
全局UI管理单例
using UnityEngine;using System.Collections;using System.Collections.Generic;using DevelopEngine;public class UIName{public const string UIStandAlone = "UIScene_StandAlone";public const string UIRoleName = "UIScene_RoleName";public const string UIResetRole = "UIScene_ResetRole";public const string UIMessageBox = "UIScene_MessageBox";public const string UISelectRole = "UIScene_SelectRole";public const string UIRoleInfo = "UIScene_RoleInfo";public const string UIPlayerInfo = "UIScene_PlayerInfo";public const string UIMoeny = "UIScene_Moeny";public const string UIOther = "UIScene_Other";public const string UIBattle = "UIScene_Battle";public const string UIMenu = "UIScene_Menu";public const string UIRole = "UIScene_Role";public const string UISkillSelect = "UIScene_SkillSelect";public const string UIBackpack = "UIScene_Backpack";public const string UIStore = "UIScene_Store";public const string UIMessage = "UIScene_Message";public const string UIBattleHead = "UIScene_BattleHead";public const string UIMiniMap = "UIScene_MiniMap";public const string UIPopup = "UIScene_Popup";public const string UIBattleOver = "UIScene_BattleOver";public const string UIBattleOption = "UIScene_BattleOption";public const string UIVictory ="UIBattle_Victory";public const string UIGameStart = "UIScene_GameStart";public const string UIRegister = "UIScene_Register";public const string UILogin = "UIScene_Login";//public const string UIMessage = "UIScene_MessageBox";}public class UIManager : MonoSingleton<UIManager> {private Dictionary<string ,UIScene> mUIScene = new Dictionary<string, UIScene>();private Dictionary<UIAnchor.Side , GameObject> mUIAnchor = new Dictionary<UIAnchor.Side, GameObject>();public void InitializeUIs(){mUIAnchor.Clear();Object[] objs = FindObjectsOfType(typeof(UIAnchor));if (objs != null){foreach(Object obj in objs){UIAnchor uiAnchor = obj as UIAnchor;if (!mUIAnchor.ContainsKey(uiAnchor.side))mUIAnchor.Add(uiAnchor.side, uiAnchor.gameObject);}}mUIScene.Clear();Object[] uis = FindObjectsOfType(typeof(UIScene));if (uis != null){foreach (Object obj in uis){UIScene ui = obj as UIScene;ui.SetVisible(false);mUIScene.Add(ui.gameObject.name, ui);}}}public void SetVisible (string name, bool visible){if (visible && !IsVisible(name)){OpenScene(name);}else if (!visible && IsVisible(name)){CloseScene(name);}}public bool IsVisible (string name){UIScene ui = GetUI(name);if (ui != null)return ui.IsVisible();return false;}private UIScene GetUI(string name){UIScene ui;return mUIScene.TryGetValue(name , out ui) ? ui : null;}public T GetUI<T> (string name) where T : UIScene{return GetUI(name) as T;}private bool isLoaded(string name){if (mUIScene.ContainsKey(name)){return true;}return false;}private void OpenScene(string name){if (isLoaded(name)){mUIScene[name].SetVisible(true);}}private void CloseScene(string name){if (isLoaded(name)){mUIScene[name].SetVisible(false);}}//显示开始游戏场景一级界面public void SetStandAloneVisible(bool visible){SetVisible(UIName.UIStandAlone, visible);}//显示创建角色场景public void SetCreateRoleVisible(bool visible){SetVisible(UIName.UISelectRole, visible);SetVisible(UIName.UIRoleInfo, visible);}//显示主城场景一级界面public void SetMainCityVisible(bool visible){SetVisible(UIName.UIPlayerInfo, visible);SetVisible(UIName.UIMoeny, visible);SetVisible(UIName.UIOther, visible);SetVisible(UIName.UIMenu, visible);}//显示战斗场景一级界面public void SetBattleVisible(bool visible){SetVisible(UIName.UIBattleHead, visible);SetVisible(UIName.UIMiniMap, visible);SetVisible(UIName.UIBattleOption, visible);}//显示登录场景一级界面public void SetLoginVisible(bool visible){SetVisible(UIName.UIGameStart, visible);}}
1 1
- NGUI自定义事件(MonoBehaviour)&& 界面控件基类
- Untiy3D --- 组件基类MonoBehaviour
- MonoBehaviour类
- MonoBehaviour类
- 界面控件与事件(一、登录界面)
- C#中自定义控件隐藏基类成员(属性、方法和事件)的方法
- 用户控件自定义事件
- 控件的自定义事件
- 自定义控件的事件
- 自定义控件添加事件
- C#-控件-自定义事件
- 自定义控件加事件
- 自定义控件添加事件
- 自定义控件添加事件
- 自定义控件添加事件
- Winform自定义控件事件
- .net 自定义控件事件
- 自定义控件触动事件
- 结构体 枚举 typedef
- Linux基本命令(二)
- 16年夏天八月份开发经验
- urllib2 使用代理服务器
- ejabberd模块开发
- NGUI自定义事件(MonoBehaviour)&& 界面控件基类
- 羽化制作lomo照片
- ITSA [C_MM115-中] 奇妙數列
- 数据结构之线性结构--数组
- 文章标题
- 程序员(技术开发)修炼三部曲(单元测试之道、版本控制之道、项目自动化之道)
- 预处理指令 宏定义 条件编译指令 static和extern关键字
- 复杂数据类型的映射之枚举
- TaskStackBuilder使用