コガネブログ

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

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

using UnityEditor;

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