UI中心跟随鼠标移动

//脚本挂在需要跟随鼠标移动的UI上

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

public class CharacterControl : MonoBehaviour
{
  public Camera UICamera;
  Vector3 scenePos;
  void Start () 
  {
    EventTriggerListener.Get(gameObject).onDrag = OnMouseDrag;
  }
  void OnMouseDrag(GameObject img)
  {
    Vector2 mous = Input.mousePosition;

    scenePos = new Vector3(mous.x, mous.y, UICamera.WorldToScreenPoint(transform.position).z);
    transform.position = UICamera.ScreenToWorldPoint(scenePos);
  }
}

 

posted @ 2016-07-20 16:28  CocomoNo1  阅读(529)  评论(0编辑  收藏  举报