Unity 使用this关键字进行函数拓展 - RectTransform
【摘要】
Example:
private void Start(){ (transform as RectTransform) .AnchoredPosition(Vector2.zero) .AnchoredPosition(0f, 0f) .AnchoredPositionX(0f) .Anc...
Example:
-
private void Start()
-
{
-
(transform as RectTransform)
-
.AnchoredPosition(Vector2.zero)
-
.AnchoredPosition(0f, 0f)
-
.AnchoredPositionX(0f)
-
.AnchoredPositionY(0f)
-
.OffsetMax(Vector2.zero)
-
.OffsetMax(0f, 0f)
-
.OffsetMaxX(0f)
-
.OffsetMaxY(0f)
-
.OffsetMin(Vector2.zero)
-
.OffsetMin(0f, 0f)
-
.OffsetMinX(0f)
-
.OffsetMinY(0f)
-
.AnchoredPosition3D(Vector2.zero)
-
.AnchoredPosition3D(0f, 0f)
-
.AnchoredPosition3DX(0f)
-
.AnchoredPosition3DY(0f)
-
.AnchorMin(Vector2.zero)
-
.AnchorMin(0f, 0f)
-
.AnchorMinX(0f)
-
.AnchorMinY(0f)
-
.AnchorMax(Vector2.zero)
-
.AnchorMax(0f, 0f)
-
.AnchorMaxX(0f)
-
.AnchorMaxY(0f)
-
.Pivot(Vector2.zero)
-
.Pivot(0f, 0f)
-
.PivotX(0f)
-
.PivotY(0f)
-
.SizeDelta(Vector2.zero)
-
.SizeDelta(0f, 0f)
-
.SizeDeltaX(0f)
-
.SizeDeltaY(0f)
-
.SetSizeWidth(0f)
-
.SetSizeHeight(0f);
-
}
Extension:
-
/// <summary>
-
/// The extension of rect transform.
-
/// </summary>
-
public static class RectTransformExtension
-
{
-
#region AnchoredPosition
-
/// <summary>
-
/// Set the anchored position.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="anchoredPosition"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition(this RectTransform self, Vector3 anchoredPosition)
-
{
-
self.anchoredPosition = anchoredPosition;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition(this RectTransform self, float x, float y)
-
{
-
Vector2 anchoredPosition = self.anchoredPosition;
-
anchoredPosition.x = x;
-
anchoredPosition.y = y;
-
self.anchoredPosition = anchoredPosition;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPositionX(this RectTransform self, float x)
-
{
-
Vector2 anchoredPosition = self.anchoredPosition;
-
anchoredPosition.x = x;
-
self.anchoredPosition = anchoredPosition;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPositionY(this RectTransform self, float y)
-
{
-
Vector2 anchoredPosition = self.anchoredPosition;
-
anchoredPosition.y = y;
-
self.anchoredPosition = anchoredPosition;
-
return self;
-
}
-
#endregion
-
-
#region OffsetMax
-
/// <summary>
-
/// Set the offset max.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="offsetMax"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMax(this RectTransform self, Vector2 offsetMax)
-
{
-
self.offsetMax = offsetMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset max.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMax(this RectTransform self, float x, float y)
-
{
-
Vector2 offsetMax = self.offsetMax;
-
offsetMax.x = x;
-
offsetMax.y = y;
-
self.offsetMax = offsetMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset max x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMaxX(this RectTransform self, float x)
-
{
-
Vector2 offsetMax = self.offsetMax;
-
offsetMax.x = x;
-
self.offsetMax = offsetMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset max y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMaxY(this RectTransform self, float y)
-
{
-
Vector2 offsetMax = self.offsetMax;
-
offsetMax.y = y;
-
self.offsetMax = offsetMax;
-
return self;
-
}
-
#endregion
-
-
#region OffsetMin
-
/// <summary>
-
/// Set the offset min.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="offsetMin"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMin(this RectTransform self, Vector2 offsetMin)
-
{
-
self.offsetMin = offsetMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset min.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMin(this RectTransform self, float x, float y)
-
{
-
Vector2 offsetMin = self.offsetMin;
-
offsetMin.x = x;
-
offsetMin.y = y;
-
self.offsetMin = offsetMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset min x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMinX(this RectTransform self, float x)
-
{
-
Vector2 offsetMin = self.offsetMin;
-
offsetMin.x = x;
-
self.offsetMin = offsetMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the offset min y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform OffsetMinY(this RectTransform self, float y)
-
{
-
Vector2 offsetMin = self.offsetMin;
-
offsetMin.y = y;
-
self.offsetMin = offsetMin;
-
return self;
-
}
-
#endregion
-
-
#region AnchoredPosition3D
-
/// <summary>
-
/// Set the anchored position 3d.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="anchoredPosition3D"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition3D(this RectTransform self, Vector2 anchoredPosition3D)
-
{
-
self.anchoredPosition3D = anchoredPosition3D;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position 3d.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition3D(this RectTransform self, float x, float y)
-
{
-
Vector2 anchoredPosition3D = self.anchoredPosition3D;
-
anchoredPosition3D.x = x;
-
anchoredPosition3D.y = y;
-
self.anchoredPosition3D = anchoredPosition3D;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position 3d x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition3DX(this RectTransform self, float x)
-
{
-
Vector2 anchoredPosition3D = self.anchoredPosition3D;
-
anchoredPosition3D.x = x;
-
self.anchoredPosition3D = anchoredPosition3D;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchored position 3d y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchoredPosition3DY(this RectTransform self, float y)
-
{
-
Vector2 anchoredPosition3D = self.anchoredPosition3D;
-
anchoredPosition3D.y = y;
-
self.anchoredPosition3D = anchoredPosition3D;
-
return self;
-
}
-
#endregion
-
-
#region AnchorMin
-
/// <summary>
-
/// Set the anchor min.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="anchorMin"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMin(this RectTransform self, Vector2 anchorMin)
-
{
-
self.anchorMin = anchorMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor min.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMin(this RectTransform self, float x, float y)
-
{
-
Vector2 anchorMin = self.anchorMin;
-
anchorMin.x = x;
-
anchorMin.y = y;
-
self.anchorMin = anchorMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor min x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMinX(this RectTransform self, float x)
-
{
-
Vector2 anchorMin = self.anchorMin;
-
anchorMin.x = x;
-
self.anchorMin = anchorMin;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor min y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMinY(this RectTransform self, float y)
-
{
-
Vector2 anchorMin = self.anchorMin;
-
anchorMin.y = y;
-
self.anchorMin = anchorMin;
-
return self;
-
}
-
#endregion
-
-
#region AnchorMax
-
/// <summary>
-
/// Set the anchor max.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="anchorMax"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMax(this RectTransform self, Vector2 anchorMax)
-
{
-
self.anchorMax = anchorMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor max.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMax(this RectTransform self, float x, float y)
-
{
-
Vector2 anchorMax = self.anchorMax;
-
anchorMax.x = x;
-
anchorMax.y = y;
-
self.anchorMax = anchorMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor max x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMaxX(this RectTransform self, float x)
-
{
-
Vector2 anchorMax = self.anchorMax;
-
anchorMax.x = x;
-
self.anchorMax = anchorMax;
-
return self;
-
}
-
/// <summary>
-
/// Set the anchor max y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform AnchorMaxY(this RectTransform self, float y)
-
{
-
Vector2 anchorMax = self.anchorMax;
-
anchorMax.y = y;
-
self.anchorMax = anchorMax;
-
return self;
-
}
-
#endregion
-
-
#region Pivot
-
/// <summary>
-
/// Set the pivot.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="pivot"></param>
-
/// <returns></returns>
-
public static RectTransform Pivot(this RectTransform self, Vector2 pivot)
-
{
-
self.pivot = pivot;
-
return self;
-
}
-
/// <summary>
-
/// Set the pivot.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform Pivot(this RectTransform self, float x, float y)
-
{
-
Vector2 pivot = self.pivot;
-
pivot.x = x;
-
pivot.y = y;
-
self.pivot = pivot;
-
return self;
-
}
-
/// <summary>
-
/// Set the pivot x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform PivotX(this RectTransform self, float x)
-
{
-
Vector2 pivot = self.pivot;
-
pivot.x = x;
-
self.pivot = pivot;
-
return self;
-
}
-
/// <summary>
-
/// Set the pivot y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform PivotY(this RectTransform self, float y)
-
{
-
Vector2 pivot = self.pivot;
-
pivot.y = y;
-
self.pivot = pivot;
-
return self;
-
}
-
#endregion
-
-
#region SizeDelta
-
/// <summary>
-
/// Set the size delta.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="sizeDelta"></param>
-
/// <returns></returns>
-
public static RectTransform SizeDelta(this RectTransform self, Vector2 sizeDelta)
-
{
-
self.sizeDelta = sizeDelta;
-
return self;
-
}
-
/// <summary>
-
/// Set the size delta.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform SizeDelta(this RectTransform self, float x, float y)
-
{
-
Vector2 sizeDelta = self.sizeDelta;
-
sizeDelta.x = x;
-
sizeDelta.y = y;
-
self.sizeDelta = sizeDelta;
-
return self;
-
}
-
/// <summary>
-
/// Set the size delta x value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="x"></param>
-
/// <returns></returns>
-
public static RectTransform SizeDeltaX(this RectTransform self, float x)
-
{
-
Vector2 sizeDelta = self.sizeDelta;
-
sizeDelta.x = x;
-
self.sizeDelta = sizeDelta;
-
return self;
-
}
-
/// <summary>
-
/// Set the size delta y value.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="y"></param>
-
/// <returns></returns>
-
public static RectTransform SizeDeltaY(this RectTransform self, float y)
-
{
-
Vector2 sizeDelta = self.sizeDelta;
-
sizeDelta.y = y;
-
self.sizeDelta = sizeDelta;
-
return self;
-
}
-
#endregion
-
-
#region Size
-
/// <summary>
-
/// Set width with current anchors.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="width"></param>
-
/// <returns></returns>
-
public static RectTransform SetSizeWidth(this RectTransform self, float width)
-
{
-
self.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
-
return self;
-
}
-
/// <summary>
-
/// Set height with current anchors.
-
/// </summary>
-
/// <param name="self"></param>
-
/// <param name="height"></param>
-
/// <returns></returns>
-
public static RectTransform SetSizeHeight(this RectTransform self, float height)
-
{
-
self.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
-
return self;
-
}
-
-
#endregion
-
}
文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。
原文链接:coderz.blog.csdn.net/article/details/109021350
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)