前提・実現したいこと
クリックで生成したオブジェクトの座標をjsonに保存させ、それをクリックした場所通りロードするようにしたいです。
ソースコード
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Creator_Prefab : MonoBehaviour { public GameObject prefab; private Vector3 mousePosition; public int count_Preafab =0; private int count = 0; public static List<GameObject> Prefab_List = new List<GameObject>(); void Update() { Onprefab(); } public void Onprefab() { if (Input.GetMouseButtonDown(0))//左クリック { count++; if (count <= count_Preafab) { mousePosition = Input.mousePosition; mousePosition.z = 10.0f;//z軸を修正 Instantiate(prefab, Camera.main.ScreenToWorldPoint(mousePosition), Quaternion.identity); var obj = Instantiate(prefab) as GameObject; Prefab_List.Add(obj); Vector3 position = Prefab_List[i].transform.position; Debug.Log(position); } } } }
ソースコード
using UnityEngine; using UnityEngine.UI; using System.IO; using System.Collections.Generic; public class save_data : MonoBehaviour { [SerializeField] InputField inputArea; Creator_Prefab Creator; [System.Serializable] public class Stage_Data { public int ID; public string StageName; public List<Vector2> Prefab_List_Save = new List<Vector2>(); } void Start() { Creator = GetComponent<Creator_Prefab>(); } Stage_Data myData = new Stage_Data(); public void SavePlayerData() { StreamWriter writer; var StageName = inputArea.text; myData.StageName = StageName; string jsonstr = JsonUtility.ToJson(myData); writer = new StreamWriter(Application.dataPath + "/save" + StageName + ".json", false); writer.Write(jsonstr); writer.Flush(); writer.Close(); Debug.Log(myData.StageName + "のデータを保存しました"); } public void LoadPlayerData() { string datastr = ""; var StageName = inputArea.text; StreamReader reader; reader = new StreamReader(Application.dataPath + "/save" + StageName + ".json"); datastr = reader.ReadToEnd(); reader.Close(); myData = JsonUtility.FromJson<Stage_Data>(datastr); Debug.Log(myData.StageName + "のデータをロードしました"); } }
試したこと
Stage_Dataの中に、Listを作りその中に格納するっという事を試してみました。
サイトや自分で試行錯誤してみましたが、実現できませんでした。
(頭よくないので、ここまでしかできませんでした。)
補足情報(FW/ツールのバージョンなど)
2021.1.7f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/02/01 04:13
2022/02/01 09:46 編集