/// /// Created by SWAN DEV 2018 /// using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; /// /// Dynamic UI - Canvas Layer Handler script for handling UGUI canvas sorting layer: place the last clicked canvas on top layer. /// public class DCanvasLayerHandler : MonoBehaviour { /// The base sorting order of handled canvases. Canvas with larger sorting order will show on top. public static int baseLayer = 100; /// A list of Canvases to be handled by this script. private static List handledCanvasList = new List(); private static DCanvasLayerHandler _instance = null; public enum SortLayerEventType { OnTouchUp = 0, OnTouchDown, OnTap, } public SortLayerEventType sortLayerEventType = SortLayerEventType.OnTouchDown; float onTouchDownTime = 0f; float onTapInterval = 0.3f; bool isTouchDown = false; bool isTouchUp = true; GameObject touchedObject; PointerEventData cursor = new PointerEventData(EventSystem.current); List objectsHit = new List (); /// Set the target canvas to be managed by this handler, /// for automatically handle the canvas layers when the user drag/tap on the UI. public static void iAddCanvas(Canvas canvas, bool createCanvasLayerHandlerIfNotExist = true) { if(canvas == null) return; if (_instance == null && createCanvasLayerHandlerIfNotExist) { _instance = new GameObject("[DCanvasLayerHandler]").AddComponent(); } if(_instance && !handledCanvasList.Contains(canvas)) { canvas.sortingOrder = baseLayer + handledCanvasList.Count; handledCanvasList.Add(canvas); } } /// Set the target canvas to be managed by this handler, /// for automatically handle the canvas layers when the user drag/tap on the UI. public void AddCanvas(Canvas canvas) { iAddCanvas(canvas); } void Awake() { if(_instance == null) { _instance = this; } else { Debug.LogWarning("Duplicated DCanvasLayerHandler removed in gameObject: " + this.name); Destroy(this); } } void Update() { if(isTouchUp) { isTouchDown = SDev.Util.UnifyInputUtil.WasTouchOrMousePressed(); if(isTouchDown) { //A mouse/touch down event detected. isTouchUp = false; onTouchDownTime = Time.time; cursor.position = SDev.Util.UnifyInputUtil.GetCursorPosition(); EventSystem.current.RaycastAll(cursor, objectsHit); if(objectsHit.Count > 0) { touchedObject = objectsHit[0].gameObject; } if(sortLayerEventType == SortLayerEventType.OnTouchDown) _SortLayer(); } } else if(isTouchDown) { isTouchUp = SDev.Util.UnifyInputUtil.WasTouchOrMouseReleased(); if(isTouchUp) { //A mouse/touch up event detected. isTouchDown = false; if(sortLayerEventType == SortLayerEventType.OnTouchUp) { _SortLayer(); } else if(sortLayerEventType == SortLayerEventType.OnTap) { if(onTouchDownTime + onTapInterval > Time.time) { _SortLayer(); } } } } } private void _SortLayer() { cursor.position = SDev.Util.UnifyInputUtil.GetCursorPosition(); EventSystem.current.RaycastAll(cursor, objectsHit); if(objectsHit.Count > 0) { if(touchedObject != null && touchedObject == objectsHit[0].gameObject) { for(int i=0; i() != null) { getCanvas = tf.gameObject.GetComponent(); } } while(tf != root && getCanvas == null); if(getCanvas != null && handledCanvasList.Contains(getCanvas)) { handledCanvasList.Remove(getCanvas); handledCanvasList.Add(getCanvas); for(int i=0; i