0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Unity】RaycastPaddingの当たり判定をGizmoで表示するEditor拡張

Last updated at Posted at 2022-07-31

はじめに

unity 2020からUIの当たり判定を変更できるRaycastPaddingが追加されました。

非常に便利だと思うのですが視覚的にどのように変更されたのか分からなかったのでGizmoで表示してみました。

概要

image.png

当たり判定とGizmoが正しいかのために赤のImageを追加して、実際にタッチがどのようになっているか確認してみます。

Editorで確認

ある程度はあっているみたいなのですが1ピクセルの正確に表示されているかは微妙なところです、一応あっていると思います。

Unity_tN62f18efZ.gif

コード

コード全文.cs
.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEditor;

public static class RaycastPaddingDrawer
{
    [DrawGizmo(GizmoType.Selected, typeof(Image))]
    private static void DrawPadding(Image image, GizmoType gizmoType)
    {
        var rectTransform = image.rectTransform;
        var rect = rectTransform.rect;

        var max = rect.max;
        max.x -= image.raycastPadding.z;
        max.y -= image.raycastPadding.w;
        var min = rect.min;
        min.x += image.raycastPadding.x;
        min.y += image.raycastPadding.y;

        var p0 = rectTransform.TransformPoint(max);
        var p2 = rectTransform.TransformPoint(min);
        var p1 = new Vector3(p2.x, p0.y, p0.z);
        var p3 = new Vector3(p0.x, p2.y, p0.z);

        Gizmos.color = Color.green;
        Gizmos.DrawLine(p0, p1);
        Gizmos.DrawLine(p1, p2);
        Gizmos.DrawLine(p2, p3);
        Gizmos.DrawLine(p3, p0);
    }
}

おわりに

便利な機能ですが視覚化して確認できなければ正直使いにくく拡張用のImageを追加するほうが確実な感じがしました、Unityが公式に対応してほしいですね…

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?