自作したクラスを配列で扱うために、
MasterData.cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class MasterData : MonoBehaviour 6{ 7 public static AllyMemberStatus[] AllyMemberStatusArray; //冒険者パーティー。一覧。 8 public static AllyMemberStatus[] AllyMemberSelectedArray;//パーティ選出メンバー管理用。 9 10 11 12 // Start is called before the first frame update 13 void Start() 14 { 15 DontDestroyOnLoad(this); 16 17 bool InitialGame = true; 18 19 var AllyMemberStatusList = new List<AllyMemberStatus>(); 20 if (InitialGame) 21 { 22 AllyStatus a1 = Resources.Load<AllyStatus>("AllyStatus/いち/1"); 23 AllyMemberStatus aa1 = new AllyMemberStatus(); 24 aa1 = a1.GetAllyMemberStatus(); 25 AllyMemberStatusArray = new AllyMemberStatus[1] {aa1}; 26 AllyMemberSelectedArray = new AllyMemberStatus[1] {aa1}; 27 Debug.Log(AllyMemberStatusArray[0].GetCharactorName()); 28 AllyStatus a2 = Resources.Load<AllyStatus>("AllyStatus/に/1"); 29 AllyMemberStatus aa2 = new AllyMemberStatus(); 30 aa2 = a2.GetAllyMemberStatus(); 31 32 System.Array.Resize(ref AllyMemberStatusArray, AllyMemberStatusArray.Length + 1); 33 AllyMemberStatusArray[AllyMemberStatusArray.Length - 1] = aa2; 34 AllyMemberSelectedArray = new AllyMemberStatus[2] { aa1, aa2 }; 35 36 Debug.Log(AllyMemberStatusArray[0].GetCharactorName()); 37 Debug.Log(AllyMemberStatusArray[1].GetCharactorName()); 38 InitialGame = false; 39 } 40 } 41 42 // Update is called once per frame 43 void Update() 44 { 45 46 } 47} 48
AllyStatus.cs
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Linq; 5using UnityEngine; 6using UnityEngine.UI; 7 8[Serializable] 9[CreateAssetMenu(fileName = "AllyStatus" , menuName = "CreateAllyStatus")] 10public class AllyStatus : CharactorsStatus 11{ 12 13 14} 15
CharactorStatus.cs
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Linq; 5using UnityEngine; 6using UnityEngine.UI; 7 8[Serializable] 9public abstract class CharactorsStatus : ScriptableObject 10{ 11 12 [SerializeField] 13 private string charactorName = " "; 14 [SerializeField] 15 private string CharactorID = System.Guid.NewGuid().ToString("N"); 16 [SerializeField] 17 private int level = 1; 18 [SerializeField] 19 private JobType.JobKind jobType =JobType.JobKind.戦士; 20 [SerializeField] 21 private JobType.JobName jobName = JobType.JobName.戦士; 22 [SerializeField] 23 private WeaponEnum.AllWeapon equipWeapon; 24 25 [SerializeField] 26 private int earnedExperience = 0; 27 [SerializeField] 28 private int nextLvUpExperience = 0; 29 30 [SerializeField] 31 private int maxHP = 100; 32 [SerializeField] 33 private int nowHP = 100; 34 [SerializeField] 35 private int maxMP = 30; 36 [SerializeField] 37 private int nowMP = 30; 38 [SerializeField] 39 private int ATK = 20; 40 [SerializeField] 41 private int DEF = 10; 42 [SerializeField] 43 private int INT = 15; 44 [SerializeField] 45 private int MND = 10; 46 [SerializeField] 47 private int DEX = 10; 48 [SerializeField] 49 private int AGI = 10; 50 [SerializeField] 51 private int CRI = 5; 52 53 [SerializeField] 54 private bool isPoisonState = false; 55 [SerializeField] 56 private bool isNumbnessState = false; 57 [SerializeField] 58 private bool isSelected = false; 59 60/*上記の要素すべてのget setは省略。*/ 61 62 public AllyMemberStatus GetAllyMemberStatus() 63 { 64 AllyMemberStatus allyMemberStatus = new AllyMemberStatus(); 65 allyMemberStatus.SetCharactorName(charactorName); 66 allyMemberStatus.SetCharactorID(CharactorID); 67 allyMemberStatus.SetLevel(level); 68 allyMemberStatus.SetJobType(jobType); 69 allyMemberStatus.SetJobName(jobName); 70 allyMemberStatus.SetEquipWeapon(equipWeapon); 71 allyMemberStatus.SetEarnedExperience(earnedExperience); 72 allyMemberStatus.SetNextLvUpExperience(nextLvUpExperience); 73 allyMemberStatus.SetMaxHP(maxHP); 74 allyMemberStatus.SetNowHP(nowHP); 75 allyMemberStatus.SetMaxMP(maxMP); 76 allyMemberStatus.SetNowMP(nowMP); 77 allyMemberStatus.SetATK(ATK); 78 allyMemberStatus.SetDEF(DEF); 79 allyMemberStatus.SetINT(INT); 80 allyMemberStatus.SetMND(MND); 81 allyMemberStatus.SetDEX(DEX); 82 allyMemberStatus.SetAGI(AGI); 83 allyMemberStatus.SetCRI(CRI); 84 return allyMemberStatus; 85 } 86 87 88 89 // Start is called before the first frame update 90 void Start() 91 { 92 DontDestroyOnLoad(this); 93 } 94 95 96} 97 98
AllyMemberStatus
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class AllyMemberStatus : MonoBehaviour 6{ 7 public static string charactorName = " "; 8 public static string CharactorID; 9 public static int level = 1; 10 public static JobType.JobKind jobType = JobType.JobKind.戦士; 11 public static JobType.JobName jobName = JobType.JobName.戦士; 12 public static WeaponEnum.AllWeapon equipWeapon; 13 14 15 public static int earnedExperience = 0; 16 public static int nextLvUpExperience = 0; 17 18 public static int maxHP = 100; 19 public static int nowHP = 100; 20 public static int maxMP = 30; 21 public static int nowMP = 30; 22 public static int ATK = 20; 23 public static int DEF = 10; 24 public static int INT = 15; 25 public static int MND = 10; 26 public static int DEX = 10; 27 public static int AGI = 10; 28 public static int CRI = 5; 29 30 31 public static bool isPoisonState = false; 32 public static bool isNumbnessState = false; 33 public static bool isSelected = false; 34 35 public void SetCharactorName(string CharactorName) 36 { 37 charactorName = CharactorName; 38 } 39 40 public string GetCharactorName() 41 { 42 return charactorName; 43 } 44 public void SetCharactorID(string CharactorId) 45 { 46 CharactorID = CharactorId; 47 } 48 /*上記以外のすべての要素のget setは省略。*/ 49 50 // Start is called before the first frame update 51 void Start() 52 { 53 DontDestroyOnLoad(this); 54 } 55 56} 57
上記のコードを作成しました。AllyMemberStatus.csは実際に使用しているget,set以外略。原因不明の場合は修正で追記します。)
scriptableObjectのallyStatus型を読み込み、GetAllyMemberStatus()して、変換して、AllyMemberStatusArrayに保存しています。
出力結果
このコードを出力すると、配列にaa1を追加時はAllyMemberStatusArray[0].GetCharactorName()はちゃんと、「いち/1」のデータが入っているのですが、
aa2を追加すると、AllyMemberStatusArrayの[0]も[1]も「に/1」のデータになってしまいます。
(ちなみに、リストにしてみたり、同時にArrayにいれて初期宣言しても同様の出力です。)
回答1件
あなたの回答
tips
プレビュー