Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question A way to get the text mesh generated by text mesh pro without using a component

Discussion in 'UGUI & TextMesh Pro' started by noobnewbier, Aug 10, 2021.

  1. noobnewbier

    noobnewbier

    Joined:
    May 24, 2017
    Posts:
    6
    At the moment I am trying to write some customized editor code that draws on the scene view. The custom editor needs to draw text on the scene view to display information to designer. Attached screenshot is a demo of what I am drawing atm.

    The problem I am having is that Handles.Labels can be too consistent - I don't need my text to rotate and face the camera. I think what I need is something more like a mesh that can be drawn using
    Graphics.DrawMeshNow.

    Hence the question: is there a way for me to generate a text mesh using TMP without any TMP component(as I am not actually using any mono behaviours)?
     

    Attached Files:

  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,596
    When a text object is rendered, you can access the textInfo which contains information about every character, word, line, etc. but also the geometry of the text in the textInfo.meshInfo. As such, you can grab this mesh and geometry and reuse it any way you want. You will need to still use the correct material / shaders but that should all work.

    Take a look at the TextInfoDebugTool.cs file include in the TMP Examples & Extras. Add this script to any TMP text object and it will allow you to visible to content of the textInfo. This script should give you a good idea of how to access this information. There are other scripts like the VertexJitter.cs which also accesses the textInfo and the geometry to modify vertex positions. Again these script should be helpful in getting a better understanding of it all :)
     
  3. noobnewbier

    noobnewbier

    Joined:
    May 24, 2017
    Posts:
    6

    Thanks for the hasty reply, I had a look at the sample code but I am not entirely sure what you are referring to and it will be nice if I can have a bit more guidance. From what I can see, the sample code are all built from an assumption that the user already having some TMP component within the scene, and this is not the case for me.

    Here are a bit more context:

    [My Goal]
    I am trying to build a hex map editor where designer can put objects on the scene and modify the map(nodes weight, unit's positioning etc.) The underlying navigation of units are using the map data generated based on scene's
    gameObjects
    positioning - If tile at coordinate
    [x:2, y:3] 
    contains an obstacle, it is blocked and can't be traversed. The information is important for debugging purposes.

    Although the units positioning depends on the tiles, the tile and their coordinate should not be rendered in the scene - this is too much information for the player. So no text mesh pro component in the scene at all - in theory, I think I can have TMP components in only during edit time and destroy them on launches/pre build, but that seems like a bad idea.

    To sum up, I need some information to be displayed as text in the scene view during edit time. Attached is a screenshot of what I am having atm which is a brief demo of how coordinate should be visualized in the scene view. Note both the grid and the coordinate is rendered purely using Handles API, there are no additional gameObject placed in the scene as a placeholder in the given screenshot.

    [Problem]
    My problem is that using
    Handles.Label 
    does not yields the result I am looking for. There are various issues with
    Handles.Label
    in my use case, for example:
    1. As the label size is consistent regardless of distance, it can get bigger than the tile itself which makes it annoying - I sort of resolved it by doing some calculation to decrease the font size depending on the distance, although the result is not ideal as fontSize difference is at least 1 in Handles(so a relatively large change can be seen if the font size is already low).
    2. One can see the label even if the camera is looking from the back of the camera - this is not desired. I can stop drawing when the camera is from the back to solve this problem.
    3. As aforementioned, the text is rotating with the camera, as I think internally they are drawn using
      GUI.Label.
      Again this is not desired.
    Looking at these problems, I think I am facing these problems as I am trying to use
    Handles.Label
    to do something it is not designed for, and what I really want is to render a mesh that represents some text within the scene view.

    [Summary]
    I am speculating what I am looking for is something akin to
    TMP_Text.GenerateTextMesh
    but doesn't requires me having a TMP component in the first place. In my imagination it will be something like this(pseudo code):
    Code (CSharp):
    1. Mesh textMesh = TMPUtility.CreateMesh("RenderedText", font, fontStyle, fontSize, Color);
    How can I achieve something similar to this? Or am I looking at a completely wrong direction?
    Thanks for replying, junior programmer here and admittedly, sometimes I don't really know what I am looking for lol
     

    Attached Files:

  4. Gekigengar

    Gekigengar

    Joined:
    Jan 20, 2013
    Posts:
    742
    How this goes? I am aiming for something similar as well, wanted to try
    Code (CSharp):
    1. Gizmos.DrawMesh(someTextMesh, position);