public void Awake() { // Get component references and fix values this.canvas = GetComponentInParent<Canvas>(); this.height = responsive ? Mathf.Max(0, Mathf.Min(height, 100)) : Mathf.Max(0, height); this.rectTransform = GetComponent<RectTransform>(); this.width = responsive ? Mathf.Max(0, Mathf.Min(width, 100)) : Mathf.Max(0, width); // Get reference height and width this.originHeight = (canvas.renderMode == RenderMode.ScreenSpaceOverlay) ? Screen.height : canvas.GetComponent<RectTransform> ().sizeDelta.y; this.originWidth = (canvas.renderMode == RenderMode.ScreenSpaceOverlay) ? Screen.width : canvas.GetComponent<RectTransform> ().sizeDelta.x; // Apply settings Invalidate(); }
void Start() { canvas = FindObjectOfType(typeof(Canvas)) as Canvas; canvas.GetComponent <UnityEngine.UI.CanvasScaler>().referencePixelsPerUnit = 64; Tooltip = Instantiate(FindObjectOfType <GUIPrefabs>().Tooltip, canvas.transform); //Tooltip. ScriptLoader.LoadScript("UI", "UI/UI.lua"); ScriptLoader.RegisterPlaceolder("UI", typeof(GUI.Button)); ScriptLoader.RegisterPlaceolder("UI", typeof(GUI.Panel)); ScriptLoader.RegisterPlaceolder("UI", typeof(GUI.Label)); ScriptLoader.RegisterPlaceolder("UI", typeof(GUI.Image)); ScriptLoader.RegisterPlaceolder("UI", typeof(UnityEngine.Color)); ScriptLoader.RegisterPlaceolder("UI", typeof(SpriteInfo)); ScriptLoader.RegisterPlaceolder("UI", typeof(UI.Input)); ScriptLoader.RegisterPlaceolder("UI", typeof(Verse)); ScriptLoader.RegisterPlaceolder("UI", typeof(Coordinate)); ScriptLoader.RegisterPlaceolder("UI", typeof(Projectile)); BuildUI(); }
public Rect GetCanvasRect(RectTransform t, Canvas c) { t.GetWorldCorners(this.m_WorldCorners); Transform component = c.GetComponent<Transform>(); for (int index = 0; index < 4; ++index) this.m_CanvasCorners[index] = component.InverseTransformPoint(this.m_WorldCorners[index]); return new Rect(this.m_CanvasCorners[0].x, this.m_CanvasCorners[0].y, this.m_CanvasCorners[2].x - this.m_CanvasCorners[0].x, this.m_CanvasCorners[2].y - this.m_CanvasCorners[0].y); }
public void Start() { // we need the root canvas Transform t = transform; while (canvas == null && t != null || !canvas.isRootCanvas) { canvas = t.GetComponent<Canvas>(); t = t.parent; } canvasRT = canvas.GetComponent<RectTransform>(); }
public Rect GetCanvasRect(RectTransform t, Canvas c) { if (c == null) { return new Rect(); } t.GetWorldCorners(this.m_WorldCorners); Transform component = c.GetComponent<Transform>(); for (int i = 0; i < 4; i++) { this.m_CanvasCorners[i] = component.InverseTransformPoint(this.m_WorldCorners[i]); } return new Rect(this.m_CanvasCorners[0].x, this.m_CanvasCorners[0].y, this.m_CanvasCorners[2].x - this.m_CanvasCorners[0].x, this.m_CanvasCorners[2].y - this.m_CanvasCorners[0].y); }
public static void SetWorldCanvas(Canvas canvas) { if (canvas.renderMode != RenderMode.WorldSpace) { return; } //copy public params then disable existing graphic raycaster var defaultRaycaster = canvas.gameObject.GetComponent<GraphicRaycaster>(); var customRaycaster = canvas.gameObject.GetComponent<VRTK_UIGraphicRaycaster>(); //if it doesn't already exist, add the custom raycaster if (!customRaycaster) { customRaycaster = canvas.gameObject.AddComponent<VRTK_UIGraphicRaycaster>(); } if (defaultRaycaster && defaultRaycaster.enabled) { customRaycaster.ignoreReversedGraphics = defaultRaycaster.ignoreReversedGraphics; customRaycaster.blockingObjects = defaultRaycaster.blockingObjects; defaultRaycaster.enabled = false; } //add a box collider and background image to ensure the rays always hit var canvasSize = canvas.GetComponent<RectTransform>().sizeDelta; if (!canvas.gameObject.GetComponent<BoxCollider>()) { var canvasBoxCollider = canvas.gameObject.AddComponent<BoxCollider>(); canvasBoxCollider.size = new Vector3(canvasSize.x, canvasSize.y, 10f); canvasBoxCollider.center = new Vector3(0f, 0f, 5f); } if (!canvas.gameObject.GetComponent<Image>()) { canvas.gameObject.AddComponent<Image>().color = Color.clear; } }
public IEnumerator ResetRender(int currentIteration, Camera camera, Canvas canvas) { var canvasScaler = canvas.GetComponent<CanvasScaler>(); if (canvasScaler != null) { canvasScaler.SendMessage("Update"); } var current = Selection.instanceIDs; Selection.activeGameObject = canvas.gameObject; Canvas.ForceUpdateCanvases(); var timeout = this.timeout; var time = UnityEditor.EditorApplication.timeSinceStartup; while (UnityEditor.EditorApplication.timeSinceStartup < time + timeout) { yield return false; } Canvas.ForceUpdateCanvases(); this.StartCoroutine(this.Render(currentIteration, camera, null)); if (currentIteration == this.currentIteration) { canvas.renderMode = RenderMode.ScreenSpaceCamera; } else { Debug.LogWarning("Out of sync: " + currentIteration + "/" + this.currentIteration); } Selection.instanceIDs = current; }
void Start() { // Disable UI Layer for Maincamera, leave others as-is int layerUI = LayerMask.NameToLayer("UI"); Camera.main.cullingMask = ~(1 << layerUI); _canvasMouseCursorDuplicate = GameObject.Find("CanvasMouseCursorDuplicate").GetComponent<Canvas>(); _canvasMouseCursorDuplicateRectTransform = _canvasMouseCursorDuplicate.GetComponent<RectTransform>(); FetchCameras(); CreateSettings(); AdjustCameras(); AdjustSecondaryCameras(); PlaceUIElements(); // After configuring alle cameras, don't render anything with obsolete Main-Camera in order to gain better performance Camera.main.cullingMask = 0; Camera.main.clearFlags = CameraClearFlags.Color; Camera.main.farClipPlane = 1.01f; Camera.main.nearClipPlane = 1f; }
public Calabash.Rect GetUIRect(GameObject gameObject, UnityEngine.Canvas canvas) { var rectTransform = gameObject.GetComponent <RectTransform>(); Vector3[] v = new Vector3[4]; rectTransform.GetWorldCorners(v); FileLog.Log(v[0].x + " " + v[0].y + " " + v[0].z); FileLog.Log(v[1].x + " " + v[1].y + " " + v[1].z); FileLog.Log(v[2].x + " " + v[2].y + " " + v[2].z); FileLog.Log(v[3].x + " " + v[3].y + " " + v[3].z); FileLog.Log("CANVAS"); FileLog.Log(canvas.GetComponent <RectTransform>().rect.x.ToString()); FileLog.Log(canvas.GetComponent <RectTransform>().rect.y.ToString()); FileLog.Log(canvas.GetComponent <RectTransform>().rect.width.ToString()); FileLog.Log(canvas.GetComponent <RectTransform>().rect.height.ToString()); FileLog.Log(canvas.GetComponent <RectTransform>().localScale.x.ToString()); FileLog.Log(canvas.GetComponent <RectTransform>().localScale.y.ToString()); FileLog.Log("RESOLUTION"); FileLog.Log(UnityEngine.Screen.currentResolution.width.ToString()); FileLog.Log(UnityEngine.Screen.currentResolution.height.ToString()); float scale = 0.0f; float screenWidth = 375.0f; if (Calabash.iOS.Device.generation == UnityEngine.iOS.DeviceGeneration.iPadAir1) { screenWidth = 768.0f; } if (Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight) { screenWidth = 667.0f; if (Calabash.iOS.Device.generation == UnityEngine.iOS.DeviceGeneration.iPadAir1) { screenWidth = 1024.0f; } } if (canvas.renderMode == RenderMode.ScreenSpaceCamera) { FileLog.Log("CANVAS IN CAMERA MODE"); FileLog.Log(canvas.worldCamera.orthographicSize.ToString()); FileLog.Log(canvas.worldCamera.aspect.ToString()); float cameraSize = canvas.worldCamera.orthographicSize; bool bb = false; bb |= Calabash.iOS.Device.generation == UnityEngine.iOS.DeviceGeneration.iPadAir1; bb |= Screen.orientation == ScreenOrientation.LandscapeLeft || Screen.orientation == ScreenOrientation.LandscapeRight; if (bb) { cameraSize = 2.0f * canvas.worldCamera.orthographicSize * canvas.worldCamera.aspect; } for (int i = 0; i < 4; i++) { v[i].x = v[i].x + 0.5f * cameraSize; v[i].y = -v[i].y + 0.5f * cameraSize / canvas.worldCamera.aspect; } scale = screenWidth / cameraSize; } else { float worldHeight = UnityEngine.Screen.currentResolution.height; for (int i = 0; i < 4; i++) { v[i].y = worldHeight - v[i].y; } scale = screenWidth / UnityEngine.Screen.currentResolution.width; } Calabash.Rect result = new Calabash.Rect { x = v[0].x, y = v[1].y, width = v[2].x - v[0].x, height = v[0].y - v[1].y }; result.x = scale * result.x; result.y = scale * result.y; result.width = scale * result.width; result.height = scale * result.height; result.center_x = result.x + 0.5f * result.width; result.center_y = result.y + 0.5f * result.height; return(result); }
Vector2 WorldToCanvas(Canvas a_canvas, Vector3 a_worldPosition, Camera a_camera) { Vector3 viewportPos = a_camera.WorldToViewportPoint(a_worldPosition); RectTransform canvasRect = a_canvas.GetComponent<RectTransform>(); return new Vector2((viewportPos.x * canvasRect.sizeDelta.x) - (canvasRect.sizeDelta.x * 0.5f), (viewportPos.y * canvasRect.sizeDelta.y) - (canvasRect.sizeDelta.y * 0.5f)); }
/// <summary> /// Initializes a new instance of the <see cref="CanvasProjectionParams"/> class. /// </summary> /// <param name="canvas"> The canvas. </param> public CanvasProjectionParams(Canvas canvas) { this.canvas = canvas; mode = canvas.renderMode; if (mode == RenderMode.ScreenSpaceOverlay) { rect = canvas.GetComponent<RectTransform>(); } else { camera = canvas.worldCamera ?? Camera.main; } }
/// <summary> /// Set the specified draggable object. /// </summary> /// <param name="newDrag">New drag.</param> public void Drag(RectTransform newDrag) { drag = newDrag; canvas = Utilites.FindCanvas(transform).GetComponent<Canvas>(); canvasRect = canvas.GetComponent<RectTransform>(); }