You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
CrowdControl/Assets/Feel/MMTools/Tools/MMGUI/MMUIFollowMouse.cs

28 lines
890 B
C#

using UnityEngine;
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
using UnityEngine.InputSystem;
#endif
namespace MoreMountains.Tools
{
/// <summary>
/// This component will let you have a UI object follow the mouse position
/// </summary>
public class MMUIFollowMouse : MonoBehaviour
{
public Canvas TargetCanvas { get; set; }
protected Vector2 _newPosition;
protected Vector2 _mousePosition;
protected virtual void LateUpdate()
{
#if !ENABLE_INPUT_SYSTEM || ENABLE_LEGACY_INPUT_MANAGER
_mousePosition = Input.mousePosition;
#else
_mousePosition = Mouse.current.position.ReadValue();
#endif
RectTransformUtility.ScreenPointToLocalPointInRectangle(TargetCanvas.transform as RectTransform, _mousePosition, TargetCanvas.worldCamera, out _newPosition);
transform.position = TargetCanvas.transform.TransformPoint(_newPosition);
}
}
}