public GetWorldCorners ( Vector3 fourCornersArray ) : void | ||
fourCornersArray | Vector3 | Array that corners should be filled into. |
return | void |
public static Rect GetScreenRectFromRectTransform(RectTransform rectTransform) { Vector3[] corners = new Vector3[4]; rectTransform.GetWorldCorners(corners); float xMin = float.PositiveInfinity; float xMax = float.NegativeInfinity; float yMin = float.PositiveInfinity; float yMax = float.NegativeInfinity; for (int i = 0; i < 4; i++) { // For Canvas mode Screen Space - Overlay there is no Camera; best solution I've found // is to use RectTransformUtility.WorldToScreenPoint) with a null camera. Vector3 screenCoord = RectTransformUtility.WorldToScreenPoint(null, corners[i]); if (screenCoord.x < xMin) xMin = screenCoord.x; if (screenCoord.x > xMax) xMax = screenCoord.x; if (screenCoord.y < yMin) yMin = screenCoord.y; if (screenCoord.y > yMax) yMax = screenCoord.y; } Rect result = new Rect(xMin, Screen.height - yMax, xMax - xMin, yMax - yMin); return result; }
static int QPYX_GetWorldCorners_YXQP(IntPtr L_YXQP) { try { int QPYX_count_YXQP = LuaDLL.lua_gettop(L_YXQP); if (QPYX_count_YXQP == 1) { UnityEngine.RectTransform obj = (UnityEngine.RectTransform)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] QPYX_arg0_YXQP = new UnityEngine.Vector3[4]; obj.GetWorldCorners(QPYX_arg0_YXQP); ToLua.Push(L_YXQP, QPYX_arg0_YXQP); return(1); } else if (QPYX_count_YXQP == 2) { UnityEngine.RectTransform QPYX_obj_YXQP = (UnityEngine.RectTransform)ToLua.CheckObject(L_YXQP, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] QPYX_arg0_YXQP = ToLua.CheckStructArray <UnityEngine.Vector3>(L_YXQP, 2); QPYX_obj_YXQP.GetWorldCorners(QPYX_arg0_YXQP); return(0); } else { return(LuaDLL.luaL_throw(L_YXQP, "invalid arguments to method: UnityEngine.RectTransform.GetWorldCorners")); } } catch (Exception e_YXQP) { return(LuaDLL.toluaL_exception(L_YXQP, e_YXQP)); } }
public Vector3 WorldTopLeftCorner(RectTransform rt) { Vector3[] corners = new Vector3[4]; rt.GetWorldCorners(corners); int rot = (int)rt.eulerAngles.z; //Vector3 eulerSave = rt.eulerAngles; if(rot > 88 && rot < 92){ rot = 270; }else if(rot > 178 && rot < 182){ rot = 180; }else if(rot > 268 && rot < 272){ rot = 90; }else if(rot > 358 || rot < 2){ rot = 0; } //rot is now clockwise and right-angled //rt.eulerAngles = new Vector3(0f,0f,0f); Vector3 result = new Vector3(0f,0f,0f); if(rot == 0){ result = corners[1]; //result = rt.TransformPoint(new Vector3(rt.rect.xMin,rt.rect.yMin,rt.position.z)); }else if(rot == 90){ result = corners[0];//rt.TransformPoint(new Vector3(rt.rect.xMin,rt.rect.yMax,rt.position.z)); }else if(rot == 180){ result = corners[3];//rt.TransformPoint(new Vector3(rt.rect.xMax,rt.rect.yMax,rt.position.z)); }else if(rot == 270){ result = corners[2];//rt.TransformPoint(new Vector3(rt.rect.xMax,rt.rect.yMin,rt.position.z)); } //rt.eulerAngles = eulerSave; return result; }
static int GetWorldCorners(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 1) { UnityEngine.RectTransform obj = (UnityEngine.RectTransform)ToLua.CheckObject(L, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] arg0 = new UnityEngine.Vector3[4]; obj.GetWorldCorners(arg0); ToLua.Push(L, arg0); return(1); } else if (count == 2) { UnityEngine.RectTransform obj = (UnityEngine.RectTransform)ToLua.CheckObject(L, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] arg0 = ToLua.CheckStructArray <UnityEngine.Vector3>(L, 2); obj.GetWorldCorners(arg0); return(0); } else { return(LuaDLL.luaL_throw(L, "invalid arguments to method: UnityEngine.RectTransform.GetWorldCorners")); } } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
static public int GetWorldCorners(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.RectTransform self = (UnityEngine.RectTransform)checkSelf(l); UnityEngine.Vector3[] a1; checkArray(l, 2, out a1); self.GetWorldCorners(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
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); }
static public int GetWorldCorners(IntPtr l) { try { UnityEngine.RectTransform self = (UnityEngine.RectTransform)checkSelf(l); UnityEngine.Vector3[] a1; checkType(l, 2, out a1); self.GetWorldCorners(a1); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int GetWorldCorners(IntPtr l) { try { UnityEngine.RectTransform self = (UnityEngine.RectTransform)checkSelf(l); UnityEngine.Vector3[] a1; checkArray(l, 2, out a1); self.GetWorldCorners(a1); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
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); }
private Rect GetScreenRect(RectTransform rTransform) { Vector3[] corners = new Vector3[4]; Vector3[] screenCorners = new Vector3[2]; rTransform.GetWorldCorners(corners); screenCorners[0] = RectTransformUtility.WorldToScreenPoint(camera, corners[1]); screenCorners[1] = RectTransformUtility.WorldToScreenPoint(camera, corners[3]); screenCorners[0].y = Screen.height - screenCorners[0].y; screenCorners[1].y = Screen.height - screenCorners[1].y; return new Rect(screenCorners[0], screenCorners[1] - screenCorners[0]); }
static int GetWorldCorners(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); UnityEngine.RectTransform obj = (UnityEngine.RectTransform)ToLua.CheckObject(L, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] arg0 = ToLua.CheckObjectArray <UnityEngine.Vector3>(L, 2); obj.GetWorldCorners(arg0); return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
public void Init(Transform slotTransform, List<RectTransform> slotRects, Weapon weapon, Tooltip tooltip) { this.slotTransform = slotTransform; this.slotRects = slotRects; this.weapon = weapon; this.tooltip = tooltip; tileRectTransform = transform as RectTransform; canvasRectTransform = GetComponentInParent<Canvas>().transform as RectTransform; rectTransform = GetComponent<RectTransform>(); Vector3[] corners = new Vector3[4]; tileRectTransform.GetWorldCorners(corners); // RectTransform doesn't give a way to get width/height in world units, so must hack it. rectWidth = corners[2].x - corners[0].x; rectHeight = corners[2].y - corners[0].y; }
static int GetWorldCorners0(IntPtr L) { try { int count = LuaDLL.lua_gettop(L); if (count == 1) { UnityEngine.RectTransform obj = (UnityEngine.RectTransform)ToLua.CheckObject(L, 1, typeof(UnityEngine.RectTransform)); UnityEngine.Vector3[] arg0 = new UnityEngine.Vector3[4]; obj.GetWorldCorners(arg0); ToLua.Push(L, arg0); return(1); } return(0); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
private Rect GetScreenRect(RectTransform rectTransform, Canvas canvas) { Vector3[] corners = new Vector3[4]; Vector3[] screenCorners = new Vector3[2]; rectTransform.GetWorldCorners(corners); if (canvas.renderMode == RenderMode.ScreenSpaceCamera || canvas.renderMode == RenderMode.WorldSpace) { screenCorners[0] = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, corners[1]); screenCorners[1] = RectTransformUtility.WorldToScreenPoint(canvas.worldCamera, corners[3]); } else { screenCorners[0] = RectTransformUtility.WorldToScreenPoint(null, corners[1]); screenCorners[1] = RectTransformUtility.WorldToScreenPoint(null, corners[3]); } screenCorners[0].y = screenCorners[0].y - referenceRect.rect.height; screenCorners[1].y = screenCorners[1].y - referenceRect.rect.height; return new Rect(screenCorners[0], new Vector2(referenceRect.rect.width, referenceRect.rect.height)); }
//Snap the inbound slice position to one of the 8 positions along the edge of our bounding box. Vector3 SnapToBounds( RectTransform obj, Vector3 pos, out bool isCorner, out int chosenIndex ) { Vector3[] corners = new Vector3[4]; Vector3[] inners = new Vector3[4]; obj.GetWorldCorners( corners ); corners = Flatten( corners ); //Calculate inners based on corners inners[0] = Vector3.Lerp(corners[3], corners[0], 0.5f); inners[1] = Vector3.Lerp(corners[0], corners[1], 0.5f); inners[2] = Vector3.Lerp(corners[1], corners[2], 0.5f); inners[3] = Vector3.Lerp(corners[2], corners[3], 0.5f); //Debug.DrawRay( inners[3], Vector3.up, Color.cyan, 2f ); //Find what is closest to pos isCorner = false; chosenIndex = -1; int index = -1; float distance = Mathf.Infinity; //TODO - instead of distance, base off of closest to original angle Vector3 closestPos = Vector3.zero; foreach( Vector3 c in corners ) { index++; if( Vector3.Distance( c, pos ) < distance ) { distance = Vector3.Distance( c, pos ); closestPos = c; chosenIndex = index; isCorner = true; } } index = -1; foreach( Vector3 i in inners ) { index++; if( Vector3.Distance( i, pos ) < distance ) { distance = Vector3.Distance( i, pos ); closestPos = i; chosenIndex = index; isCorner = false; } } if( _doDebug ) Debug.DrawLine(pos, closestPos, Color.yellow, 2f); return closestPos; }
public static Vector2 GetWorldSize(this RectTransform trans) { trans.GetWorldCorners(_sCorners); return(new Vector2(_sCorners.Max(x => x.x) - _sCorners.Min(x => x.x), _sCorners.Max(x => x.y) - _sCorners.Min(x => x.y))); }
private static Vector3 GetRectReferenceCorner(RectTransform gui, bool worldSpace) { if (!worldSpace) { return gui.rect.min + gui.transform.localPosition; } Transform transform = gui.transform; gui.GetWorldCorners(RectTransformEditor.s_Corners); if (transform.parent) { return transform.parent.InverseTransformPoint(RectTransformEditor.s_Corners[0]); } return RectTransformEditor.s_Corners[0]; }
private void DrawSizeDistances(Transform userSpace, Rect rectInParentSpace, Transform parentSpace, RectTransform gui, RectTransform guiParent, float size, int axis, float alpha) { if (alpha <= 0f) { return; } Color color = RectTransformEditor.kAnchorColor; color.a *= alpha; GUI.color = color; if (userSpace == gui.transform) { gui.GetWorldCorners(RectTransformEditor.s_Corners); } else { gui.GetLocalCorners(RectTransformEditor.s_Corners); for (int i = 0; i < 4; i++) { RectTransformEditor.s_Corners[i] += gui.transform.localPosition; RectTransformEditor.s_Corners[i] = userSpace.TransformPoint(RectTransformEditor.s_Corners[i]); } } string text = gui.sizeDelta[axis].ToString(); GUIContent label = new GUIContent(text); Vector3 b = ((axis != 0) ? userSpace.right : userSpace.up) * size * 2f; this.DrawLabelBetweenPoints(RectTransformEditor.s_Corners[0] + b, RectTransformEditor.s_Corners[(axis != 0) ? 1 : 3] + b, label); }
/// <summary> /// Function used to determine if the position intersects with the RectTransform. /// </summary> /// <param name="rectTransform">A reference to the RectTranform of the text object.</param> /// <param name="position">Position to check for intersection.</param> /// <param name="camera">The scene camera which may be assigned to a Canvas using ScreenSpace Camera or WorldSpace render mode. Set to null is using ScreenSpace Overlay.</param> /// <returns></returns> public static bool IsIntersectingRectTransform(RectTransform rectTransform, Vector3 position, Camera camera) { // Convert position into Worldspace coordinates ScreenPointToWorldPointInRectangle(rectTransform, position, camera, out position); rectTransform.GetWorldCorners(m_rectWorldCorners); if (PointIntersectRectangle(position, m_rectWorldCorners[0], m_rectWorldCorners[1], m_rectWorldCorners[2], m_rectWorldCorners[3])) { return true; } return false; }
// Use this for initialization void Start() { m_RectTran = transform as RectTransform; m_ScrollRect = gameObject.GetComponent<ScrollRect>(); m_ScreensContent = m_ScrollRect.content; m_ScrollRect.movementType = ScrollRect.MovementType.Unrestricted; m_ScrollRect.inertia = false; m_Horizontal = m_ScrollRect.horizontal; DistributePages(); m_Page = m_ScreensContent.childCount; m_Positions = new List<Vector3>(); Vector3[] worldCorner = new Vector3[4]; m_RectTran.GetWorldCorners(worldCorner); Vector3 center = Vector3.Lerp(worldCorner[0], worldCorner[2], 0.5f); if (m_Page > 0) { for (int i = 0; i < m_Page; ++i) { RectTransform child = m_ScreensContent.transform.GetChild(i) as RectTransform; if(m_Horizontal) { float distance = child.position.x - center.x + ItemWidth / 2.0f; m_ScreensContent.position = m_ScreensContent.position - new Vector3(distance, 0, 0); m_Positions.Add(m_ScreensContent.localPosition); m_ScrollRect.horizontalNormalizedPosition = 0; } else { float distance = child.position.y - center.y + ItemHeight / 2.0f; m_ScreensContent.position = m_ScreensContent.position - new Vector3(0, distance, 0); m_Positions.Add(m_ScreensContent.localPosition); m_ScrollRect.verticalNormalizedPosition = 0; } } } m_LerpTarget = m_Positions[0]; m_Lerp = true; if (NextButton) UGUIEventListener.Get(NextButton).onClick = NextScreen; if (PrevButton) UGUIEventListener.Get(PrevButton).onClick = PreviousScreen; }
/// <summary> /// Detects whether a ray intersects a RectTransform and if it does also /// returns the world position of the intersection. /// </summary> /// <param name="rectTransform"></param> /// <param name="ray"></param> /// <param name="worldPos"></param> /// <returns></returns> static bool RayIntersectsRectTransform(RectTransform rectTransform, Ray ray, out Vector3 worldPos) { Vector3[] corners = new Vector3[4]; rectTransform.GetWorldCorners(corners); Plane plane = new Plane(corners[0], corners[1], corners[2]); float enter; if (!plane.Raycast(ray, out enter)) { worldPos = Vector3.zero; return false; } Vector3 intersection = ray.GetPoint(enter); Vector3 BottomEdge = corners[3] - corners[0]; Vector3 LeftEdge = corners[1] - corners[0]; float BottomDot = Vector3.Dot(intersection - corners[0], BottomEdge); float LeftDot = Vector3.Dot(intersection - corners[0], LeftEdge); if (BottomDot < BottomEdge.sqrMagnitude && // Can use sqrMag because BottomEdge is not normalized LeftDot < LeftEdge.sqrMagnitude && BottomDot >= 0 && LeftDot >= 0) { worldPos = corners[0] + LeftDot * LeftEdge / LeftEdge.sqrMagnitude + BottomDot * BottomEdge / BottomEdge.sqrMagnitude; return true; } else { worldPos = Vector3.zero; return false; } }