Unity of the dynamic font fonts beyond the size of the font image fonts led to screen __ fonts

Source: Internet
Author: User

Reproduced from: http://www.cnblogs.com/bicker/p/3669176.html

In our game development process, in part of the mobile phone running games, there are problems with the loss of fonts, the problem of the phone seems to be used are Qualcomm chips.

The unity of use is version 4.2.0, Ngui is the 3.4.9 version.

In unity of the Forum and unity answer to find the cause of the problem and solutions for a long time, and later in the CSDN to find a blog post, which describes the reasons for the problem and the corresponding solutions. According to the method in the article, I proved that the problem can be solved after the experiment.

Blog Address: http://blog.csdn.net/langresser_king/article/details/22095235

As you can see in Unity Editor, the font used in TTF files will generate a texture file and material file. When you need to display the text, the Requestcharactersintexture function is used to update the text information to the font request, and then use Getcharacterinfo to get the text information to render it. When calling Getcharacterinfo, make sure that all text has been requested through the requestcharactersintexture.

If the request, the font internal maintenance texture is not enough, will trigger Texturerebuildcallback callback, notify the external use of the object of font, its internal texture was updated, external should be refreshed.

It is mentioned in the original, because the size of the texture is not enlarged in time to cause the loss of fonts. But after many tests, the reason for guessing the problem should not be this. Because there is no problem with the loss of fonts on the PC or on other phones, the problem does not arise in the unity dynamic font strategy. But this problem can be avoided by using the method at the very beginning to texture size enlargement. The real reason has not been found.

The following provides a way to circumvent this problem for reference:

Using Unityengine;
Using System.Collections;

public class Expanddynamicfonttexture:monobehaviour 
{public
	unityengine.font Basefont;
	private static string chinesetext = null;
	private static int flag = 1;
	void Start ()
	{
		if (flag!= 1) 
		{return
			;
		}
		flag++;
		Expandfonttexture ();
	}
	public void Expandfonttexture ()
	{
		if (Chinesetext = = null) 
		{
			Textasset text = Resources.load (" Fonts/configtext ") as Textasset;
			Chinesetext = text. ToString ();
		}

		Basefont.requestcharactersintexture (Chinesetext, fontstyle.normal);		baseFont.material.mainTexture.height = 1024;		baseFont.material.mainTexture.width  = 1024;

		Texture texture = baseFont.material.mainTexture;
		Debug.Log (String. Format ("Texture:{0},{1}", Texture.width,texture.height));
	}

This can be called at the beginning of the game, passing the Requestcharactersintexture function, passing in a larger string to make unity expand the size of the texture.

Also, if the text used in the game is added through the configuration table, it comes with a function to count all the text used in the game and to generate the text file that the previous function needs.

Using Unityengine;
Using Unityeditor;
Using System.IO;
Using System.Text;
Using System.Linq;
Using System.Collections;

Using System.Collections.Generic; 
		public class Collectwords:monobehaviour {[MenuItem (' custom/statistic Words ')] public static void Statisticwords () {
		Used for statistical character Dictionary<char, int> wordmap = new Dictionary<char, int> ();
		string[] Txtfiles = Directory.GetFiles ("assets/resources/localization/", "*.txt");
			for (int i = 0; i < txtfiles.length ++i) {Debug.Log ("> Open file: + txtfiles[i]);"
			String textcontent = File.readalltext (Txtfiles[i], System.Text.Encoding.UTF8);
			Debug.Log ("> file" + txtfiles[i] + "size:" + textcontent.length);
				for (int wordindex = 0; Wordindex < textcontent.length; ++wordindex) {int usecount = 0;
				Wordmap.trygetvalue (Textcontent[wordindex], out usecount);
			Wordmap[textcontent[wordindex]] = usecount + 1; } Dictionary<char, int> sortedwordmap = new Dictionary<chaR, Int> (); Sortedwordmap = (from entry in Wordmap by entry. Value Descending Select entry). ToDictionary (pair => pair. Key, pair => pair.
		Value);
		IEnumerator enumerator = SortedWordMap.Keys.GetEnumerator ();
		
		StringBuilder Sbuilder = new StringBuilder (); while (enumerator. MoveNext ()) {Sbuilder.append (string). Format ("{0}:\t{1}\n", enumerator.) Current, sortedwordmap[(char) enumerator.
		Current])); } file.writealltext ("Assets/resources/localization/sortedchinesewords.txt", sbuilder.tostring (), ENC Oding.
		UTF8);
		Sbuilder = new StringBuilder ();
		Enumerator = SortedWordMap.Keys.GetEnumerator ();
		int wordCount = 0; while (enumerator.
			MoveNext () &&wordcount<800) {wordcount++; Sbuilder.append (Enumerator.
		Current); } Debug.Log ("> Total word:" + sbuilder.tostring ().
		Length); File.writealltext ("Assets/resources/localization/chinesewords.txt", Sbuilder.tosTring (), Encoding.UTF8); }
}

If you know unity is the real reason for this missing font, please tell us.

PS: The above from the resources directory to obtain text files and font files, with our own implementation of the Resmngr, can be replaced by their own unity of the original method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.