【Unity】GUI.DrawTexture()でいろんな図形を描画する

UnityのIMGUIのGUI.DrawTexture()で色々描いてみます。

Unity2020.1

はじめに

この記事ではGUI.DrawTexture()でいろんな図形を描画します。
処理は以下のものをベースとし、次節以降ではOnGUI以外は省略します。

using UnityEngine;

public class Example : MonoBehaviour
{
    private Texture _texture;

    private void Start()
    {
        var texture = new Texture2D(1, 1);
        texture.SetPixel(0, 0, Color.white);
        texture.Apply();
        _texture = texture;
    }
    
    private void OnGUI()
    {
        GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red);
    }
}

矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red);

f:id:halya_11:20210220122452p:plain
矩形

角丸の矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, 0, 12);

f:id:halya_11:20210220122630p:plain
角丸矩形

中抜きの矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.white, 3, 0);

f:id:halya_11:20210220122739p:plain
中抜きの矩形

中抜きの角丸矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.white, 3, 12);

f:id:halya_11:20210220122842p:plain
中抜き角丸矩形

ボーダー付きの矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, 0, 0);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, 3, 0);

f:id:halya_11:20210220123158p:plain
ボーダー付きの矩形

ボーダー付きの角丸矩形を描画

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, 0, 12);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, 3, 12);

f:id:halya_11:20210220123100p:plain
ボーダー付き角丸矩形

ボーダーの太さを辺によって変える

var rect = new Rect(12, 12, 300, 100);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, 0, 0);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, new Vector4(0, 0, 1, 3), 0);

f:id:halya_11:20210220123445p:plain
ボーダーの太さを変える

丸みを角によって変える

var rect = new Rect(12, 12, 300, 100);
var radius = new Vector4(12, 12, 0, 0);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, Vector4.zero, radius);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, new Vector4(0, 0, 0, 3), radius);

f:id:halya_11:20210220123619p:plain
丸みを角によって変える

丸ボタン

var rect = new Rect(12, 12, 300, 100);
var radius = Mathf.Min(rect.width, rect.height) / 2;
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, Vector4.zero, radius);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, new Vector4(3, 3, 3, 3), radius);

f:id:halya_11:20210220123813p:plain
丸ボタン

var rect = new Rect(12, 12, 200, 200);
var radius = rect.width / 2;
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red * 0.5f, Vector4.zero, radius);
GUI.DrawTexture(rect, _texture, ScaleMode.StretchToFill, true, 0, Color.red, new Vector4(3, 3, 3, 3), radius);

f:id:halya_11:20210220124017p:plain

参考

docs.unity3d.com