From 7f493f682503f5186308de7b8f74b5b49233cfe4 Mon Sep 17 00:00:00 2001 From: chai <215380520@qq.com> Date: Thu, 2 Nov 2023 11:51:31 +0800 Subject: +init --- Rewired/Rewired.Demos/UIPointer.cs | 98 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Rewired/Rewired.Demos/UIPointer.cs (limited to 'Rewired/Rewired.Demos/UIPointer.cs') diff --git a/Rewired/Rewired.Demos/UIPointer.cs b/Rewired/Rewired.Demos/UIPointer.cs new file mode 100644 index 0000000..c65fdae --- /dev/null +++ b/Rewired/Rewired.Demos/UIPointer.cs @@ -0,0 +1,98 @@ +using UnityEngine; +using UnityEngine.EventSystems; +using UnityEngine.UI; + +namespace Rewired.Demos; + +[AddComponentMenu("")] +[RequireComponent(typeof(RectTransform))] +public sealed class UIPointer : UIBehaviour +{ + [Tooltip("Should the hardware pointer be hidden?")] + [SerializeField] + private bool _hideHardwarePointer = true; + + [Tooltip("Sets the pointer to the last sibling in the parent hierarchy. Do not enable this on multiple UIPointers under the same parent transform or they will constantly fight each other for dominance.")] + [SerializeField] + private bool _autoSort = true; + + private Canvas _canvas; + + public bool autoSort + { + get + { + return _autoSort; + } + set + { + if (value != _autoSort) + { + _autoSort = value; + if (value) + { + base.transform.SetAsLastSibling(); + } + } + } + } + + protected override void Awake() + { + base.Awake(); + Graphic[] componentsInChildren = GetComponentsInChildren(); + for (int i = 0; i < componentsInChildren.Length; i++) + { + componentsInChildren[i].raycastTarget = false; + } + if (_hideHardwarePointer) + { + Cursor.visible = false; + } + if (_autoSort) + { + base.transform.SetAsLastSibling(); + } + GetDependencies(); + } + + private void Update() + { + if (_autoSort && base.transform.GetSiblingIndex() < base.transform.parent.childCount - 1) + { + base.transform.SetAsLastSibling(); + } + } + + protected override void OnTransformParentChanged() + { + base.OnTransformParentChanged(); + GetDependencies(); + } + + protected override void OnCanvasGroupChanged() + { + base.OnCanvasGroupChanged(); + GetDependencies(); + } + + public void OnScreenPositionChanged(Vector2 screenPosition) + { + if (!(_canvas == null)) + { + Camera cam = null; + RenderMode renderMode = _canvas.renderMode; + if (renderMode != 0 && (uint)(renderMode - 1) <= 1u) + { + cam = _canvas.worldCamera; + } + RectTransformUtility.ScreenPointToLocalPointInRectangle(base.transform.parent as RectTransform, screenPosition, cam, out var localPoint); + base.transform.localPosition = new Vector3(localPoint.x, localPoint.y, base.transform.localPosition.z); + } + } + + private void GetDependencies() + { + _canvas = base.transform.root.GetComponentInChildren(); + } +} -- cgit v1.1-26-g67d0