プロジェクトテンプレートの進め方
プロジェクトの作成
2DでProjectTemplateとする
Coreフォルダ作成
Scripts/Coreフォルダを作成、この中に作成する
SceneManager系のクラスを作成
Core/Scene
【ISceneData.cs】
csharp1namespace Core{ 2 public interface ISceneData { } 3}
【SceneManager.cs】
csharp1using UnityEngine; 2 3namespace Core{ 4 public class SceneManager 5 { 6 public static SceneManager Instance { get; } = new SceneManager(); 7 private SceneManager() { } 8 9 public ISceneData CurrentSceneData { get; private set; } 10 11 public void LoadScene(string sceneName, ISceneData sceneData = null) 12 { 13 CurrentSceneData = sceneData; 14 UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName); 15 } 16 } 17}
ResourceManager系のクラスを作成
Core/Resourceフォルダ内に作成(Resourcesにすると予約語)
【ResourceLoader.cs】
csharp1using System.Collections.Generic; 2using UnityEngine; 3 4namespace Core 5{ 6 public class ResourceLoader 7 { 8 private Dictionary<string, Object> resourceCache = new Dictionary<string, Object>(); 9 10 public T Load<T>(string path) where T : Object 11 { 12 if (resourceCache.ContainsKey(path)) 13 { 14 Object cached = resourceCache[path]; 15 if (cached is T typedCache) return typedCache; 16 17 Debug.LogWarning($"型の不一致: '{path}' は {cached.GetType().Name} としてキャッシュされていますが、{typeof(T).Name} として要求されました"); 18 return null; 19 } 20 21 T resource = Resources.Load<T>(path); 22 if (resource == null) 23 { 24 Debug.LogWarning($"リソースが見つかりません: Resources/{path}"); 25 return null; 26 } 27 resourceCache[path] = resource; 28 return resource; 29 } 30 31 public void ClearCache() 32 { 33 resourceCache.Clear(); 34 } 35 } 36}
【ResourceManager.cs】
csharp1using UnityEngine; 2 3namespace Core 4{ 5 public class ResourceManager 6 { 7 public static ResourceManager Instance { get; } = new ResourceManager(); 8 private ResourceManager() { } 9 10 private const string PrefabPath = "Prefabs/"; 11 private ResourceLoader resourceLoader = new ResourceLoader(); 12 13 14 public T Instantiate<T>(string path, Transform parent = null) where T : Object 15 { 16 T prefab = Load<T>(PrefabPath + path); 17 if (prefab == null) return null; 18 19 T instance = Object.Instantiate(prefab, parent); 20 return instance; 21 } 22 23 public T Load<T>(string path) where T : Object 24 { 25 return resourceLoader.Load<T>(path); 26 } 27 28 public void ClearCache() 29 { 30 resourceLoader.ClearCache(); 31 } 32 } 33}
【SceneManager.cs】の変更
csharp1public void LoadScene(string sceneName, ISceneData sceneData = null) 2{ 3 ResourceManager.Instance.ClearCache(); 4 CurrentSceneData = sceneData; 5 UnityEngine.SceneManagement.SceneManager.LoadScene(sceneName); 6 Resources.UnloadUnusedAssets(); 7}
パッケージ化
アセットを選択してパッケージ化
この後は課題