Unity3D教程:制作彩色字体

1.首先随便扔进去一个字体,最简单的比如扔进去一个arial。

设置好Font Size,比如24。Character里一般来说不需要Unicode,除非你要把中文做成花。我选ASCII default set。如果只要大写或者小写,自己选。

2.建一个目录,取名叫Editor。然后创建一个Javascript,按回车(Mac)或者F2(Win)改名成SaveFontTexture,不用加.js。然后双击,贴进去下面代码:

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;
    }//Unity3D教程手册:www.unitymanual.com
 
    // 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", "Please 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[i]);
      b.Close();
    }
 
    DestroyImmediate(tex2);
}

3.这个时候,Unity的Assests菜单里多了一个选项,Save Font Texture。在Project Panel里找到刚才拽进去的字体,比如Arial,找到里面的font Texture,选中,然后在菜单里点“Save Font Texture”,会打开一个保存对话框,把这个图保存到你的项目的Assests目录里的随便哪个地方。

4.拿Photoshop打开刚才保存的这张图,如果你没改名应该就叫font Texture.png,你会看到一个字体材质图。这时候你就可以编辑这个图了。Photoshop不熟的人可能会发现这个图看不清,再它下面加个层填成黑色就容易编辑了。也可以在设置里面改透明背景网格的颜色。

5.改完之后按cmd+s或者ctrl+s,直接保存。切回Unity。建立一个Shader,随便取个名,把下面的内容贴进去。

Shader "GUI/Textured Text Shader"
 {
    Properties {
       _MainTex ("Font Texture", 2D) = "white" {}
       _Color ("Text Color", Color) = (1,1,1,1)
    }
 
    SubShader {
       Lighting Off
       cull off
       ztest always
       Zwrite off
       Fog { Mode Off }
       Tags {"Queue" = "Transparent" }
       Pass {  //Unity3D教程手册:www.unitymanual.com
          Blend SrcAlpha OneMinusSrcAlpha
          SetTexture [_MainTex] {
             constantColor [_Color]
             Combine texture * constant, texture * constant
          }
       }
    }

6 新建一个Material,随便取个名,比如我取名叫Font Mat。在Shader里选GUI->Textured Text Shader。这个shader是你刚才建的那个shader。 FontTexture选你改过的那个字体的图。Text Color不用管,白色就行。

7 扔进去一个GUI Text (菜单的GameObject->Create Other->GUI Text,Font选你扔进去的字体,比如arial,Material选刚才建的材质,比如我的就是Font Mat。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小小姑娘很大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值