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