Retrieving a font's texture

Hello,
I am walking myself through a step by step process of how to hide text behind walls (through http://wiki.unity3d.com/index.php?title=3DText), but I want to do this through the creation of manually created GameObjects created from scratch through scripts.

The problem I stumble across, is that the Font class has a material property, BUT, it doesn’t have a texture property. How would I assign something like…

Font downloadedFontVariable = Resources.Load(“Fonts/font folder/downloadedFontName”) as Font;
material.SetTexture(“_MainTex”, downloadedFontVariable.texture);

… if the Font class doesn’t have a texture property? Is there another way?

Thanks

I suspect that you’re putting a lot of unnecessary effort into making old text rendering methods work, when you should be using the new (easier, better-looking) Unity UI instead. If you’re using the TextMesh component, stop and check out Unity’s UI tutorials, which use the much-better modern system.

You could also use TextMeshPro, which is maintained and has a lot more options, at the cost of being more complex.

Both TMP and the Unity UI Text object are correctly obscured by objects in front of them.

The Unify Wiki has a lot of obsolete code examples on it, sadly - it’s been kind of a ghost town for a number of years, and no one has bothered to remove articles about systems that have been obsolete in Unity for years.

1 Like

Thank you. Is there any way I can get the local vertical and local horizontal sizing of what a TextMesh produces (so I can create my own word-wrap and letter-limitation code)?.. or is there already a way to do this for TextMeshs?

I did something pretty neat, to where all you have to do, is add this script to any cube, and it will automatically add hide-able text to it through a newly created child object. Direction tells what side of the cube to add text to. You can use Front, Right, Back, Left, Up or Down. Offset tells how far off the cube you want the text. Message is what message to put on the cube. fontName tells what fond to load from the resources folder. shaderName tells what shader to use and matName tells what material to use (all loaded from the resources folder). BECAUSE there is no fontVariable.texture property, I had to get the texture off another cube (called textObj), and put it onto the child object, and it works that way. Now, all I am left to do is try and see if there is a way to ‘word-wrap’ the text off the TextMesh horizontally, and limit the text vertically (so none of the text goes off the cube). Is there a way to do this?..

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Writing : MonoBehaviour {

    public GameObject textObj;

    public string direction;
    public float offset = 0.001f;

    public string message;
    public string fontName = "Fonts/magnificent/Magnificent";
    public string shaderName = "Shaders/TextOneSided";
    public string matName = "Materials/Font Materials/FontMaterial";

    private GameObject sheetObj;

    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {

        GameObject obj = null;

        int cnt = 0;

        foreach (Transform child in transform)
        {
            cnt++;
        }

        if (cnt == 0)
        {
            obj = new GameObject();
            obj.transform.parent = transform;

            obj.AddComponent<MeshRenderer>();

            TextMesh objMesh = obj.AddComponent<TextMesh>();

            sheetObj = obj;
        }

        if (direction == "Front")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3(0, 0, (obj.transform.localScale.z * 1.0f / 2) + offset);
            }
        }
        if (direction == "Right")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3((obj.transform.localScale.x * 1.0f / 2) + offset, 0, 0);
            }
        }
        if (direction == "Back")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3(0, 0, -(obj.transform.localScale.z * 1.0f / 2) - offset);
            }
        }
        if (direction == "Left")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3(-(obj.transform.localScale.x * 1.0f / 2) - offset, 0, 0);
            }
        }
        if (direction == "Up")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3(0, (obj.transform.localScale.y * 1.0f / 2) + offset, 0);
            }
        }
        if (direction == "Down")
        {
            if (obj != null)
            {
                obj.transform.localPosition = new Vector3(0, -(obj.transform.localScale.y * 1.0f / 2) - offset, 0);
            }
        }

        if (obj != null) {
      
            obj.transform.LookAt(transform.position);

            obj.transform.Translate(-new Vector3(0.5f, -0.5f, 0));

            obj.transform.localScale = new Vector3(1, 1, 0.0001f);

            Material mat = Resources.Load(matName) as Material;

            mat.shader = Resources.Load(shaderName) as Shader;

            Font font = Resources.Load(fontName) as Font;

            mat.SetTexture("_MainTex", textObj.GetComponent<MeshRenderer>().material.GetTexture("_MainTex"));

            obj.GetComponent<TextMesh>().font = font;

            obj.GetComponent<TextMesh>().text = message;

            obj.GetComponent<TextMesh>().characterSize = 0.1f;

            obj.GetComponent<MeshRenderer>().material = mat;

        }

        if (sheetObj != null)
        {
            sheetObj.GetComponent<TextMesh>().text = message;
        }

    }
}

Lamest answer ever. So to say, Unity UI is the worst thing to use in entire Unity engine, for multiple reasons.
The question was “How to”, not “Alternative simple way”

If someone is following an obsolete tutorial from a hopelessly obsolete wiki, then “how to” is often just plain not worth answering. The tutorial will be dependent on so many outdated systems that have changed in who knows how many ways that it may not even be possible to do what’s being asked without significant hacks and changes that are well beyond the scope of the question. Even if it is, someone learning to do something should never be taught obsolete techniques; it helps them none.

1 Like

I didn’t notice the issue date :slight_smile: