Jump to content
Unity Insider Forum

wie positioniert man Text in einem Canvas via script?


peachplayer
 Share

Recommended Posts

Hallo Leute

Weiss jemand wie man Textfelder in einem Canvas via Script anordnen kann. Habs wie folgt probiert:

void Start()
{       
        ArrayList arrayList = new ArrayList();
        UnityEngine.Object[] allObject = (UnityEngine.UI.Text[])FindObjectsOfType(typeof(UnityEngine.UI.Text));
        foreach (UnityEngine.UI.Text item in allObject)  
        {
            UnityEngine.UI.Text go = item;
            if (go != null) 
            {
                arrayList.Add(go);
            }
        }
        foreach (UnityEngine.UI.Text go in arrayList)
        {
            Debug.Log(go.name);
            RectTransform rectTransform;
            rectTransform = go.GetComponent<RectTransform>();
            rectTransform.localPosition = new Vector3(0 + i, 0, 0);
            rectTransform.sizeDelta = new Vector2(200, 200);
            go.GetComponent<RectTransform>().localPosition = new Vector2(200 + i, 500);
       
            i += 50;
        }
}

Gruss, peachplayer

Link to comment
Share on other sites

Habs jetzt mit einem Panel versucht:

   ArrayList arrayList = new ArrayList();

        UnityEngine.UI.Text[] allObject = (UnityEngine.UI.Text[])FindObjectsOfType(typeof(UnityEngine.UI.Text));


        foreach (UnityEngine.UI.Text item in allObject)
        {
            UnityEngine.UI.Text go = item;
            switch (go.name)
            {
                case "Text_Flurname":
                    arrayList.Add(go);
                    break;
                case "Text_Feldarbeiten":
                    arrayList.Add(go);
                    break;
                case "Text_Kosten":
                    arrayList.Add(go);
                    break;
                case "Text_fläche":
                    arrayList.Add(go);
                    break;
                case "Text_Kultur":
                    arrayList.Add(go);
                    break;
                case "Text_Datum":
                    arrayList.Add(go);
                    break;
                case "Text_nr":
                    arrayList.Add(go);
                    break;

            }
        }

        var canvas_transform = panel.transform;

        int i = 0;

        foreach (UnityEngine.UI.Text go in arrayList)
        {
            Debug.Log(go.name);
            // Provide Text position and size using RectTransform.
            RectTransform AnzeigeRect = go.GetComponent<RectTransform>();

            go.transform.SetParent(canvas_transform);


            canvas_transform.GetComponent<RectTransform>().anchoredPosition = new Vector2(200 + i, 200);

            i = i + 50;

        }

Leider wird der Text nicht richtig positioniert. Gruss, peachplayer

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...