Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by vrfinance · Jul 25, 2017 at 10:17 AM · cube3dtext

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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Light,Shadow: My Cube is beige instead of white 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges