/// <summary> /// Set the parent object base on the fit screen setting. /// </summary> protected void SetParentObjectByMode() { var canvas = JCS_Canvas.GuessCanvas(this.transform); var resizeUI = canvas.ResizeUI; if (canvas == null) { JCS_Debug.LogReminder("Doesn't use JCS_Canvas object"); return; } Transform parentObject; // if is Resize UI is enable than add Dialogue under resize ui transform if (JCS_UISettings.instance.RESIZE_UI) { parentObject = resizeUI.transform; } // Else we add it directly under the Canvas else { parentObject = canvas.canvas.transform; } // set it to parent this.gameObject.transform.SetParent(parentObject); }
/// <summary> /// Start sliding the screen out the scene. /// </summary> /// <param name="align"> align type </param> public void StartSlideOut(JCS_Align align, float time) { Vector2 sizeDelta = JCS_Canvas.GuessCanvas().AppRect.sizeDelta; float imageSize = 1200; float distanceX = sizeDelta.x + imageSize; float distanceY = sizeDelta.y + imageSize; // NOTE(jenchieh): this is just some tweeking. mTweener.DurationX = time; mTweener.DurationY = time; mTweener.DurationZ = time; Vector3 tweenTo = this.transform.localPosition; switch (align) { // going left showing from right // ============-------------- case JCS_Align.ALIGN_RIGHT: { tweenTo.x = JCS_Mathf.ToNegative(distanceX); } break; // going right showing from left // -----------============= case JCS_Align.ALIGN_LEFT: { tweenTo.x = JCS_Mathf.ToPositive(distanceX); } break; case JCS_Align.ALIGN_BOTTOM: { tweenTo.y = JCS_Mathf.ToPositive(distanceY); } break; case JCS_Align.ALIGN_TOP: { tweenTo.y = JCS_Mathf.ToNegative(distanceY); } break; } // start sliding mTweener.DoTween(tweenTo); }
public static void ReattachSelf(RectTransform trans, ReattachCallback callback) { if (trans == null || callback == null) return; var canvas = JCS_Canvas.GuessCanvas(); var parent = trans.parent; trans.SetParent(canvas.AppRect); if (callback != null) callback.Invoke(parent); trans.SetParent(parent); }
private void Start() { #if (UNITY_5_4_OR_NEWER) RectTransform appRect = JCS_Canvas.GuessCanvas().AppRect; // TODO(jenchieh): unknown reason that something changes this to // somewhere else. (since 5.4.0f3) Vector3 tempPos = appRect.localPosition; tempPos.z = 0; this.transform.localPosition = Vector3.zero; #endif this.transform.localEulerAngles = Vector3.zero; this.transform.localScale = Vector3.one; }
/// <summary> /// Transfer canvas space to world space. /// /// NOTE(jenchieh): this dont often use, cuz world space element /// dont often follow the ui element. /// </summary> /// <param name="targetCanvasPos"> canvas position to transfer. </param> /// <returns> world space position </returns> public Vector3 CanvasToWorldSpace(Vector2 targetCanvasPos) { Camera cam = GetCamera(); //first you need the RectTransform component of your canvas RectTransform canvasRect = JCS_Canvas.GuessCanvas().AppRect; Vector2 canvasObject_WorldPosition = new Vector2( ((targetCanvasPos.x + (canvasRect.sizeDelta.x * JCS_Mathf.T_HALF)) / canvasRect.sizeDelta.x), ((targetCanvasPos.y + (canvasRect.sizeDelta.y * JCS_Mathf.T_HALF)) / canvasRect.sizeDelta.y)); Vector3 worldPos = cam.ViewportToWorldPoint(canvasObject_WorldPosition); //now you can set the position to the world element return(worldPos); }
private void Start() { // Set the canvas to be our root. this.transform.SetParent(JCS_Canvas.GuessCanvas(transform).transform); // Add panel root without losing the original size. { Vector2 originalSize = mRectTransform.sizeDelta; this.mPanelRoot = this.gameObject.AddComponent <JCS_PanelRoot>(); mRectTransform.sizeDelta = originalSize; } SetToScreenEdge(); }
/// <summary> /// Check weather the "type" in the screen space. (Sprite Renderer) /// </summary> /// <param name="checkTrans"> sprite renderer to check. (Sprite width & height) </param> /// <returns> /// true: in screen space, /// false: not in screen space /// </returns> public bool CheckInScreenSpace(SpriteRenderer checkTrans) { Vector2 objectRect = JCS_Util.GetSpriteRendererRect(checkTrans); Camera cam = main.GetCamera(); Vector2 objPos = cam.WorldToViewportPoint(checkTrans.transform.position); Vector2 camPos = cam.WorldToViewportPoint(cam.transform.position); float objLeft = objPos.x - (objectRect.x / JCS_Mathf.D_HALF); float objRight = objPos.x + (objectRect.x / JCS_Mathf.D_HALF); float objTop = objPos.y + (objectRect.y / JCS_Mathf.D_HALF); float objBot = objPos.y - (objectRect.y / JCS_Mathf.D_HALF); RectTransform appRect = JCS_Canvas.GuessCanvas().AppRect; float camWidth = appRect.sizeDelta.x; float camHeight = appRect.sizeDelta.y; float camLeft = camPos.x - (camWidth / JCS_Mathf.D_HALF); float camRight = camPos.x + (camWidth / JCS_Mathf.D_HALF); float camTop = camPos.y + (camHeight / JCS_Mathf.D_HALF); float camBot = camPos.y - (camHeight / JCS_Mathf.D_HALF); #if (UNITY_EDITOR) Vector3 topLeft = new Vector3(objLeft, objTop, 0.0f); Vector3 topRight = new Vector3(objRight, objTop, 0.0f); Vector3 botRight = new Vector3(objRight, objBot, 0.0f); Vector3 botLeft = new Vector3(objLeft, objBot, 0.0f); Debug.DrawLine(topLeft, topRight); Debug.DrawLine(topLeft, botLeft); Debug.DrawLine(botRight, botLeft); Debug.DrawLine(topRight, botRight); #endif // TODO(JenChieh): Not done. if ((objRight < camLeft || objLeft > camRight) && (objTop < camBot || objBot > camTop)) { // out of screen. return(false); } return(true); }
/// <summary> /// Check to see if enable to spawn the dialogue or not /// </summary> /// <returns></returns> private static bool CheckIfOkayToSpawnDialogue(JCS_DialogueType type) { // Force Diaglogue have higher priority, // so it will block the lower priority dialogue type if (JCS_UIManager.instance.GetJCSDialogue(type) != null) { JCS_Debug.LogError("(" + type.ToString() + ")No able to spawn Dialogue cuz there are multiple dialogue in the scene..."); return(false); } if (JCS_Canvas.GuessCanvas() == null) { JCS_Debug.LogError("No able to spawn Dialogue cuz Canvas are null..."); return(false); } return(true); }
public static List<RectTransform> DetachChildren(RectTransform trans) { var childs = new List<RectTransform>(); var canvas = JCS_Canvas.GuessCanvas(); for (int index = 0; index < trans.childCount; ++index) { Transform child = trans.GetChild(index); var rect = child.GetComponent<RectTransform>(); if (rect == null) continue; childs.Add(rect); // Remove from parent. child.SetParent(canvas.AppRect); } return childs; }
/// <summary> /// Make a canvas space object to a world space position. /// /// NOTE(jenchieh): Make UI object (canvas space) on top of the /// world space game object. /// </summary> /// <param name="pos"></param> /// <returns></returns> public Vector3 CastToScreen(Vector3 pos) { var camera = JCS_Camera.main; Vector3 positionOffset = mPositionOffset; if (mPanelRoot != null) { positionOffset.x *= mPanelRoot.PanelDeltaWidthRatio; positionOffset.y *= mPanelRoot.PanelDeltaHeightRatio; } switch (GetObjectType()) { case JCS_UnityObjectType.TEXT: case JCS_UnityObjectType.UI: { Vector2 worldToCanvasSpace = camera.WorldToCanvasSpace(pos); var canvas = JCS_Canvas.GuessCanvas(); var resizeUI = canvas.ResizeUI; float targetScale = resizeUI.TargetScale; if (targetScale != 0.0f) { worldToCanvasSpace.x /= resizeUI.TargetScale; worldToCanvasSpace.y /= resizeUI.TargetScale; } this.LocalPosition = worldToCanvasSpace + (Vector2)positionOffset; } break; } return(this.LocalPosition); }
/// <summary> /// Check weather the "type" in the screen space. (RectTransform) /// </summary> /// <param name="checkTrans"> rect transform's size, etc. </param> /// <returns> /// true: in screen space, /// false: not in screen space /// </returns> public bool CheckInScreenSpace(RectTransform checkTrans) { Vector2 rectSize = checkTrans.sizeDelta; Vector3 panelPos = checkTrans.localPosition; float halfSlotWidth = rectSize.x / JCS_Mathf.D_HALF * checkTrans.localScale.x; float halfSlotHeight = rectSize.y / JCS_Mathf.D_HALF * checkTrans.localScale.y; float panelLeftBorder = panelPos.x - halfSlotWidth; float panelRightBorder = panelPos.x + halfSlotWidth; float panelTopBorder = panelPos.y + halfSlotHeight; float panelBottomBorder = panelPos.y - halfSlotHeight; Camera cam = main.GetCamera(); Vector3 camPos = cam.transform.position; // Transfer 3D space to 2D space Vector2 camPosToScreen = cam.WorldToScreenPoint(camPos); // Get application rect RectTransform appRect = JCS_Canvas.GuessCanvas().AppRect; Vector2 screenRect = appRect.sizeDelta; float camLeftBorder = camPosToScreen.x - screenRect.x / JCS_Mathf.D_HALF; float camRightBorder = camPosToScreen.x + screenRect.x / JCS_Mathf.D_HALF; float camTopBorder = camPosToScreen.y + screenRect.y / JCS_Mathf.D_HALF; float camBottomBorder = camPosToScreen.y - screenRect.y / JCS_Mathf.D_HALF; if (panelRightBorder - rectSize.x > camRightBorder || panelLeftBorder + rectSize.x < camLeftBorder || panelTopBorder - rectSize.y > camTopBorder || panelBottomBorder + rectSize.y < camBottomBorder) { return(false); } return(true); }
private Vector3 GameDepthRect(Vector3 depth) { // Next step: find camera 4 bounds. Camera cam = main.GetCamera(); var canvas = JCS_Canvas.GuessCanvas(); Vector3 camPos = cam.transform.position; // only need to know the depth. { camPos.x = 0.0f; camPos.y = 0.0f; } Vector3 canvasPos = canvas.transform.position; // only need to know the depth. { canvasPos.x = 0.0f; canvasPos.y = 0.0f; } float camToCanvasDistance = Vector3.Distance(camPos, canvasPos); float camToGameDepthDistance = Vector3.Distance(camPos, depth); RectTransform appRect = canvas.AppRect; Vector2 canvasRect = appRect.sizeDelta; // transfer rect from screen space to world space { canvasRect.x *= appRect.localScale.x; canvasRect.y *= appRect.localScale.y; } return(new Vector3( camToGameDepthDistance * canvasRect.x / camToCanvasDistance, camToGameDepthDistance * canvasRect.y / camToCanvasDistance, 0)); }
/// <summary> /// Transfer world space to canvas space. /// Source -> http://answers.unity3d.com/questions/799616/unity-46-beta-19-how-to-convert-from-world-space-t.html /// </summary> /// <param name="targetWorldPos"> world position to transfer. </param> /// <returns> Canvas space position </returns> public Vector2 WorldToCanvasSpace(Vector3 targetWorldPos) { Camera cam = GetCamera(); //first you need the RectTransform component of your canvas RectTransform canvasRect = JCS_Canvas.GuessCanvas().AppRect; //then you calculate the position of the UI element // 0,0 for the canvas is at the center of the screen, // whereas WorldToViewPortPoint treats // the lower left corner as 0,0. Because of // this, you need to subtract the height / width of // the canvas * 0.5 to get the correct position. Vector3 viewportPos = cam.WorldToViewportPoint(targetWorldPos); Vector2 worldObject_ScreenPosition = new Vector2( ((viewportPos.x * canvasRect.sizeDelta.x) - (canvasRect.sizeDelta.x * JCS_Mathf.T_HALF)), ((viewportPos.y * canvasRect.sizeDelta.y) - (canvasRect.sizeDelta.y * JCS_Mathf.T_HALF))); //now you can set the position to the ui element return(worldObject_ScreenPosition); }