12 第1页 | 共2 页下一页
返回列表 发新帖
查看: 3181|回复: 19
打印 上一主题 下一主题

【转】显示图片字符的脚本

[复制链接]

1023

主题

3

听众

359

积分

设计实习生

Rank: 2

纳金币
335582
精华
0

最佳新人

跳转到指定楼层
楼主
发表于 2011-7-26 08:07:59 |只看该作者 |倒序浏览
using UnityEngine;

namespace user

{//by gotoxy

class userFont

{

/*

LoadResources.FontTexture[]:为字体的纹理图片,格式为m*n个,每个字符大小相同,从空格(0x20)开始

使用的是Graphics.DrawTexture(),所以要在OnGUI()中调用

*/

private static int font_width = 16;

private static int font_height = 16;

private static int font_space_width = 4;//字符边沿空隙

private static int font_space_height = 2;//字符边沿空隙

private static int fontid = 0;

private static float zoom = 1.0f;

private static Color color = new Color(255, 0, 255);

//---------------------------------------------------------------------------------------------------------------

public static

void choosefont(int f)

{

if (f >= LoadResources.FontTexture.Length) return;

fontid = f;

if(fontid==0)

{

font_width = 16;

font_height = 16;

font_space_width = 4;

font_space_height = 2;

}

else if(fontid==1)

{

font_width = 16;

font_height = 16;

font_space_width = 4;

font_space_height = 2;

}

else

{

fontid = 0;

font_width = 16;

font_height = 16;

font_space_width = 4;

font_space_height = 2;

}

}

public static

void setcolor(Color usercolor)

{

color = usercolor;

}

public static

Color getcolor()

{

return color;

}

public static

void setzoom(float z)

{

zoom = z;

}

public static

void setsize(int w, int h)

{

font_width = w;

font_height = h;

}

public static

void setsize(float fw, float fh)

{

font_width = (int)((float)font_width * fw);

font_height = (int)((float)font_height * fh);

}

//---------------------------------------------------------------------------------------------------------------

public static

void user_puts(int x, int y, string str)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;



FONT_W = (int)((float)FONT_W * zoom);

FONT_H = (int)((float)FONT_H * zoom);

char[] buff = new char[str.Length];

buff = str.ToCharArray();

for (i = 0; i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;//从空格开始

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, color);

x += FONT_W;

}

}

public static

void user_puts(int x, int y, string str, float userzoom)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;

FONT_W = (int)((float)FONT_W * userzoom);

FONT_H = (int)((float)FONT_H * userzoom);

char[] buff = new char[str.Length];

buff = str.ToCharArray();

for (i = 0; i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, color);

x += FONT_W;

}

}

public static

void user_puts(int x, int y, string str, float userzoom, Color usercolor)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;

FONT_W = (int)((float)FONT_W * userzoom);

FONT_H = (int)((float)FONT_H * userzoom);

char[] buff = new char[str.Length];

buff = str.ToCharArray();

for (i = 0; i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, usercolor);

x += FONT_W;

}

}

//---------------------------------------------------------------------------------------------------------------

public static

void user_puts(int x, int y, byte[] buff, int len)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;

FONT_W = (int)((float)FONT_W * zoom);

FONT_H = (int)((float)FONT_H * zoom);

for (i = 0; i < len && i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, color);

x += FONT_W;

}

}

public static

void user_puts(int x, int y, byte[] buff, int len, float userzoom)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;

FONT_W = (int)((float)FONT_W * userzoom);

FONT_H = (int)((float)FONT_H * userzoom);

for (i = 0; i < len && i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, color);

x += FONT_W;

}

}

public static

void user_puts(int x, int y, byte[] buff, int len, float userzoom, Color usercolor)

{

int FONT_W = font_width;

int FONT_H = font_height;

if (FONT_W == 0 || FONT_H == 0) return;

float font_base_w = (float)FONT_W / LoadResources.FontTexture[fontid].width; //fontWidth/LoadResources.FontTexture[fontid].width;

float font_base_h = (float)FONT_H / LoadResources.FontTexture[fontid].height; //fontHeight/LoadResources.FontTexture[fontid].height;

float Space_X = (float)font_space_width / LoadResources.FontTexture[fontid].width;//字符边沿空隙

float Space_Y = (float)font_space_height / LoadResources.FontTexture[fontid].height;

float tx, ty;

int i, a;

int XTile = LoadResources.FontTexture[fontid].width / FONT_W;

int YTile = LoadResources.FontTexture[fontid].height / FONT_H;

FONT_W = (int)((float)FONT_W * userzoom);

FONT_H = (int)((float)FONT_H * userzoom);

for (i = 0; i < len && i < buff.Length; i++)

{

if (buff >= 0x20) a = (int)(buff & 0xff) - 0x20;

else a = 0;

tx = (float)(a % XTile) * font_base_w;

ty = (float)(YTile - 1 - (a / XTile)) * font_base_h;

Graphics.DrawTexture(new Rect(x, y, FONT_W, FONT_H), LoadResources.FontTexture[fontid], new Rect(tx + Space_X, ty + Space_Y, font_base_w - 2 * Space_X, font_base_h - 2 * Space_Y), 0, 0, 0, 0, usercolor);

x += FONT_W;

}

}

}

}
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

797

主题

1

听众

1万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
5568
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

沙发
发表于 2011-8-2 21:23:34 |只看该作者
在学习
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

板凳
发表于 2011-8-20 11:14:27 |只看该作者
密密麻麻的,有占难 啊
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

地板
发表于 2012-2-2 23:28:59 |只看该作者
人过留名!
回复

使用道具 举报

462

主题

1

听众

31万

积分

首席设计师

Rank: 8Rank: 8

纳金币
2
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

5#
发表于 2012-4-11 23:25:16 |只看该作者
真不错,全存下来了.
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

6#
发表于 2012-5-2 23:24:41 |只看该作者
这么后现代
回复

使用道具 举报

tc    

5089

主题

1

听众

33万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

7#
发表于 2012-5-3 23:26:27 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

   

671

主题

1

听众

3247

积分

中级设计师

Rank: 5Rank: 5

纳金币
324742
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

8#
发表于 2012-9-30 23:21:40 |只看该作者
你们都躲开,我来顶
回复

使用道具 举报

5969

主题

1

听众

39万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
0

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

9#
发表于 2012-10-20 23:26:28 |只看该作者
读铁系缘分,顶铁系友情
回复

使用道具 举报

2317

主题

54

听众

2万

积分

资深设计师

Rank: 7Rank: 7Rank: 7

纳金币
20645
精华
62

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

10#
发表于 2012-10-21 00:41:45 |只看该作者
我来顶下,支持楼主的技术交流
回复

使用道具 举报

12 第1页 | 共2 页下一页
返回列表 发新帖
您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-6-16 15:55 , Processed in 0.089107 second(s), 28 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部