UnityでPrefabを使ったアプリを作っています。
Prefabを生成するスクリプト(ScrollController)で
Prefabを生成する際にある条件を満たした場合のみPrefab内のあるオブジェクトを
Activeにしたいのですがうまくできません。
Prefab内はこんな感じです。
(ContinuのCheckオブジェクトはデフォルトはactive(false)にしています)
生成するScrollController.csを生成する背景はこんな感じです。
(Scroll/Contentの下にlistPrefabを生成します)
具体的な質問)
ScrollController.csでlistPrefabを生成する際に
PlayerPrefsで条件(0か1)を取得して条件が1の場合は
CheckオブジェクトをSetActive(true)にして生成したいのですが
ScrollController.csからlistPrefab内のCheckオブジェクトを
参照する方法が分かりません。
以前PrefabのボタンからHierarchy上のオブジェクトを参照する方法は
こちらで教えてもらって理解できたのですが今回は逆(?)でよく分かりません。
分かる方お手数ですが教えて下さい。
環境)
PC: mac
Unity2018.2
言語:C#
ScrollController.csはこんな感じです。
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScrollController : MonoBehaviour { #pragma warning disable 649 [SerializeField]GameObject listPrefab; [SerializeField]GameObject continuCheckObject; //Prefab内なので参照できない private Text[] texts; private int continuChecked; // Start is called before the first frame update void Start() { for(int i = 0; i < 16; i++) { //prefabの生成 var item = GameObject.Instantiate(listPrefab) as GameObject; item.transform.SetParent(transform.transform, false); //prefab生成時にinputsciptコンボーネントにindexNoを渡す item.GetComponentInChildren<InputScript>().indexNo = i.ToString(); //1列目に数字を入れる texts = item.GetComponentsInChildren<Text>(); texts[0].text = (i + 1).ToString(); //2列目 texts[1].text = PlayerPrefs.GetFloat("distance" + i, 0).ToString("F1"); //3列目 continuChecked = PlayerPrefs.GetInt("continu" + i, 0); if(continuChecked == 1) { continuCheckObject.SetActive(true); }else{ continuCheckObject.SetActive(false); } } } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/13 07:27