Skip to content

Instantly share code, notes, and snippets.

Created November 8, 2015 10:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/10de0cd126923242a571 to your computer and use it in GitHub Desktop.
Save anonymous/10de0cd126923242a571 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;//UI オブジェクトを扱う時は必須
public class QuizMgr : MonoBehaviour {
//http://trick7.com/2014/08/newui/ 動的にボタンを作る
public GameObject content;
public GameObject buttonPrefab;
//http://engineer.blog.lancers.jp/2015/05/ugui_quiz_unity/
//アタッチしたオブジェクトが呼ばれた時に実行される。
void Start () {
// QuestionLabelSet ();
AnswerLabelSet ();
}
private void QuestionLabelSet(){
//特定の名前のオブジェクトを検索してアクセス
Text qLabel = GameObject.Find("Quiz/QLabel").GetComponentInChildren<Text> ();
//データをセットすることで、既存情報を上書きできる
qLabel.text = "ランサーズ君は何歳?";
}
private void AnswerLabelSet(){
//プレハブの取得
GameObject prefab = (GameObject)Resources.Load("Prefabs/AnsButton");
// Vertical Layout Group コンポーネントをつけたGameObject
Transform listContent = transform.FindChild("Content");
//回答文面の作成
string[] array = new string[]{"東京","埼玉","神奈川","千葉"};
int x =array.Length;//配列の長さ
//ボタンが4つあるのでそれぞれ代入
for (int i=1; i<=array.Length ; i++){
//プレハブからボタンを生成
GameObject listButton = Instantiate(prefab) as GameObject;
//Vertical Layout Group の子にする (Content)
listButton.transform.SetParent(listContent,false);
//適当にボタンのラベルを変える
listButton.transform.FindChild("Text").GetComponent<Text>().text = "TTTT";
// Text ansLabel = GameObject.Find("Canvas/ScrollController/Content/AnsButton" + i).GetComponentInChildren<Text> ();
// ansLabel.text = array[i-1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment