首页 分享 Unity3d 制作彩色字

Unity3d 制作彩色字

来源:花匠小妙招 时间:2024-11-22 20:07

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。

完事儿。

用这种方法,随便做什么阴影字发光字都可以。本来今天想写个脚本来处理,后来找了半天官方论坛,总算找到个解决方法。 

相关知识

彩色拼贴剪纸的制作过程 ——《福娃》彩色拼贴剪纸制作
物联网 + Unity3D虚拟现实花卉养护远程智能监控系统
无光照可调节透明通道Shader(unity3d)
儿童手工制作:彩色花篮
diy 制作手工纸花? DIY手工制作玫瑰花,彩色花朵、纸花、海绵纸花?
益智创意手工,用彩色环保纸制作立体花朵
如何用ps制作鲜花文字 Photoshop制作花朵字教程
五色草造型组字花坛如何制作
幼儿园彩色手工花制作方法
基于Unity 3D果树交互虚拟修剪技术及其实现

网址: Unity3d 制作彩色字 https://www.huajiangbk.com/newsview659261.html

所属分类:花卉
上一篇: 空心字体转换器在线转换
下一篇: 可爱字体转换器在线转换

推荐分享