一、UI中的点击,拖拉,移入,移出事件。
二、Drag,Drope 拖拽类 IBeginDragHandler,IDragHandler,IEndDragHandler(拖拽脚本的实现)
实现鼠标拖拽UI移动
代码展示:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using System;
using UnityEngine.UI;
public class UIFloatPanelMove : MonoBehaviour,IBeginDragHandler,IDragHandler,IEndDragHandler
{
void Start()
{
}
void Update()
{
}
/// <summary>
/// 开始拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnBeginDrag(PointerEventData eventData)
{
}
/// <summary>
/// 推拽中
/// </summary>
/// <param name="eventData"></param>
public void OnDrag(PointerEventData eventData)
{
Vector3 offset = new Vector3(eventData.delta.x, eventData.delta.y, 0);
CanvasScaler canvasScaler = transform.parent.GetComponent<CanvasScaler>();
if (canvasScaler.uiScaleMode == CanvasScaler.ScaleMode.ScaleWithScreenSize)
{
float resolutionX = canvasScaler.referenceResolution.x;
float resolutionY = canvasScaler.referenceResolution.y;
float rate = (Screen.width / canvasScaler.referenceResolution.x) * (1 - canvasScaler.matchWidthOrHeight) + (Screen.height / canvasScaler.referenceResolution.y) * canvasScaler.matchWidthOrHeight;
offset /= rate;
}
transform.localPosition += offset;
}
/// <summary>
/// 结束拖拽
/// </summary>
/// <param name="eventData"></param>
public void OnEndDrag(PointerEventData eventData)
{
}
三、Pointer鼠标指针类 IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler,IPointerUpHandler(鼠标点击,移入,移出的控制)
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEngine;
using SpacePlanner;
using System;
using SpacePlanner.Core.DataStructure;
//父类,子类可以重写这些方法,进行扩充
/// <summary>
/// 所有UI物体的父类
/// </summary>
public class UIObject : MaskableGraphic, IPointerClickHandler, IScrollHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler
{
public Base currentVO;
public RectTransform body;
/// <summary>
/// 全局控制当前选中的Object
/// </summary>
public static UIObject currentSelected = null;
internal virtual void Redraw()
{
}
internal virtual void Redraw_Alan()
{
}
/// <summary>
/// 鼠标是否悬停在上面
/// </summary>
/// <param name="isHover"></param>
internal virtual void OnHover(bool isHover)
{
}
/// <summary>
/// 是否被选中
/// </summary>
/// <param name="isSelected"></param>
internal virtual void OnSelected(bool isSelected)
{
}
#region 封装多一层,用于外部调用改变选中状态
public void Select()
{
if (currentSelected != null)
currentSelected.Unselect();
else
OnSelected(true);
currentSelected = this;
}
public void Unselect()
{
OnSelected(false);
currentSelected = null;
}
#endregion
/// <summary>
/// 不做特别的判断
/// </summary>
/// <param name="screenPoint"></param>
/// <param name="eventCamera"></param>
/// <returns></returns>
public virtual bool IsRaycastLocationValid(Vector2 screenPoint, Camera eventCamera)
{
return true;
}
#region EventHandle
/// <summary>
/// 屏蔽掉拖拉时触发的OnPointerClick,建议子类重写该函数
/// </summary>
/// <param name="eventDat"></param>
public virtual void OnClick(PointerEventData eventData)
{
}
public void OnPointerClick(PointerEventData eventData)
{
if (Input.GetMouseButtonUp(0))//只响应左键
{
if (!eventData.dragging)
OnClick(eventData);
}
}
/// <summary>
/// 考虑到目前所有继承于他的元素都需要将滚轮事件传递下去,自身不处理该事件
/// </summary>
/// <param name="eventData"></param>
public void OnScroll(PointerEventData eventData)
{
EventSystem.current.PropgateEvent(eventData, ExecuteEvents.scrollHandler, gameObject);
}
public void OnPointerEnter(PointerEventData eventData)
{
if (currentSelected != null) return;
OnHover(true);
}
public void OnPointerExit(PointerEventData eventData)
{
if (currentSelected != null) return;
OnHover(false);
}
public void OnPointerDown(PointerEventData eventData)
{
if (Input.GetMouseButtonDown(0))//只响应左键
Select();
}
#endregion
}
子类的扩充,已实现自己想要的效果
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Sprites;
using UnityEngine.EventSystems;
using SpacePlanner;
using SpacePlanner.Core.DataStructure;
public class UIWall : UIObject, ICanvasRaycastFilter, IBeginDragHandler, IDragHandler, IEndDragHandler
{
internal override void OnHover(bool isHover)
{
base.OnHover(isHover);
if (isHover)
color = new Color(hoverColor.r, hoverColor.g, hoverColor.b, color.a);
else
color = new Color(normalColor.r, normalColor.g, normalColor.b, color.a);
}
internal override void OnSelected(bool isSelected)
{
base.OnSelected(isSelected);
if (isSelected)
color = new Color(hoverColor.r, hoverColor.g, hoverColor.b, color.a);
else
color = new Color(normalColor.r, normalColor.g, normalColor.b, color.a);
}
public override void OnClick(PointerEventData eventData)
{
base.OnClick(eventData);
//Alan编写
//检测墙的链接状态
WallConnectState state = WallConnectMessage.WallConnectCheck(SPApplication.Instance.Space, (Wall)currentVO);
CreateFloatUI(state);
}
public void OnBeginDrag(PointerEventData eventData)
{
//FPBApplication.Instance.SendNotification(ApplicationConstants.TO_C_BEGIN_MOVE_WALL, currentVO);
}
public void OnDrag(PointerEventData eventData)
{
VOAndVector3 data = new VOAndVector3(currentVO as Wall, TransformHelper.ScreenToRealWorldPoint(Input.mousePosition));
//FPBApplication.Instance.SendNotification(ApplicationConstants.TO_C_MOVING_WALL, data);
}
//IEndDragHandler
public void OnEndDrag(PointerEventData eventData)
{
//FPBApplication.Instance.SendNotification(ApplicationConstants.TO_C_MOVE_WALL_DONE, currentVO);
}
}
四、Select点选类 IUpdateSelectedHandle ISelectHandler IDeselectHandler
五、Input输入类 IScrollHandler,IMoveHandler ISubmitHandler ICancelHandelr
六、扩展文献,程序动态设置实现。
http://blog.csdn.net/baidu_28955655/article/details/51387446