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?

【Unity】簡潔に書けるシングルトンな ScriptableObject の実装方法の紹介

Last updated at Posted at 2025-01-26

概要

ScriptableObject は設定やデータ管理に利用できるため、シングルトンで取り扱いたい場面もあるかと思います。
そこで今回は、私がよく使うシングルトン?な ScriptableObject の実装を紹介します。

コード

SingletonScriptableObject.cs
using UnityEngine;

[CreateAssetMenu(fileName = nameof(SingletonScriptableObject), menuName = "ScriptableObjects/Create " + nameof(SingletonScriptableObject))]
public class SingletonScriptableObject : ScriptableObject
{
    private static SingletonScriptableObject instance = null;
    public static SingletonScriptableObject Instance => instance ??= Resources.Load<SingletonScriptableObject>(nameof(SingletonScriptableObject));
}

補足

ScriptableObject は Resources 直下に生成してください。
nameof() で名前の変更に対応しやすくしています。

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?