您的位置:首页 > 产品设计 > UI/UE

NGUI自定义事件(MonoBehaviour)&& 界面控件基类

2016-08-25 12:11 190 查看
自定义事件(MonoBehaviour),让它处理更多的事件

void OnHover (bool isOver) ? Sent out when the mouse hovers over the collider or moves away from it.Not sent on touch-based devices.
void OnPress (bool isDown) ? Sent when a mouse button (or touch event) gets pressed over the collider (with ‘true’) and when it gets released (with ‘false’, sent to the same collider even if it’s released elsewhere).
void OnClick() ? Sent to a mouse button or touch event gets released on the same collider as OnPress.UICamera.currentTouchID tells you which button was clicked.
void OnDoubleClick () ? Sent when the click happens twice within a fourth of a second.UICamera.currentTouchID tells you which button was clicked.
void OnSelect (bool selected) ? Same as OnClick, but once a collider is selected it will not receive any further OnSelect events until you select some other collider.
void OnDrag (Vector2 delta) ? Sent when the mouse or touch is moving in between of OnPress(true) and OnPress(false).
void OnDrop (GameObject drag) ? Sent out to the collider under the mouse or touch when OnPress(false) is called over a different collider than triggered the OnPress(true) event. The passed parameter is the game object of the collider that received the OnPress(true)
event.
void OnInput (string text) ? Sent to the same collider that received OnSelect(true) message after typing something. You likely won’t need this, but it’s used by UIInput
void OnTooltip (bool show) ? Sent after the mouse hovers over a collider without moving for longer thantooltipDelay, and when the tooltip should be hidden. Not sent on touch-based devices.
void OnScroll (float delta) is sent out when the mouse scroll wheel is moved.
void OnKey (KeyCode key) is sent when keyboard or controller input is used.详细目录

/// <summary>

/// 界面控件基类

/// </summary>

public class UISceneWidget : MonoBehaviour
{
DateTime OnClickTime;
public float Throughtime = 0.5f;
/// - OnHover (isOver) 悬停,悬停时传入true,移出时传入false
public delegate void onMouseHover (UISceneWidget eventObj, bool isOver);
public onMouseHover OnMouseHover = null;
void OnHover (bool isOver)
{
if (OnMouseHover != null) OnMouseHover(this, isOver);
}
/// - OnPress (isDown)按下时传入true,抬起时传入false
public delegate void onMousePress (UISceneWidget eventObj, bool isDown);
public onMousePress OnMousePress = null;
void OnPress (bool isDown)
{
if (OnMousePress != null) OnMousePress(this, isDown);
}
/// - OnSelect 相似单击,区别在于选中一次以后再选中将不再触发OnSelect
public delegate void onMouseSelect (UISceneWidget eventObj, bool selected);
public onMouseSelect OnMouseSelect = null;
void OnSelect
4000
(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 dictionary
Transform 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 dictionary
GameObject 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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐