###前提・実現したいこと
一つのListをデータ一覧として使いそれらを参照して別々に使用・加工(変更)。参照と言いますが実際に参照でデータをとってしまうと使用はできますがデータを加工する時に元のデータ一覧も変わってしまいます。何か解決策はないでしょうか?
また、もしListで実現できないなら別の方法が何かあるでしょうか?
お願いします。
###発生している問題・エラーメッセージ
エラーメッセージはありません
###該当のソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharPrefab { public int id; public int rear; public int efectnumber; public string name; public int[] move = new int[8]; public string efect; public bool efect_onoff = false; public CharPrefab(int i,int r,int en ,string n,int N,int NE,int E,int SE,int S,int SW,int W,int NW){ id = i; rear = r; efectnumber = en; name = n; move[0] = N; move[1] = NE; move[2] = E; move[3] = SE; move[4] = S; move[5] = SW; move[6] = W; move[7] = NW; } }
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Decktest { //データ一覧用。値は固定させたい public List<CharPrefab> chars = new List<CharPrefab>(); public void AllAdd(){ chars.Add (new CharPrefab (0,1,4,"歩",1,0,0,0,0,0,0,0)); chars.Add (new CharPrefab (1,2,4,"香",8,0,0,0,0,0,0,0)); chars.Add (new CharPrefab (2,2,4,"馬",0,2,0,0,0,0,0,2)); chars.Add (new CharPrefab (3,3,4,"銀",1,1,0,1,0,1,0,1)); chars.Add (new CharPrefab (4,3,4,"金",1,1,1,0,1,0,1,1)); chars.Add (new CharPrefab (5,4,7,"飛",8,0,8,0,8,0,8,0)); chars.Add (new CharPrefab (6,4,7,"角",0,8,0,8,0,8,0,8)); chars.Add (new CharPrefab (7,5,0,"王",1,1,1,1,1,1,1,1)); } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; public class GameManager : MonoBehaviour { public Decktest deck = new Decktest(); public CharTemplate tempA; public CharTemplate tempB; public List<CharPrefab> ListA = new List<CharPrefab>(); public List<CharPrefab> ListB = new List<CharPrefab>(); void Start () { deck.AllAdd(); //データ一覧読み込み //※今回はListA,Bと同じデータを入れていますが違うこともあります。つまりコピーなどでは対応できません。 for(int i=0;i<9;i++){ ListA.Add(deck.chars[i]); } for(int i=0;i<9;i++){ ListA.Add(deck.chars[i]); } tempA.InitStatus(ListA[0]); tempB.InitStatus(ListB[0]); tempA.Move_skills(deck.chars[tempA.efectnumber].move); Debug.Log(tempA.move[0]); Debug.Log(tempB.move[0]); } // Update is called once per frame void Update () {} }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class CharTemplete :MonoBehaviour{ //アタッチするクラス public int id; public int rear; public int efectnumber; public string name; public int[] move = new int[8]; public bool efect_onoff = false; //ステータスを付与 public void InitStatus(CharPrefab t){ this.id = t.id; this.rear = t.rear; this.efectnumber = t.efectnumber; this.name = t.name; this.move = t.move; efect_onoff = false; } public void Move_skills(int[] m){ for(int y = 0;y<8;y++){ this.move[y] = this.move[y] + m[y]; if(move[y]>8) move[y] = 8; } } void Start() { } void Update() { } }
###試したこと
・回答にならって
GameManagerのStart()に
ListB = new List<CharPrefab>(listA); を追加
⇨変化なし
・chars自体をコピーして2つにして別々のデータ一覧からListA,Bに格納する
charsをコンストラクタで2つにコピーする方法
⇨変化なし
charsの内容をforeachで、新しく作ったListに格納していく
⇨変化なし
後者の2つでもできないとは...
まだどこかで同じところを参照しているということでしょうか
・Deckクラス,CharPrefabクラスは変化させたくないため構造体に
⇨変化なし
###補足情報(言語/FW/ツール等のバージョンなど)
unity2017.2.0f3
C#
最悪自分用と敵用のデータ一覧を作ればよさそうですが、できればやりたくはないです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/12/29 03:57