コガネブログ

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

【Unity】Camera.ScreenToWorldPoint で毎回 z に 0 を入れなくて済む拡張メソッド

ソースコード

using UnityEngine;

namespace Kogane
{
    public static class CameraExtensionMethods
    {
        public static Vector2 ScreenToWorldPoint2D
        (
            this Camera self,
            Vector3     position
        )
        {
            return self.ScreenToWorldPoint( position );
        }

        public static Vector2 ScreenToWorldPoint2D
        (
            this Camera                  self,
            Vector3                      position,
            Camera.MonoOrStereoscopicEye eye
        )
        {
            return self.ScreenToWorldPoint( position, eye );
        }
    }
}

使用例

Before

var worldPoint = Camera.main.ScreenToWorldPoint( screenPoint );
worldPoint.z = 0;

After

var worldPoint = Camera.main.ScreenToWorldPoint2D( screenPoint );