using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; #if UNITY_EDITOR using UnityEditor; #endif namespace Rellac.Windows { public static class GUIWindowUtils { #if UNITY_EDITOR [MenuItem("GameObject/UI/Window")] public static void InstantiateWindow() { NewWindow(); } #endif public static GameObject NewWindow() { Object prefab = Resources.Load("WindowUIComponent"); Canvas canvas = GameObject.FindObjectOfType(); if (canvas == null) { // Create default canvas GameObject newCanvas = new GameObject("Canvas"); canvas = newCanvas.AddComponent(); canvas.renderMode = RenderMode.ScreenSpaceOverlay; newCanvas.AddComponent(); newCanvas.AddComponent(); if (GameObject.FindObjectOfType() == null) { // Create defalut EventSystem new GameObject("EventSystem").AddComponent(). // create new GameObject with EventSystem gameObject.AddComponent() // add Input Module #if UNITY_5_3_OR_NEWER ; // we don't need the Touch Input Module at 5.3+ #else .gameObject.AddComponent(); #endif } } GameObject go = (GameObject)GameObject.Instantiate(prefab); go.transform.SetParent(canvas.transform); go.transform.localPosition = Vector2.zero; go.name = "Window"; return go; } public static void SetPivot(this RectTransform rectTransform, Vector2 pivot) { if (rectTransform == null) return; Vector2 size = rectTransform.rect.size; Vector2 deltaPivot = rectTransform.pivot - pivot; Vector3 deltaPosition = new Vector3(deltaPivot.x * size.x, deltaPivot.y * size.y); rectTransform.pivot = pivot; rectTransform.localPosition -= deltaPosition; } public static Vector3 MousePosition() { var mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); return new Vector3(mousePos.x, mousePos.y, 0); } } }