UGUI RectTransform 組件簡單介紹


1.RectTransform 組件介紹
1.組件基礎介紹
Transform 組件是所有的游戲物體必備的一個組件,且不可刪除,不可隱藏。
就算是一個空物體,也是具備 Transform 組件的。
Unity 官方在推出 UGUI 系統后,針對 UI 游戲物體,創建了一個新的基礎組件:
RectTransform,這個組件是基於 Transform 組件的。[簡單演示]

namespace UnityEngine
{
    //
    // 摘要:
    //     Position, size, anchor and pivot information for a rectangle.
    [NativeClass("UI::RectTransform")]
     //RectTransform 繼承了Transform
public sealed class RectTransform : Transform { public RectTransform(); // // 摘要: // The offset of the upper right corner of the rectangle relative to the upper right // anchor. public Vector2 offsetMax { get; set; } // // 摘要: // The offset of the lower left corner of the rectangle relative to the lower left // anchor. public Vector2 offsetMin { get; set; } // // 摘要: // The normalized position in this RectTransform that it rotates around. public Vector2 pivot { get; set; } // // 摘要: // The size of this RectTransform relative to the distances between the anchors. public Vector2 sizeDelta { get; set; } // // 摘要: // The position of the pivot of this RectTransform relative to the anchor reference // point. public Vector2 anchoredPosition { get; set; } // // 摘要: // The normalized position in the parent RectTransform that the upper right corner // is anchored to. public Vector2 anchorMax { get; set; } // // 摘要: // The 3D position of the pivot of this RectTransform relative to the anchor reference // point. public Vector3 anchoredPosition3D { get; set; } // // 摘要: // The calculated rectangle in the local space of the Transform. public Rect rect { get; } // // 摘要: // The normalized position in the parent RectTransform that the lower left corner // is anchored to. public Vector2 anchorMin { get; set; } public static event ReapplyDrivenProperties reapplyDrivenProperties; // // 摘要: // Get the corners of the calculated rectangle in the local space of its Transform. // // 參數: // fourCornersArray: // The array that corners are filled into. public void GetLocalCorners(Vector3[] fourCornersArray); // // 摘要: // Get the corners of the calculated rectangle in world space. // // 參數: // fourCornersArray: // The ray that corners are filled into. public void GetWorldCorners(Vector3[] fourCornersArray); public void SetInsetAndSizeFromParentEdge(Edge edge, float inset, float size); public void SetSizeWithCurrentAnchors(Axis axis, float size); // // 摘要: // Enum used to specify one edge of a rectangle. public enum Edge { // // 摘要: // The left edge. Left = 0, // // 摘要: // The right edge. Right = 1, // // 摘要: // The top edge. Top = 2, // // 摘要: // The bottom edge. Bottom = 3 } // // 摘要: // An axis that can be horizontal or vertical. public enum Axis { // // 摘要: // Horizontal. Horizontal = 0, // // 摘要: // Vertical. Vertical = 1 } // // 摘要: // Delegate used for the reapplyDrivenProperties event. // // 參數: // driven: public delegate void ReapplyDrivenProperties(RectTransform driven); } }

2.組件組成部分
RectTransform 組件由兩部分組成:
①組件基礎部分:類似於 Transform,控制游戲物體基本屬性。
②Anchors 錨點部分:UGUI 特有屬性,用於實現 UI 游戲物體錨點定位。

 

2.RectTransform 基本屬性
1.位置屬性
  Pos X,Pos Y,Pos Z 三個屬性等同於 Transform 組件的 Position;
  都是用於表示游戲物體在三維空間內的位置信息的。
2.旋轉屬性
  Rotation 屬性等同於 Transform 組件的 Rotation;用於表示物體的旋轉。
3.縮放屬性
  Scale 屬性等同於 Transform 組件的 Scale;用於表示物體的縮放比例。
4.寬高屬性
  Width,Height 屬性用於表示 UI 游戲物體的寬和高。
5.中心點屬性
  Pivot 屬性用於表示 UI 游戲物體的中心點。中心點在 Scene 界面表現為一個
  “空心的藍色圓環”。


UI 游戲物體的中心點的作用和 3D 模型的中心點的作用是一樣的,當我們改變
一個游戲物體的位置的時候,都是相對於該物體的中心點進行移動的。


Pivot 屬性的 X,Y 兩個值的取值范圍0-1[見圖]。

x=0時,左邊,X=1時,右邊

y=0時,下邊,Y=1時,上邊


當我們改變了 UI 游戲物體的 Pivot 屬性后,UI 游戲物體的 Pos 屬性也會跟隨
發生改變。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM