実現したいこと
作成したインプットフィールドに値を入力する際、個別に認識し
それをセーブし次回アプリを起動した際に個別に読み込む。
問題点
Instantiateでインスタンスを作成しているため、個別に値を変数に代入する方法がわからず全ての作成したインスタンスにセーブした値が適応されてしまう。
inputinstance[i].onEndEdit.AddListener(OnSubmit);で
入力された値(value)をPlayerPrefs.SetString(key,value);でkeyに保存しているがこれを個別の配列に対応させ、inputfuieldclone.GetComponentInChildren<Text> ().text = PlayerPrefs.GetString (key);
をする際それぞれのインプットフィールドインスタンスにセーブしたい。
よろしくお願いします。
c#
1using UnityEngine; 2using System.Collections; 3using UnityEngine.UI; 4using System.Collections.Generic; 5public class sample2 : MonoBehaviour { 6 7 [SerializeField] 8 private GameObject inputfuield; 9 //インスタンス表示数 10 const int inputfuield_count = 6; 11 12 public InputField[] inputinstance = new InputField[10]; 13 14 [SerializeField] 15 public RectTransform parent; 16 17 public string key; 18 19 void Start () 20 { 21 22 23 for (int i = 0; i < inputfuield_count; i++) 24 { 25 int no = i; 26 //インプットフィールド生成 27 GameObject inputfuieldclone = (GameObject)Instantiate(inputfuield); 28 //できたインスタンスをContentの子に設定 29 inputfuieldclone.transform.SetParent(parent, false); 30 31 inputinstance[i] = inputfuieldclone.GetComponent<InputField> (); 32 33 inputinstance[i].onEndEdit.AddListener(OnSubmit); 34 35 inputfuieldclone.GetComponentInChildren<Text> ().text = PlayerPrefs.GetString (key); 36 } 37 38 } 39 40 41 public void OnSubmit(string value) 42 { 43 PlayerPrefs.SetString(key,value); 44 45 Debug.Log("Submit value is " + value); 46 47 } 48 49}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/15 05:52
2017/09/15 06:41
2017/09/15 14:35 編集
2017/09/16 07:53
2017/09/16 14:54