以下の内容はhttps://baba-s.hatenablog.com/entry/2024/03/08/153320より取得しました。


【Unity】ローカル座標の距離が一番近いコンポーネントを検索して返す拡張メソッド

ソースコード

/// <summary>
/// ローカル座標の距離が一番近いコンポーネントを検索して返します
/// </summary>
public static T2 FindClosestByLocalPosition<T1, T2>
(
    this IReadOnlyList<T2> self,
    T1                     component,
    float                  maxDistance
)
    where T1 : Component
    where T2 : Component
{
    T2  closestComponent = null;
    var closestDistance  = Mathf.Infinity;

    foreach ( var x in self )
    {
        var distance = Vector2.Distance
        (
            component.transform.localPosition,
            x.transform.localPosition
        );

        if ( distance < closestDistance && distance < maxDistance )
        {
            closestDistance  = distance;
            closestComponent = x;
        }
    }

    return closestComponent;
}



以上の内容はhttps://baba-s.hatenablog.com/entry/2024/03/08/153320より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14