How to add text to cubes generated through c# script
Hello, I am working on a bar chart application in which I am plotting bar chart using cube. I want to display the value for each cube as a text on the cube surface. How can I do this in c# script.
below is the code I am using for plotting chart:
void Start() { alColor.Add(Color.red); alColor.Add(Color.green); alColor.Add(Color.blue); ArrayList alData = TextRead(Application.dataPath + "/Scripts" + "/ChartData.txt"); generateColoredCubeSquare(alData); }
private float x1, y1;
private float z1;
private int i = 0;
private float x = 0, z = -70;
private GameObject cube;
public void generateColoredCubeSquare(ArrayList a)
{
int ColorIndex = -1;
foreach (String[] o in a)
{
if (o.Length > 1)
{
x1 = float.Parse(o[0]);
y1 = float.Parse(o[1]);
z1 = float.Parse(o[2]);
//LineRenderer LineRenderer = new LineRenderer();
//LineRenderer.material.color = Color.red;
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
//if (ColorIndex == 0)
//{
cube.transform.position = new Vector3(x, 1, z);
//}
//if (ColorIndex == 1)
//{
// cube.transform.position = new Vector3(x * (-1), 1, z);
//}
//else
//{
// cube.transform.position = new Vector3(x, 1, z * (-1));
//}
cube.transform.localScale = new Vector3(x1, y1, z1 / 4);
//cube.transform.position = new Vector3(x, cube.transform.localScale.y / 2, z);
MeshRenderer rend = cube.GetComponent<MeshRenderer>();
rend.material.color = (Color)alColor[ColorIndex];
//TextMesh variableForPrefab = (TextMesh)Resources.Load("Assets/Prefabs/Prefab");
//TextMesh txtMesh = (TextMesh)Instantiate(variableForPrefab);
////TextMesh txtMesh = txtMeshTransform.GetComponent<TextMesh>();
//cube.AddComponent<TextMesh>();
//txtMesh.text = "New text set through script";
//TextMesh tm = cube.AddComponent<TextMesh>();
//tm.transform.parent = cube.transform;
//tm.text = y1.ToString();
x = x + 5f;
}
else
{
ColorIndex = ColorIndex + 1;
x = (ColorIndex * 10) + 10;
z = z + 50;
}
}
}
private ArrayList TextRead(string fileName)
{
string[] entries = null;
ArrayList alData = new ArrayList();
// Handle any problems that might arise when reading the text
try
{
string line;
// Create a new StreamReader, tell it which file to read and what encoding the file
// was saved as
StreamReader theReader = new StreamReader(fileName, Encoding.Default);
// Immediately clean up the reader after this block of code is done.
// You generally use the "using" statement for potentially memory-intensive objects
// instead of relying on garbage collection.
// (Do not confuse this with the using directive for namespace at the
// beginning of a class!)
using (theReader)
{
// While there's lines left in the text file, do this:
do
{
line = theReader.ReadLine();
if (line != null)
{
// Do whatever you need to do with the text line, it's a string now
// In this example, I split it into arguments based on comma
// deliniators, then send that array to DoStuff()
entries = line.Split(',');
alData.Add(entries);
if (entries.Length > 0)
{
}
}
}
while (line != null);
// Done reading, close the reader and return true to broadcast success
theReader.Close();
return alData;
}
}
// If anything broke in the try block, we throw an exception with information
// on what didn't work
catch (Exception e)
{
Console.WriteLine("{0}\n", e.Message);
return null;
}
}
Comment
Your answer
Follow this Question
Related Questions
[Problem] Change 3DText color with C# script 1 Answer
Roll/rotate a cuboid at its edge 2 Answers
Minecraft random generated perlin noise? 0 Answers
Unsavory collisions with terrain? 0 Answers