[uGUI]UIパーツにガイド線を表示する


デザイナーさんに、UnityってPhotoshopでいうガイドみたいなの無いの?と言われたので作ってみました。

    void OnDrawGizmos ()
    {
        RectTransform rectTransform = transform as RectTransform;

        float xOffset = transform.position.x - transform.localPosition.x;
        float yOffset = transform.position.y - transform.localPosition.y;

       //左端x
        float x1 = (transform.localPosition.x - (rectTransform.sizeDelta.x * rectTransform.pivot.x) * transform.lossyScale.x) + xOffset;
       //右端x        
        float x2 = (transform.localPosition.x + (rectTransform.sizeDelta.x * (1f - rectTransform.pivot.x) * transform.lossyScale.x))+ xOffset;
       //上端y    
       float y1 = (transform.localPosition.y - (rectTransform.sizeDelta.y  * rectTransform.pivot.y) * transform.lossyScale.y) + yOffset;
    //下端y
        float y2 = (transform.localPosition.y + (rectTransform.sizeDelta.y  * (1f - rectTransform.pivot.y) * transform.lossyScale.y)) + yOffset;


        float max = 100000f;
        float min = -100000f;

        Gizmos.color = new Color32(255,0,0,100);
        Gizmos.DrawLine(new Vector3(x1,min,0f), new Vector3(x1,max,0f));
        Gizmos.DrawLine(new Vector3(x2,min,0f), new Vector3(x2,max,0f));
        Gizmos.DrawLine(new Vector3(min,y1,0f), new Vector3(max,y1,0f));
        Gizmos.DrawLine(new Vector3(min,y2,0f), new Vector3(max,y2,0f));
    }

やっていること
RectTransformの4点の座標を計算し、world座標に直してGismoで描画

結果はこんな感じ