soncis
soncis

Reputation: 150

How to procedurally attach text to generated gameobjects?

I have in my unity program created a bunch of cubes like this:

foreach (Cube cube in cubeList)
{
   if (cube.canStandOn)
   {
      GameObject block;
      block = GameObject.CreatePrimitive(PrimitiveType.Cube);
      block.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
      block.transform.position = new Vector3(cube._mPos.x + CubeSize.half, cube._mPos.y + CubeSize.half, cube._mPos.z + CubeSize.half);
      block.GetComponent<Renderer>().material.color = Color.blue;
   }
}

Now I want to add a text abvove each block detailing its position. The ammount of cubes and their positions could change every run so this has to be done programatically just like the how the blocks are created. I could not find anything on this Text Mesh seems like it would be a nice way to go but did not find any example created without having a previously created game object with a text mesh attatched. This is done in an android app so I cannot use the OnGizmos() function.

Doing it in OnGUI() might have been ok but that wont show up in the scene window which is where I want it as well.

any tips?

Upvotes: 2

Views: 972

Answers (2)

Gunnar B.
Gunnar B.

Reputation: 2989

Create a prefab that consists of a cube with a canvas as child (this in turn holds a UI text). Set the canvas render mode to World Space. Adjust the scale if all have the same. Preferably you actually create an empty gameobject and have the cube and the canvas be child of that.

To place the cube do something like this:

using UnityEngine.UI;     // required for the text component

public GameObject prefabCube;

...

Vector3 cubePos = new Vector3(x, y, z);
GameObject cube = (GameObject)Instantiate(prefabCube, cubePos, Quaternion.identity);
cube.GetComponentInChildren<Text>().text = x + " " + y + " " + z;    // not quite sure if this line is correct
cube.GetComponent<Renderer>().material.color = Color.blue;     // if all blocks have the same color, change this for the prefab directly
// if you use an empty the last has to be
cube.GetComponentInChildren<Renderer>().material.color = Color.blue;

This should be about correct.

Upvotes: 0

Programmer
Programmer

Reputation: 125305

Create another GameObject and make the cube its parent. Use addComponent to add TextMesh to that new GameObject under the cube.Assign the possible of the cube to the TextMesh then set the y a-xis of the TextMesh so that it will be on top of the cube. You also need to make textMesh.anchor and textMesh.alignment to be centered in the middle so that the TextMesh will be positioned with the cube. Depending on the size of the cube, you may need to adjust the textOffset variable. without scaling the cube, it worked well on my side. If it doesn't work for you, just scale the scale the TextMesh like you scaled the cube.

int textOffset = 2;
foreach (Cube cube in cubeList)
{
   if (cube.canStandOn)
   {
      GameObject block;
      block = GameObject.CreatePrimitive(PrimitiveType.Cube);
      block.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
      block.transform.position = new Vector3(cube._mPos.x + CubeSize.half, cube._mPos.y + CubeSize.half, cube._mPos.z + CubeSize.half);
      block.GetComponent<Renderer>().material.color = Color.blue;

      ////////////////////////TEXT MESH CODE//////////////////////

        //Create new GameObject
        GameObject childObj = new GameObject();

        //Make block to be parent of this gameobject
        childObj.transform.parent = block.transform;
        childObj.name = "Text Holder";

        //Create TextMesh and modify its properties
        TextMesh textMesh = childObj.AddComponent<TextMesh>();
        textMesh.text = "Blah Blah Blah";
        textMesh.characterSize = 1;

        //Set postion of the TextMesh with offset
        textMesh.anchor = TextAnchor.MiddleCenter;
        textMesh.alignment = TextAlignment.Center;
        textMesh.transform.position = new Vector3(block.transform.position.x,  block.transform.position.y + textOffset, block.transform.position.z);

   }
}

Upvotes: 3

Related Questions