public static Texture TextToTexture(string text, string alphabetTextureName) { string[] guids = AssetDatabase.FindAssets(alphabetTextureName + " t:texture"); if (guids.Length > 0) { string path = null; foreach (string g in guids) { string p = AssetDatabase.GUIDToAssetPath(g); if (p.Contains("/" + alphabetTextureName + ".")) { path = p; } } if (path == null) { Debug.LogWarning("Alphabet texture could not be found."); return(null); } TextureImporter importer = (TextureImporter)TextureImporter.GetAtPath(path); importer.isReadable = true; importer.SaveAndReimport(); Texture2D alphabet_texture = AssetDatabase.LoadAssetAtPath <Texture2D>(path); Color background = alphabet_texture.GetPixel(0, 0); //load letter data from alphabet int padding = 0; int[][] letterSpaces = new int[62][]; int currentLetterIndex = -1; bool wasLetter = false; int pixelLetterFreshhold = 3; for (int x = 0; x < alphabet_texture.width; x++) { int pixelIsLetter = 0; for (int y = 0; y < alphabet_texture.height; y++) { if (Helper.ColorDifference(background, alphabet_texture.GetPixel(x, y)) > 0.01) { pixelIsLetter += 1; } } bool isLetter = pixelIsLetter > pixelLetterFreshhold; if (isLetter && !wasLetter) { currentLetterIndex += 1; if (currentLetterIndex >= letterSpaces.Length) { break; } letterSpaces[currentLetterIndex] = new int[2]; letterSpaces[currentLetterIndex][0] = x; } else if (isLetter && wasLetter) { letterSpaces[currentLetterIndex][1] += 1; } else if (!isLetter) { padding += 1; } wasLetter = isLetter; } int spaceWidth = 0; foreach (int[] s in letterSpaces) { if (s != null) { spaceWidth += s[1]; } } spaceWidth /= 62; padding /= 61; int a = 97; int A = 65; int zero = 48; int space = 32; int totalWidth = 0; int[] letterIndecies = new int[text.Length]; // text to alphabet texture letter indecies int i = 0; foreach (char c in text) { if ((int)c == 10) { continue; } int letterIndex = 0; if ((int)c - a >= 0) { letterIndex = ((int)c - a) * 2 + 1; } else if ((int)c - A >= 0) { letterIndex = ((int)c - A) * 2; } else if ((int)c - zero >= 0) { letterIndex = 52 + (int)c - zero; } else if ((int)c - space >= 0) { letterIndex = -1; } //Debug.Log("Char: " + c +","+ (int)c+","+letterIndex); letterIndecies[i] = letterIndex; if (letterIndex == -1) { totalWidth += spaceWidth; } else { totalWidth += letterSpaces[letterIndex][1] + padding; } i++; } Texture2D text_texture = new Texture2D(totalWidth, alphabet_texture.height); Debug.Log("Padding: " + padding); Debug.Log("Total width: " + totalWidth); // indecies to texture int letterX = 0; for (i = 0; i < letterIndecies.Length; i++) { //Debug.Log(i + "," + letterIndecies[i]); if (letterIndecies[i] == -1) { for (int x = 0; x < spaceWidth; x++) { for (int y = 0; y < text_texture.height; y++) { text_texture.SetPixel(letterX + x, y, background); } } letterX += spaceWidth; } else { for (int x = 0; x < letterSpaces[letterIndecies[i]][1] + padding; x++) { for (int y = 0; y < text_texture.height; y++) { text_texture.SetPixel(letterX + x, y, alphabet_texture.GetPixel(letterSpaces[letterIndecies[i]][0] + x - padding / 2, y)); } } letterX += letterSpaces[letterIndecies[i]][1] + padding; } } string file_name = "text_" + Regex.Replace(text, @"\s", "_"); string save_path = "Assets/Textures/Text/" + file_name + ".png"; return(Helper.SaveTextureAsPNG(text_texture, save_path, null)); } else { Debug.LogWarning("Alphabet texture could not be found."); } return(null); }