コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【C#】Rect の代入を簡略化する Deconstruction

ソースコード

using UnityEngine;

public static class RectExt
{
    public static void Deconstruct
    (
        this Rect    self,
        out  Vector2 position,
        out  Vector2 size
    )
    {
        position = self.position;
        size     = self.size;
    }

    public static void Deconstruct
    (
        this Rect  self,
        out  float x,
        out  float y,
        out  float width,
        out  float height
    )
    {
        x      = self.x;
        y      = self.y;
        width  = self.width;
        height = self.height;
    }
}

使用例

通常

var rect = new Rect( 1, 2, 3, 4 );

Deconstruction

var ( position, size ) = new Rect( 1, 2, 3, 4 );
var ( x, y, width, height ) = new Rect( 1, 2, 3, 4 );