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;
    }

    // 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不熟的人可能会发现这个图看不清,再它下面加个层填成黑色就容易编辑了。也可以在设置里面改透明背景网格的颜色。

这个图是我随便改的。
[attachment=0:33oqz9vk]2.jpeg[/attachment:33oqz9vk]
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 {
          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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值