Home > Software design >  Creating a monitor in Unity
Creating a monitor in Unity

Time:05-24

I have a prefab of a submarine I made in blender. In this submarine there is a "altitude" screen which is basically a cube I scaled and painted black. I can easly get the altitude of my submarine by doing sub.transform.position.y. Now I want to put a "Text" over the screen with the altitude I am getting.

My question is : What is the best way to do it ?

In an Ideal world I would have a function like TextObject putTextOnTop(GameObject the_panel) that would generate a textObject that I would then update each frame with my value.

Thanks

CodePudding user response:

There are multiple solutions for this.

There is an old 3D Text component in unity. This can do what you want. The Problem with this is, that you have only limited styling options.

A Better way is to use a WorldSpaceCanvas. If you use this option, you have all TextMeshPro styling options.

Here is a video: https://www.youtube.com/watch?v=GuWEXBeHEy8

The video is not very good, but i can't find a better one.

If you have any Questions left, feel free to ask

CodePudding user response:

You can put the Text GameObject to be the child of the screen (Cube GameObject), there are 2 ways for this:

  1. Set directly in Hierarchy

  2. Create a prefab TextObject and save it in Resources with path: Resources/UI/TextObject. Then in the code, instance and add it as a child of the Cube using the following code:

    GameObject textObject = Instantiate(Resources.Load("/UI/TextObject") as GameObject);
    
    textObject.transform.SetParent(cube.transform, false);
    

Then, in the Update function you add the following code:

void Update()
{
    TextObject.GetComponent<Text>().text =  sub.transform.position.y.ToString();
}

Hope it can help you!

  • Related