Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Retrieving a font's texture

Discussion in 'Scripting' started by EnsurdFrndship, Apr 4, 2017.

  1. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    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
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,776
    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.
     
    Shmardow likes this.
  3. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    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?
     
  4. EnsurdFrndship

    EnsurdFrndship

    Joined:
    Apr 17, 2010
    Posts:
    786
    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?...

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Writing : MonoBehaviour {
    6.  
    7.     public GameObject textObj;
    8.  
    9.     public string direction;
    10.     public float offset = 0.001f;
    11.  
    12.     public string message;
    13.     public string fontName = "Fonts/magnificent/Magnificent";
    14.     public string shaderName = "Shaders/TextOneSided";
    15.     public string matName = "Materials/Font Materials/FontMaterial";
    16.  
    17.     private GameObject sheetObj;
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.        
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.         GameObject obj = null;
    28.  
    29.         int cnt = 0;
    30.  
    31.         foreach (Transform child in transform)
    32.         {
    33.             cnt++;
    34.         }
    35.  
    36.         if (cnt == 0)
    37.         {
    38.             obj = new GameObject();
    39.             obj.transform.parent = transform;
    40.  
    41.             obj.AddComponent<MeshRenderer>();
    42.  
    43.             TextMesh objMesh = obj.AddComponent<TextMesh>();
    44.  
    45.             sheetObj = obj;
    46.         }
    47.  
    48.         if (direction == "Front")
    49.         {
    50.             if (obj != null)
    51.             {
    52.                 obj.transform.localPosition = new Vector3(0, 0, (obj.transform.localScale.z * 1.0f / 2) + offset);
    53.             }
    54.         }
    55.         if (direction == "Right")
    56.         {
    57.             if (obj != null)
    58.             {
    59.                 obj.transform.localPosition = new Vector3((obj.transform.localScale.x * 1.0f / 2) + offset, 0, 0);
    60.             }
    61.         }
    62.         if (direction == "Back")
    63.         {
    64.             if (obj != null)
    65.             {
    66.                 obj.transform.localPosition = new Vector3(0, 0, -(obj.transform.localScale.z * 1.0f / 2) - offset);
    67.             }
    68.         }
    69.         if (direction == "Left")
    70.         {
    71.             if (obj != null)
    72.             {
    73.                 obj.transform.localPosition = new Vector3(-(obj.transform.localScale.x * 1.0f / 2) - offset, 0, 0);
    74.             }
    75.         }
    76.         if (direction == "Up")
    77.         {
    78.             if (obj != null)
    79.             {
    80.                 obj.transform.localPosition = new Vector3(0, (obj.transform.localScale.y * 1.0f / 2) + offset, 0);
    81.             }
    82.         }
    83.         if (direction == "Down")
    84.         {
    85.             if (obj != null)
    86.             {
    87.                 obj.transform.localPosition = new Vector3(0, -(obj.transform.localScale.y * 1.0f / 2) - offset, 0);
    88.             }
    89.         }
    90.  
    91.         if (obj != null) {
    92.      
    93.             obj.transform.LookAt(transform.position);
    94.  
    95.             obj.transform.Translate(-new Vector3(0.5f, -0.5f, 0));
    96.  
    97.             obj.transform.localScale = new Vector3(1, 1, 0.0001f);
    98.  
    99.             Material mat = Resources.Load(matName) as Material;
    100.  
    101.             mat.shader = Resources.Load(shaderName) as Shader;
    102.  
    103.             Font font = Resources.Load(fontName) as Font;
    104.  
    105.             mat.SetTexture("_MainTex", textObj.GetComponent<MeshRenderer>().material.GetTexture("_MainTex"));
    106.  
    107.             obj.GetComponent<TextMesh>().font = font;
    108.  
    109.             obj.GetComponent<TextMesh>().text = message;
    110.  
    111.             obj.GetComponent<TextMesh>().characterSize = 0.1f;
    112.  
    113.             obj.GetComponent<MeshRenderer>().material = mat;
    114.  
    115.         }
    116.  
    117.         if (sheetObj != null)
    118.         {
    119.             sheetObj.GetComponent<TextMesh>().text = message;
    120.         }
    121.  
    122.     }
    123. }
    124.  
     
  5. Vollmondum

    Vollmondum

    Joined:
    Sep 19, 2012
    Posts:
    10
    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"
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,776
    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.
     
    Vollmondum likes this.
  7. Vollmondum

    Vollmondum

    Joined:
    Sep 19, 2012
    Posts:
    10
    I didn't notice the issue date :)