Esempio n. 1
0
        /// <summary>
        /// Gets the canvas transform.
        /// </summary>
        /// <returns>The canvas transform.</returns>
        public static Transform GetCanvasTransform()
        {
            var canvas = (Selection.activeGameObject != null) ? Selection.activeGameObject.GetComponentInParent <Canvas>() : null;

            if (canvas == null)
            {
                canvas = UnityEngine.Object.FindObjectOfType <Canvas>();
            }

            if (canvas != null)
            {
                return(canvas.transform);
            }

            var canvasGO = new GameObject("Canvas");

            canvasGO.layer    = LayerMask.NameToLayer("UI");
            canvas            = canvasGO.AddComponent <Canvas>();
            canvas.renderMode = RenderMode.ScreenSpaceOverlay;
            canvasGO.AddComponent <CanvasScaler>();
            canvasGO.AddComponent <GraphicRaycaster>();
            Undo.RegisterCreatedObjectUndo(canvasGO, "Create " + canvasGO.name);

            if (UnityEngine.Object.FindObjectOfType <EventSystem>() == null)
            {
                Compatibility.CreateEventSystem();
            }

            return(canvasGO.transform);
        }