拖动UI组件(坐标转换)

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class MoveDirections : MonoBehaviour
{
  //控制四个方向的按钮
  public GameObject MoveUp;
  public GameObject MoveDown;
  public GameObject MoveLeft;
  public GameObject MoveRight;
  public GameObject DragRotateBtn;
  //控制方向的布尔变量
  private bool isUp = false;
  private bool isDown = false;
  private bool isLeft = false;
  private bool isRight = false;

  public Camera UICamera;
  Vector3 scenePos;
  Vector3 mous;
  Vector3 tranScreenPos;
  Vector3 TempMouse;

  void Start()
  {

    EventTriggerListener.Get(gameObject).onDrag = OnMouseDragUI;
    EventTriggerListener.Get(DragRotateBtn).onDrag = OnMouseDragUI;

    EventTriggerListener.Get(gameObject).onDown = OnGameOnDown;
    EventTriggerListener.Get(DragRotateBtn).onDown = OnGameOnDown;

    EventTriggerListener.Get(MoveLeft).onDown += OnDownBtnClick;
    EventTriggerListener.Get(MoveRight).onDown += OnDownBtnClick;
    EventTriggerListener.Get(MoveDown).onDown += OnDownBtnClick;
    EventTriggerListener.Get(MoveUp).onDown += OnDownBtnClick;

    EventTriggerListener.Get(MoveLeft).onUp += OnUpBtnClick;
    EventTriggerListener.Get(MoveRight).onUp += OnUpBtnClick;
    EventTriggerListener.Get(MoveDown).onUp += OnUpBtnClick;
    EventTriggerListener.Get(MoveUp).onUp += OnUpBtnClick;
  }
  void Update()
  {
    if (isLeft && Input.GetMouseButton(0))
    {
      Debug.Log("向左移动");
    }
    if (isRight && Input.GetMouseButton(0))
    {
      Debug.Log("向右移动");
    }
    if (isUp && Input.GetMouseButton(0))
    {
      Debug.Log("向上移动");
    }
    if (isDown && Input.GetMouseButton(0))
    {
      Debug.Log("向下移动");
    }
  }
  /// <summary>
  /// 按下鼠标时判断按下的按钮
  /// </summary>
  /// <param name="btn"></param>
  void OnDownBtnClick(GameObject btn)
  {
    isLeft = (btn == MoveLeft);
    isRight = (btn == MoveRight);
    isUp = (btn == MoveUp);
    isDown = (btn == MoveDown);
  }
  void OnGameOnDown(GameObject ga)
  {
    mous = Input.mousePosition;//记录下鼠标坐标
    tranScreenPos = UICamera.WorldToScreenPoint(transform.position);
  }
  /// <summary>
  /// 鼠标抬起控制方向变量重新设置为false;
  /// </summary>
  /// <param name="game"></param>
  void OnUpBtnClick(GameObject game)
  {
  isLeft = false;
  isRight = false;
  isUp = false;
  isDown = false;
  }
  /// <summary>
  /// 点击方向按钮
  /// </summary>
  /// <param name="btn"></param>
  void OnMouseDragUI(GameObject btn)
  {
    TempMouse = mous - Input.mousePosition;
    scenePos = new Vector3(tranScreenPos.x - TempMouse.x, tranScreenPos.y - TempMouse.y, tranScreenPos.z);
  transform.position = UICamera.ScreenToWorldPoint(scenePos);
  }
}

 

转载于:https://www.cnblogs.com/Cocomo/p/5688486.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值