[Unity]NGUI插件的坐標(biāo)系統(tǒng)和Anchor錨點(diǎn)

[Unity]技術(shù)學(xué)習(xí)路線圖(長期更新)

最近開發(fā)項(xiàng)目使用自定義NGUI組件控件的功能比較多,對(duì)NGUI系統(tǒng) 內(nèi)的坐標(biāo)系統(tǒng)和錨點(diǎn)做個(gè)總結(jié)

NGUI坐標(biāo)系統(tǒng)及錨點(diǎn)

坐標(biāo)系統(tǒng)圖解

坐標(biāo).jpg
坐標(biāo).jpg

操作圖
操作圖
  • NGUI插件默認(rèn)以父節(jié)點(diǎn)的中心點(diǎn)(0,0)為原點(diǎn)
  • NGUI插件默認(rèn)以父節(jié)點(diǎn)的右側(cè)為x軸正向
  • NGUI插件默認(rèn)以父節(jié)點(diǎn)的上側(cè)為y軸正向

框架類結(jié)構(gòu)

NGUI框架內(nèi)支持SetRect函數(shù)的類結(jié)構(gòu)
NGUI框架內(nèi)支持SetRect函數(shù)的類結(jié)構(gòu)

在腳本代碼中修改游戲?qū)ο蟮木匦挝恢?code>Rect

  • NGUIRects.cs修改UIRect腳本的位置Position

    
    using System;
    using UnityEngine;
    
    public sealed class NGUIRects
    {
        private NGUIRects ()
        {
        }
    
        //重置UIRect組件對(duì)象的位置
        public static void CenterByPosition(UIRect rect,Vector2 center)
        {
            if (rect == null) {
                return;
            }
            
            Vector3 v = new Vector3 (center.x, center.y);
            rect.transform.localPosition = v;
        }
    
        //根據(jù)Vector2增量改變UIRect組件對(duì)象的位置
        public static void CenterByIncrement(UIRect rect,Vector2 vector)
        {
            if (rect == null) {
                return;
            }
            if (vector.Equals(Vector2.zero)) {
                return;
            }
    
            Vector3 v = new Vector3 (vector.x, vector.y);
            rect.transform.localPosition += v;
        }
    }
    
    
  • NGUIRects.cs修改UIRect腳本的位置Size

    public static void SizeBySize(UIRect rect,Vector2 size)
    {
        if (rect == null) {
            return;
        }
    
        if (size.Equals(Vector2.zero)) {
            return;
        }
    
        Vector3 center = rect.transform.localPosition;
        
        //首先修改物體的大小
        rect.SetRect (0f, 0f, size.x, size.y);
        
        //防止中心位置不發(fā)生改變
        rect.transform.localPosition = center;
    }
    
    

錨點(diǎn)圖解

錨點(diǎn)操作圖
錨點(diǎn)操作圖

框架類結(jié)構(gòu)

UIAnchor
UIAnchor

AnchorPoint
AnchorPoint

在腳本代碼中修改游戲?qū)ο蟮木匦挝恢?code>Anchor

  • 使用默認(rèn)的leftAnchor、rightAnchorbottomAnchor、topAnchor(即4個(gè)方向同時(shí)相對(duì)于同一個(gè)Transform)

    public UISprite test_sprite;
    ...
    if (test_sprite.isFullyAnchored) 
    {
        test_sprite.SetAnchor (anchor_sprite.gameObject, 1, 2, 3, 4);
    }
    
實(shí)現(xiàn)在Inspector中的效果
實(shí)現(xiàn)在Inspector中的效果
  • 使用自定義的leftAnchorrightAnchorbottomAnchor、topAnchor(即4個(gè)方向可以相對(duì)于自定義的Transform)

    public UISprite test_sprite;
    public Transform leftAnchorTarget;
    public Transform rightAnchorTarget;
    public Transform topAnchorTarget;
    public Transform bottomAnchorTarget;
    ...
    
    test_sprite.leftAnchor.Set (leftAnchorTarget, 0f, 20f);
    test_sprite.rightAnchor.Set (rightAnchorTarget, 0.5f, -30f);
    test_sprite.topAnchor.Set (topAnchorTarget, 0.5f, 20f);
    test_sprite.bottomAnchor.Set (bottomAnchorTarget, 1f, 20f);
    
    • 解釋一下AnchorPoint內(nèi)的Set的三個(gè)參數(shù)的作用

      • target:Transform

        • 指定某個(gè)方向的相對(duì)于的Transform組件
      • relative:float

        • 如果是水平方向的約束錨點(diǎn):leftAnchor、rightAnchor

          • Target游戲?qū)ο?code>Transform組件的左邊的relative的值是0;

          • Target游戲?qū)ο?code>Transform組件的中心的relative的值是0.5;

          • Target游戲?qū)ο?code>Transform組件的右邊的relative的值是1;

          • 其他的relative值是自定義位置;

        • 如果是垂直方向的約束錨點(diǎn):topAnchor、bottomAnchor

          • Target游戲?qū)ο?code>Transform組件的下邊的relative的值是0;

          • Target游戲?qū)ο?code>Transform組件的中心的relative的值是0;

          • Target游戲?qū)ο?code>Transform組件的上邊的relative的值是0;

          • 其他的relative值是自定義位置;

      • absolute:float

        • 偏移量的值;

如若有無,請聯(lián)系我

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • This article is a record of my journey to learn Game Deve...
    蔡子聰閱讀 4,098評(píng)論 0 9
  • 這個(gè)是我剛剛整理出的Unity面試題,為了幫助大家面試,同時(shí)幫助大家更好地復(fù)習(xí)Unity知識(shí)點(diǎn),如果大家發(fā)現(xiàn)有什么...
    編程小火雞閱讀 3,994評(píng)論 2 34
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評(píng)論 19 139
  • 前言 項(xiàng)目中有功能需要在代碼中動(dòng)態(tài)創(chuàng)建UGUI對(duì)象,但是在網(wǎng)上搜索了很久都沒有找到類似的教程,最后終于在官方文檔中...
    Zui閱讀 31,315評(píng)論 11 63
  • 使用Unity完成簡單的背包功能 用UGUI布局成一下這個(gè)樣子 設(shè)置BackGround的tag為Bag Lft,...
    Unity學(xué)習(xí)的路上閱讀 6,211評(píng)論 1 4

友情鏈接更多精彩內(nèi)容