Unity3D Unity 里不使用字库 显示任意中文字体
我在web的形式发布 unity 程序的时候,要显示中文字体不能要把很大的字库打进去,而我的程序不需要有中文的输入功能,能显示中文就可以了,而且字体要很漂亮。
Unity3D Unity 里不使用字库 显示任意中文字体
方法目前只有一个(等 Unity 支持客户端字体的时候这个问题就解决了),就是自动生成 Texture 然后在 unity 里显示。Unity3D Unity 里不使用字库 显示任意中文字体
先写一个 测试的脚本 – Unity3D Unity 里不使用字库 显示任意中文字体
using UnityEngine;
using System.Collections;
public class Class2:MonoBehaviour
{
    public string Url = "http://127.0.0.1/test.php?n=%C4%E3%BA%C3";
    void Start()
    {
        StartCoroutine(_LoadTexture(Url, gameObject));
    }
    private IEnumerator _LoadTexture(string url, GameObject obj)
    {
        WWW rerquest = new WWW(url);
        yield return rerquest;
        try
        {
            if (rerquest.isDone && string.IsNullOrEmpty(rerquest.error))
            {
                obj.renderer.material = new Material(Shader.Find("Transparent/Cutout/Diffuse"));
                obj.renderer.material.mainTexture = rerquest.texture;
            }
        }
        catch (UnityException e) { }
        finally
        {
            rerquest.Dispose();
        }
    }
}
test.php 的代码
<?php
header('Content-type: image/png;');
$name= request("n", "default");
$font = "STLITI.TTF";//注意这里,这里我把 C:\WINDOWS\Fonts 下的 STLITI.TTF 拷到 test.php 同目录下,可以使用 C:\WINDOWS\Fonts 下任意 *.TTF 或者 *.TTC 的字体
$name=translateCharset($name, "gb2312");
$im = imagecreate(1, 1);
$black = imagecolorallocate($im, 243, 169, 91);
$fcolor = imagecolorallocate($im, 243, 169, 90);
$para = imagettftext($im, 100, 0, 0, 112, $fcolor, $font, $name);
$im = imagecreate($para[2], $para[1]);
$black = imagecolorallocate($im, 243, 169, 91);
$fcolor = imagecolorallocate($im, 243, 169, 90);
imagettftext($im, 100, 0, 0, 112, $fcolor, $font, $name);
imagecolortransparent($im, $black);
imagepng($im);
imagedestroy($im);
function translateCharset($string, $fromCharset, $toCharset='UTF-8′) {
    if(function_exists('mb_convert_encoding')) 
    {
        return mb_convert_encoding($string, $toCharset, $fromCharset);
    } else if(function_exists('iconv')) { // iconv is flakey
        return iconv($fromCharset, $toCharset, $string);
    } else {
        return $string;
    } // end else clause
}
function request($var_name, $default = null)
{
    global $_POST, $_GET;
    if ( isset( $_POST[$var_name] ) ) {
        $result = add_slashes( $_POST[$var_name] );
    }
    else if ( isset( $_GET[$var_name] ) ) {
        $result = add_slashes( $_GET[$var_name] );
    }
    else if ( func_num_args() > 1 ) { #check for a default passed in (allowing null)
        $result = add_slashes($default);
    }
    else {
        $result = null;
    }
    if($result == " || $result == null){
        $result = $default;
    }
    return $result;
}
function add_slashes( $var )
{
    if ( 1 == get_magic_quotes_gpc() ) {
        return $var;
    }
    else if ( !is_array( $var ) ){
        return addslashes( $var );
    }
    else {
        foreach ( $var as $key => $value ) {
            $var[$key] = add_slashes( $value );
        }
        return $var;
    }
}
?>
我这个 生成png 的图片用的是 php 的 gd2 的库,因为服务器可能放到Linux上,服务器是 windows 的同学可以用 windows 的 GDI+ 去画 png 图片。Unity3D Unity 里不使用字库 显示任意中文字体
最后说说 php 中的参数问题 在 http://127.0.0.1/test.php?n=%C4%E3%BA%C3 中,n= 后面的是要显示的字符,%C4%E3%BA%C3 是"你好"的UTF8编码。Unity3D Unity 里不使用字库 显示任意中文字体 如何得到中文的UTF8编码? 有2个方法
- 可以使用javascript 的 encodeURIComponent
 - 打开 firefox,在地址栏输入 http://127.0.0.1/test.php?n=任意中文字符,敲回车,得到 – Unity3D Unity 里不使用字库 显示任意中文字体