2016-08-15 2 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 –

관련 문제