コガネブログ

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

【Unity】DontDestroyOnLoad関数の拡張メソッド

ソースコード

using UnityEngine;

public static class GameObjectExtensions
{
    /// <summary>
    /// 新しいシーンを読み込む時に自動で破棄されないようにします
    /// </summary>
    public static void DontDestroyOnLoad( this GameObject self )
    {
        GameObject.DontDestroyOnLoad( self );
    }
}

使い方

// GameObject 型の static 関数で呼び出し
GameObject.DontDestroyOnLoad( go );

// 拡張メソッドで呼び出し
go.DontDestroyOnLoad();

関連記事