新しく作成した StagePathDataSO スクリプタブル・オブジェクトをゲーム内で利用できるように
DataBaseManager に変数を追加して管理を行います。
合わせて、StagePathDataSO スクリプタブル・オブジェクト内の情報を扱いやすくするため、
指定した情報を抽出して提供できるように、戻り値のあるゲッターメソッドを5つ作成しておきます。
各メソッド内では、StagePathDataSO スクリプタブル・オブジェクトに対しての抽出処理などがメソッド単位でまとめられています。
また、その中では System.Linq ライブラリを利用している部分があるため、using に宣言の追加を行っています。
利用している Select メソッドと ToArray メソッドについては、手順8にて、すでに学習済です。復習として、しっかりと理解を深めてください。
List 内部情報の参照の仕方や抽出の仕方の多くは、他の List でも応用が利く内容になっています。
そのため処理の内容をしっかりと理解してから学習を進めていくことを強くお勧めします。
List は作ったり、値を追加しているだけでは活用出来ません。List の内部情報に対しての抽出処理や検索処理といったアクセスの方法を覚えてはじめて役立ちます。
DataBaseManager.cs
<= クリックすると開きます
using UnityEngine;
using System.Linq; // ☆ <= 宣言を追加します。
public class DataBaseManager : MonoBehaviour {
public static DataBaseManager instance;
public WeaponDataSO weaponDataSO;
////* 新しい変数の宣言を1つ追加 *////
[SerializeField]
private StagePathDataSO stagePathDataSO;
////* ここまで *////
void Awake() {
if (instance == null) {
instance = this;
DontDestroyOnLoad(gameObject);
} else {
Destroy(gameObject);
}
}
/// <summary>
/// WeaponDataSO スクリプタブル・オブジェクトの中から、引数に指定された WeaponNo を持つ WeaponData の取得
/// </summary>
/// <param name="searchWeaponNo"></param>
/// <returns></returns>
public WeaponData GetItemData(int searchWeaponNo) {
return weaponDataSO.weaponDatasList.Find(x => x.weaponNo == searchWeaponNo);
}
////* 新しいメソッドを5つ追加 *////
/// <summary>
/// ステージパス番号から分岐先の RailPathData 情報を取得
/// </summary>
/// <param name="searchStageNo"></param>
/// <returns></returns>
public RailPathData GetRailPathDatasFromBranchNo(int nextStagePathDataNo, BranchDirectionType searchBranchDirectionType) {
return stagePathDataSO.stagePathDatasList[nextStagePathDataNo].branchDatasList.Find(x => x.branchDirectionType == searchBranchDirectionType).railPathData;
}
/// <summary>
/// ステージ内のルートの数の取得
/// </summary>
/// <returns></returns>
public int GetStagePathDetasListCount() {
return stagePathDataSO.stagePathDatasList.Count;
}
/// <summary>
/// 分岐の種類の取得
/// </summary>
/// <param name="nextStagePathDataNo"></param>
/// <param name="no"></param>
/// <returns></returns>
public BranchDirectionType GetBranchDirectionTypeFromRailPathData(int nextStagePathDataNo, int no) {
return stagePathDataSO.stagePathDatasList[nextStagePathDataNo].branchDatasList[no].branchDirectionType;
}
/// <summary>
/// 全分岐情報の取得
/// </summary>
/// <param name="nextStagePathDataNo"></param>
/// <returns></returns>
public BranchDirectionType[] GetBranchDirectionTypes(int nextStagePathDataNo) {
return stagePathDataSO.stagePathDatasList[nextStagePathDataNo].branchDatasList.Select(x => x.branchDirectionType).ToArray();
}
/// <summary>
/// ブランチの管理している分岐数の取得
/// </summary>
/// <param name="branchNo"></param>
/// <returns></returns>
public int GetBranchDatasListCount(int branchNo) {
return stagePathDataSO.stagePathDatasList[branchNo].branchDatasList.Count;
}
////* ここまで *////
}
スクリプトを修正したらセーブします。