EditorUtility.DisplayDialog("No texture selected", "lease select a texture", "Cancel");
return;
}
if (tex.format != TextureFormat.Alpha8) {
EditorUtility.DisplayDialog("Wrong format", "Texture must be in uncompressed Alpha8 format", "Cancel");
return;
}
// Convert Alpha8 texture to ARGB32 texture so it can be saved as a PNG
var texPixels = tex.GetPixels();
var tex2 = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
tex2.SetPixels(texPixels);
// Save texture
var texBytes = tex2.EncodeToPNG();
var fileName = EditorUtility.SaveFilePanel("Save font texture", "", "font Texture", "png");
if (fileName.Length > 0) {
var f : FileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
var b : BinaryWriter = new BinaryWriter(f);
for (var i = 0; i < texBytes.Length; i++) b.Write(texBytes);
b.Close();
}
DestroyImmediate(tex2);
}
保存,关掉编辑器。
另外如果你用iPhone版,用这个脚本:
import System.IO;
@MenuItem ("Assets/Save Font Texture")
static function SaveFontTexture () {
var tex = Selection.activeObject as Texture2D;
if (tex == null) {
EditorUtility.DisplayDialog("No texture selected", "lease select a texture", "Cancel");
return;
}
if (tex.format != TextureFormat.Alpha8) {
EditorUtility.DisplayDialog("Wrong format", "Texture must be in uncompressed Alpha8 format", "Cancel");
return;
}
// Convert Alpha8 texture to ARGB32 texture so it can be saved as a PNG
var texPixels = tex.GetPixels();
var tex2 = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
tex2.SetPixels(texPixels);
// Save texture
var texBytes = tex2.EncodeToPNG();
var fileName = EditorUtility.SaveFilePanel("Save font texture", "", "font Texture", "png");
if (fileName.Length > 0) {
var f : FileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write);
var b : BinaryWriter = new BinaryWriter(f);
for (var i = 0; i < texBytes.Length; i++) b.Write(texBytes);
b.Close();
}
DestroyImmediate(tex2);
}
3 这个时候,Unity的Assests菜单里多了一个选项,Save Font Texture。在Project Panel里找到刚才拽进去的字体,比如Arial,找到里面的font Texture,选中,然后在菜单里点“Save Font Texture”,会打开一个保存对话框,把这个图保存到你的项目的Assests目录里的随便哪个地方。