配置確認(未完成)2017/10/25 AM11時変更
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;//必ず必要
public class dicPractice : MonoBehaviour {
public GameObject ballPre;//ボールプレファブ用
public InputField numInput;//インプットフィールド用
public InputField scnNumInput;//インプットフィールド用
public Button subBtn;//シーン戻るボタン
public Button addBtn;//シーン次ボタン
Dictionary<int, List<string>> timeList1;//多重化Dictionary
List<string> tempList ;//テンポラリ用のList
List<GameObject> goList;//生成したゲームオブジェクトの全リスト
int scenceNum;//現在のシーン番号用変数
void Start () {
//Dictionary,Listの追加
timeList1 = new Dictionary<int, List<string>> ();
tempList = new List<string>();
goList = new List<GameObject> ();
//※※シーン番号ゼロはシーンが空の意味に変更
scenceNum = 0;//現在のシーン番号初期化//保存データ読み込み時にはシーン番号を1以上にする
//シーン数がいくつかあるかチェックする空なら0を,
int scnDicCount = timeList1.Count;
if(scnDicCount < 1){
scnNumInput.text = scnDicCount .ToString ();//シーン番号の数字を文字に変換
}else{
scnNumInput.text = scenceNum.ToString ();//Dictionaryが空でなかったらシーン番号を表示させる(保存機能追加後に機能する)
}
//シーン番号0以下なら次シーンボタンオフに
if (scenceNum <= 1) {
subBtn.interactable = false;
}
//シーン番号がゼロで戻るボタンオフに
addBtn.interactable = false;
//json用オブジェクトを作成
myData jdata = new myData();
//キャラ10体作成
for (int i = 0; i < 10; i++) {
GameObject go = Instantiate (ballPre, new Vector3 (i * 2.0f, 0, 0), Quaternion.identity) as GameObject;
string myAIname = "AI" + i.ToString ();
go.name = myAIname;
//goListに 生成したgoをgoListに追加
goList.Add(go);
}
}
//番号で呼び出す用
public void numDel(){
int objNum = int.Parse (numInput.text);//インプットフィールドで受け取った番号をintに変換
int scnNum = int.Parse (scnNumInput.text);//インプットフィールドで受け取った番号をintに変換
scnNum--;//Dictionaryには0から収納されているので1引く
List<string> temp2List = new List<string> ();//一時的なList作成
Debug.Log ("呼び出し番号_" + objNum);
Debug.Log ("シーン番号_" + scnNum);
temp2List = timeList1 [scnNum];//シーン番号scnNumのJSONをLISTに読み込む
var myObject = JsonUtility.FromJson<myData>(temp2List [objNum]);//シーンゼロのobjNum番のJSONのみをオブジェクトに変換
Debug.Log ("X=" + myObject.posx);//デバッグ用
Debug.Log ("Z=" + myObject.posz);
Debug.Log ("name=" + myObject.name);
//Debug.Log ("time=" + myObject.time);
}
// シーン追加用
public void dicAdd(){
// int dicCount = timeList1.Count;//シーン番号の最大値確認用
// scnNumInput.text = dicCount.ToString ();//シーン番号の数字を文字に変換
//記録する準備
myData jdata = new myData ();
int i = 0;
//goListの中身をJSONにするforeach
foreach (var n in goList) {
jdata.id = i;
jdata.name = n.name;
jdata.posx = n.transform.position.x;
jdata.posy = n.transform.position.y;
jdata.posz = n.transform.position.z;
jdata.time = i * 10;//dummy data
string json = JsonUtility.ToJson (jdata);//オブジェクトをJSON文字列に変更
// Debug.Log(n.name);
tempList.Add (json);//LISTにJSONを追加
i++;
}
//timeList1に書き換えなのか追加なのかチェック
if (timeList1.ContainsKey(scenceNum ) )//現在のシーン番号がtimeList1に含まれているとき
{
timeList1[ scenceNum ] = tempList;//timeList1のscenceNumのリストを入れ替え
}
else
{ //現在のシーン番号がtimeList1に含まれていないとき
timeList1.Add( scenceNum , tempList );//timeList1に新規追加
//シーンが増えたので現在のシーンも追加
scenceNum++;
}
//ボタン関連処理
//現在のシーン番号が0より大きければ<ボタン表示
if (scenceNum > 1) {
subBtn.interactable = true;
}
//現在のシーン番号がtimeList1と同じなら>ボタン隠す
if (scenceNum == timeList1.Count) {
addBtn.interactable = false;
}
//現在のシーン番号がtimeList1より小さいなら>ボタン表示
if (scenceNum < timeList1.Count) {
addBtn.interactable = true;
}
scnNumInput.text = scenceNum.ToString ();//シーン番号の数字を文字に変換し表示
}
//シーン番号次へボタン
public void scnNumAdd(){
//現在のシーン番号を1足す
scenceNum++;
//シーン番号をInputFileldに表示書き換え
scnNumInput.text = scenceNum.ToString ();
//現在のシーン番号が0より大きければ<ボタン表示
if (scenceNum > 1) {
subBtn.interactable = true;
}
//現在のシーン番号がtimeList1より大きければ>ボタン隠す
if (scenceNum >= timeList1.Count) {
addBtn.interactable = false;
}
}
//シーン番号戻るボタン
public void scnNumSub(){
//現在のシーン番号を1減らす
scenceNum--;
//シーン番号をInputFileldに表示書き換え
scnNumInput.text = scenceNum.ToString ();
//現在のシーン番号が0以下であれば<ボタン隠す
if (scenceNum <= 1) {
subBtn.interactable = false;
}
//現在のシーン番号がtimeList1より小さければ>ボタン表示
if (scenceNum < timeList1.Count) {
addBtn.interactable = true;
}
}
}
[System.Serializable]
public class myData {
public int id;
public string name;
public int time;
public float posx;
public float posy;
public float posz;
}














