1、挂在需要跟随鼠标的UI上(Image,Text等)
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class UIFollowMouse : MonoBehaviour { /// /// 需要跟随的物体 /// private GameObject go; private void Awake() { go = gameObject; } private void OnEnable() { ChoosePivot(); go.transform.position = Input.mousePosition; } /// /// 根据鼠标位置选择中心点,避免出现UI到屏幕外的情况 /// private void ChoosePivot() { float width = Screen.width / 2; float height = Screen.height / 2; if (Input.mousePosition.x < width) {