Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Creating a 3D text object in world in C#

Discussion in 'Scripting' started by JeffreyVentrella, Aug 7, 2023.

  1. JeffreyVentrella

    JeffreyVentrella

    Joined:
    Jun 16, 2016
    Posts:
    12
    Hello,

    I want to create a 3D text object entirely in C# having a full transform just like any old GameObject. I want it to behave like a regular 3D object. Nothing fancy. I understand I should use TextMesh Pro but I can't find a simple straightforward code example.

    I'm using Unity version 2022.3.5f1. Is there an example in C# with all the required lines of code?

    Thanks!
    -j
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Do you actually want 3D text or you want 2D text (ie, flat object like a text bubble) in a 3D world?
     
    JeffreyVentrella likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    That's not really a thing... you need to understand all the parts of your problem to engineer a solution.

    That's why Brathnann is asking you stuff because your definition and declaration of problem space is impossibly vague.

    Try asking yourself "Can I ... ?" again and again.

    Imphenzia: How Did I Learn To Make Games:

     
  4. JeffreyVentrella

    JeffreyVentrella

    Joined:
    Jun 16, 2016
    Posts:
    12
    Hi Brathnann! Thank you for asking me for clarification. Yes, I do need flat 2D text, like text characters printed on an invisible plane that can be oriented in 3D space, not fat, extruded text characters.

    FYI I have since loaded TextMesh Pro and so I am able to create a Text (TMP) in the Unity editor. I just need to do it in code :) Once I have the basics, I can figure out the config details. Thanks!
    -j
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I think you just need a GameObject and call AddComponent<T>() on it and add whatever TMPro type of thing you want to add.

    Warning: the one that lives on a canvas is VERY hard to make in code because it is beholden to the RectTransform. The basic worldspace one is a lot easier to make but I think you still need to connect up the font asset.

    I like this pattern for all my in-code fabrications:

    Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

    https://gist.github.com/kurtdekker/5dbd1d30890c3905adddf4e7ba2b8580

    EDIT: yeah, it's pretty straightforward: check it:

    Code (csharp):
    1. using UnityEngine;
    2. using TMPro;
    3.  
    4. public class MakeTMP : MonoBehaviour
    5. {
    6.    void Start()
    7.    {
    8.        var tmp = gameObject.AddComponent<TextMeshPro>();
    9.        tmp.text = "Yabo!";
    10.        tmp.transform.position = new Vector3( 2, -2, 10);
    11.        tmp.transform.rotation = Quaternion.Euler( 0, 45, 0);
    12.    }
    13. }
     
    Last edited: Aug 8, 2023
    ijmmai and JeffreyVentrella like this.
  6. JeffreyVentrella

    JeffreyVentrella

    Joined:
    Jun 16, 2016
    Posts:
    12
    Kurt-Dekker,
    Thanks for the code snippet. It works. That's all I needed to get started.
    -j
     
    Kurt-Dekker likes this.
  7. JeffreyVentrella

    JeffreyVentrella

    Joined:
    Jun 16, 2016
    Posts:
    12
    Hello!

    I'm still having the same problems.

    I have a few questions. Polite answers greatly appreciated.

    Below is code derived from an example someone gave me. I tried to whittle it down to the bare essentials so I could get a handle on it. It displays my text, but there are issues:

    1. The position as set with rect is not consistent with world coordinates. The text appears close to the origin but the x value is 10, and the y value appears to have no effect (perhaps these are meant as "text" coordinates?) I understand this is an issue others have had, but I couldn't find an answer. I would (LOVE) it if a change could be added to the code below so that I can set the position, rotation, and scale in a way that is consistent with other world objects. I don't know how to state that any clearer.

    2. There are several layers here. Maybe that's necessary for displaying texts in the world, but it seems like a lot. Are all of these components really necessary?

    As I said, this is derived from code that someone else gave me. I would like to make it as clear as possible and then share it as a solution because I think it might help other people who just want to display texts in world coordinates using only code.

    Also, note that I do not need any other UI capabilities. I just need to be able to make a bunch of labels and to transform them using world coordinates.

    Any suggestions would be greatly appreciated!

    Here's the code:

    Code (CSharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using TMPro;
    6. using Unity.VisualScripting;
    7. using UnityEngine;
    8. using UnityEngine.UI;
    9.  
    10. public class UIManager : MonoBehaviour
    11. {
    12.     private RectTransform rect;
    13.     private float x;
    14.    
    15.     void Start()
    16.     {
    17.         x = 10.0f;
    18.        
    19.         GameObject canvasObject = new GameObject("UICanvas");
    20.         Canvas canvas = canvasObject.AddComponent<Canvas>();
    21.         canvas.renderMode = RenderMode.WorldSpace;
    22.         canvas.worldCamera = Camera.main;
    23.         canvasObject.AddComponent<CanvasScaler>();    
    24.  
    25.         RectTransform panel;
    26.         panel = new GameObject("Panel").AddComponent<RectTransform>();
    27.         panel.transform.SetParent( canvasObject.transform );
    28.         panel.AddComponent<CanvasRenderer>();
    29.        
    30.         GameObject newGameObject = new GameObject();
    31.         rect = newGameObject.AddComponent<RectTransform>();
    32.         rect.position = new Vector3( 10.0f, -1.3f, 0.1f );
    33.         newGameObject.transform.SetParent( panel );
    34.  
    35.         TextMeshPro theText = newGameObject.AddComponent<TextMeshPro>();
    36.         theText.text = "Test";
    37.         theText.fontSize = 0.2f;
    38.     }
    39.    
    40.      void Update()
    41.     {
    42.         x += 0.0001f;
    43.         rect.position = new Vector3( x, -1.3f, 0.1f );
    44.         //rect.eulerAngles = new Vector3( x, 0.0f, 0.0f );
    45.     }
    46. }
     
  8. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    It's still possible to create 3D text without having to use any external packages.

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TextSpawner : MonoBehaviour
    4. {
    5.     void Start()
    6.     {
    7.         CreateText("I no longer like turtles",Vector3.zero,Quaternion.identity);
    8.     }
    9.  
    10.     void CreateText(string text,Vector3 position, Quaternion rotation)
    11.     {
    12.         GameObject obj=new GameObject("Text");
    13.         obj.transform.position=position;
    14.         obj.transform.rotation=rotation;
    15.         TextMesh myText=obj.AddComponent<TextMesh>();
    16.         myText.text=text;
    17.         myText.anchor=TextAnchor.MiddleCenter;
    18.         myText.characterSize=0.1f;
    19.         myText.fontSize=64;
    20.     }
    21. }
    22.  
    23.  
     
    Last edited: Oct 2, 2023