Jump to content
Unity Insider Forum
peachplayer

Text an GameObjekt binden

Recommended Posts

Hallo Leute

ich möchte eine Art Balkendiagramm mit Unity erstellen. Die rechteckigen Balken habe ich grafisch schon hingekriegt, jedoch fehlen mir jetzt noch

die Beschriftungen für die zugehörigen Werte. Weiss jemand wie man einen Text an ein GameObjekt binden kann? Habs mit folgender Funktion probiert:

    GameObject CreateText(LineRenderer canvas_transform, float x, float y, string text_to_print)
    {
        GameObject UItextGO = new GameObject("Text2");
       

        RectTransform trans = UItextGO.AddComponent<RectTransform>();
        trans.anchoredPosition = new Vector2(x, y);

        Text text = UItextGO.AddComponent<Text>();
        text.text = text_to_print;
       

        return UItextGO;
    }

Gruss, peachplayer

Share this post


Link to post
Share on other sites

Meinst du ein 3d Balkendiagramm in der "Spielwelt" und jetzt willst du ein UGUI Text über einem Balken fliegen lassen?

Das geht ganz einfach, indem du dieses Script auf den UGUI Text legst und das Objekt (dein Balken) als Referenz hinzu fügst.

public Transform tObject;

void Update()
{
   transform.position = Camera.main.WorldToScreenPoint(tObject.position);
}

 

Share this post


Link to post
Share on other sites
 IEnumerator BarGraph(List<GraphData> gd)
    {

        float gap = 1;

        // X axis starts from 0 to 4 so according to my logic i have to check if no. of data
        // is > 4 than we should divide the lenght of x axis with the data count, this will
        // give us the gap between bars that we need to keep.

        if (gd.Count > 4)
        {
            gap = 30.0f / gd.Count;
        }

        float xIncrement = gap;
        int dataCount = 0;
        Vector3 startpoint = Vector3.zero;

        while (dataCount < gd.Count)
        {
            int i = dataCount;
            Vector3 endpoint = new Vector3(xIncrement, gd[dataCount].income, 0.96f);
            startpoint = new Vector3(endpoint.x, 0, 0.96f);
            // pointer is an empty gameObject, i made a prefab of it and attach it in the inspector
            GameObject p = Instantiate(pointer, new Vector3(startpoint.x, startpoint.y, 0.97f), Quaternion.identity) as GameObject;
            p.transform.parent = holder.transform;
       
            // linerenderer is an empty gameObject with Line Renderer Component Attach to it, 
            // i made a prefab of it and attach it in the inspector
            GameObject test1 = Instantiate(linerenderer, transform.position, Quaternion.identity) as GameObject;
            test1.transform.parent = holder.transform;
            test1.name = dataCount.ToString();
         
            
            LineRenderer lineRenderer = test1.GetComponent<LineRenderer>();

            lineRenderer.material = mat;
            lineRenderer.SetWidth(0.30F, 0.30F);
            lineRenderer.SetVertexCount(2);
  

            while (p.transform.position.y < endpoint.y)
            {
                p.transform.Translate(Vector3.up * Time.deltaTime * 8, Space.World);

                lineRenderer.SetPosition(0, startpoint);
                lineRenderer.SetPosition(1, p.transform.position);
            
                yield return null;
            }

            lineRenderer.SetPosition(0, startpoint);
            lineRenderer.SetPosition(1, endpoint);

            p.transform.position = endpoint;
            var parzellen2 = gd.GroupBy(item => item.Parzname);
            foreach (var parzelle1 in parzellen2)
            {
                addScoreText.text = parzelle1.Key;

                addScoreText.transform.position = Camera.main.WorldToScreenPoint(lineRenderer.transform.position);

            }
            startpoint = endpoint;
            dataCount++;
            xIncrement += gap;

            yield return null;

        }
    }

und so sieht die ganze Coroutine aus, welche die Balkendiagramme zeichnen sollte. Leider erscheint noch kein Beschriftungs-text.

Gruss, peachplayer

Share this post


Link to post
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...

×
×
  • Create New...