Unity中如何从jpg或png文件创建Texture2D
来源:互联网 发布:网络mg老虎机玩法 编辑:程序博客网 时间:2024/05/23 13:30
使用Texture2D的LoadImage方法即可实现从jpg或png文件创建Texture2D。经验证,当发布的目标平台为iOS时,此方法也是有效的。
function LoadImage (data : byte[]) : boolean
Description
Loads an image from a byte array.
This function loads a JPG or PNG image from raw byte[] array.
using UnityEngine;using System.Collections;public class example : MonoBehaviour { public TextAsset imageTextAsset; void Start() { Texture2D tex = new Texture2D(4, 4); tex.LoadImage(imageTextAsset.bytes); renderer.material.mainTexture = tex; }}
This function replaces texture contents with new image data. After LoadImage, texture size and format might change. JPG files are loaded into RGB24 format, PNG files are loaded into ARGB32 format. If texture format before calling LoadImage is DXT1 or DXT5, then the loaded image will be DXT-compressed (into DXT1 for JPG images and DXT5 for PNG images).
例子:
//imagePath是jpg或png图片的路径
FileStream fs = new System.IO.FileStream(imagePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] thebytes =new byte[fs.Length];
fs.Read(thebytes,0, (int)fs.Length);
//实例化一个Texture2D,宽和高设置可以是任意的,因为当使用LoadImage方法会对Texture2D的宽和高会做相应的调整
Texture2D texture = new Texture2D(1,1);
texture.LoadImage(thebytes);
material.mainTexture = texture;
material.mainTextureScale =new Vector2(1,1);
material.mainTextureOffset =new Vector2(0,0);
- Unity中如何从jpg或png文件创建Texture2D
- Unity 中 Png转Texture2D再转Sprite
- NSImage 存储为jpg或png文件的方法
- 从文件中恢复jpg
- webp转png或jpg
- [Unity 笔记] Unity Editor中如何更改Texture2D对应资源的压缩格式,并保存
- 从资源中加载jpg, png到GDI+ Image
- 从资源中加载jpg, png到GDI+ Image
- 将UIImage对象保存到JPG或者PNG文件中
- flex 保存控件为jpg或png
- Pdf转成一张png或jpg图片
- ios 判断图片是JPG或PNG
- cocos2d-x教程2:在windows下如何批量转换pvr,ccz为png或jpg
- java 将jpg、bmp、png、gif图片文件,进行等比或非等比的大小转换
- Unity Shaders and Effects Cookbook (2-6) 在 C# 代码中创建 Texture2D 并赋值给 Shader
- 怎样从ipa中提取PNG文件
- VC.NET从资源中加载图片(jpg,png)并显示
- 从资源中加载jpg, png到GDI+ Image, bmp InvalidParameter问题
- ubuntu下固定IP地址配置方法
- 分享10 个你可能没用过的 Linux 命令,有些比较简化常用操作,比较有用。
- FormatMessage 格式化 GetLastError 获取的错误代码
- 2011年回顾
- DB2中SQLSTATE=57016 SQLCODE=-668,原因码 "7"的解决方法
- Unity中如何从jpg或png文件创建Texture2D
- 第二部 注册Msjetoledb40.dll
- POJ 2176 Folding (字符串)
- vs2005位图插入
- opentftp server 使用指南
- 图的重构:Subway Tree System
- 数据库总结
- GDTR LDTR
- 第三部 错误真凶——自己