2016-08-15 7 views
0

私はC#とUnityの初心者ですが、テキストをインスタンス化することができるかどうか、その内容がインスペクタから編集された文字列配列に対応するかどうか疑問に思っています。私は何を意味異なるプロパティを持つインスタンス化されたテキストクローン?

は次のようである:

テキスト(こんにちは)TextClone1(あり)TextClone2(どのようにやっている)TextClone3(さようなら)

とテキスト(コンテンツ)のすべてにインスペクタから直接編集できるように、最終的にFacebookからの複数のメッセージのように見えるでしょう。

public class Wait : MonoBehaviour { 

    private int i = 0; 
    public string[] message; 
    public float t; 

    [SerializeField] 
    private Text toText; 

    public IEnumerator Message(float waitTime = 2f) 
    { 
     toText.text = message[i]; 
     i++; 
     waitTime = t; 
     yield return new WaitForSeconds(waitTime); 

    } 

    void Start() 
    { 
     StartCoroutine(startMessage()); 
    } 

    IEnumerator startMessage() 
    { 
     yield return StartCoroutine(Message(i)); 
     yield return StartCoroutine(Message(i)); 
     yield return StartCoroutine(Message(i)); 
     yield return StartCoroutine(Message(i)); 
    } 
+0

私はあなたが良いやっていると思います

私がこれまで持っているコードは次のようです。与えられたコードの問題は何ですか? –

+0

よく、文字列配列がそれに対応するようにテキストオブジェクトをインスタンス化したい:array [i] = text [i] – Sciencephile

+0

そしてあなたのコードはこれをしていないのですか? –

答えて

2

は、このコードを試してみてください:

public Transform containor; // Assign a UI Object like panel to this variable. This will hold all text objects. 
public Text textPrefab; // save a UI object (with a text component attached) as prefab in project and then assign it to this variable from inspector. 
public string[] array = new string[10]; // can set values from editor/inpector window 

int i = 0; 

IEnumerator Start() 
{ 
    foreach (var item in array) 
    { 
     yield return StartCoroutine(ShowMessage()); 
    } 
} 

IEnumerator ShowMessage() 
{ 
    yield return new WaitForSeconds(i); 
    Text newText = Instantiate<GameObject>(textPrefab.gameObject).GetComponent<Text>(); 
    newText.text = array[i]; 
    newText.transform.SetParent(containor); 
    i++; 
} 
+0

素晴らしい作品!ありがとう!今私はテキストのクローンを以前のものの下に表示する方法を把握する必要があります。 – Sciencephile

+0

VerticalLayoutGroupを使用:https://docs.unity3d.com/Manual/script-VerticalLayoutGroup.html –

関連する問題