コガネブログ

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

【Unity】指定されたオブジェクトがプレハブの場合 true を返す拡張メソッド

using UnityEditor;

/// <summary>
/// UnityEngine.Object 型の拡張メソッドを管理するクラス
/// </summary>
public static class UnityEngineObjectExt
{
    /// <summary>
    /// 指定されたオブジェクトがプレハブの場合 true を返します
    /// </summary>
    public static bool IsPrefab( this UnityEngine.Object self )
    {
        var type = PrefabUtility.GetPrefabType( self );
        
        return 
            type == PrefabType.Prefab               || 
            type == PrefabType.ModelPrefab          || 
            type == PrefabType.PrefabInstance       || 
            type == PrefabType.ModelPrefabInstance  || 
            type == PrefabType.PrefabInstance
        ;
    }
}
if ( gameObject.IsPrefab() )
{
    ...
}