本文整理汇总了C#中UnityEngine.RectTransform.GetWorldCorners方法的典型用法代码示例。如果您正苦于以下问题:C# RectTransform.GetWorldCorners方法的具体用法?C# RectTransform.GetWorldCorners怎么用?C# RectTransform.GetWorldCorners使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.RectTransform
的用法示例。
在下文中一共展示了RectTransform.GetWorldCorners方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetScreenRectFromRectTransform
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;
}
示例2: WorldTopLeftCorner
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;
}
示例3: GetCanvasRect
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);
}
示例4: GetCanvasRect
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);
}
示例5: GetScreenRect
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]);
}
示例6: Init
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;
}
示例7: GetScreenRect
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));
}
示例8: IsIntersectingRectTransform
/// <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;
}
示例9: SnapToBounds
//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;
}
示例10: RayIntersectsRectTransform
/// <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;
}
}
示例11: Start
// 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;
}
示例12: DrawSizeDistances
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);
}
示例13: GetRectReferenceCorner
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];
}