Unity3d 製作彩色字

來源:互聯網
上載者:User

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。

完事兒。

用這種方法,隨便做什麼陰影字發光字都可以。本來今天想寫個指令碼來處理,後來找了半天官方論壇,總算找到個解決方案。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.