本帖最后由 zxhhst 于 2013-6-17 11:41 编辑
各位我也查了3天。
第一步: 听雨松的:
假设你的程序中已经预先将2进制文件做好,如下图所示,你需要把二进制文件放在StreamingAssets这个文件夹中,一定要放在这里。 举个例子,放在StreamingAssets中二进制文件打包后,Unity会将这些二进制文件放置在对应平台下的路径下。所以根据不同平台,访问的路径是不一样的。切记,你的二进制文件一定要放在StreamingAssets !!!!!!
第二步下面程序:
using UnityEngine;
using System.Collections;
using System.IO;
using System.Collections.Generic;
using System;
public class TexturePoint : MonoBehaviour {
String result;
String filePath;
byte[] myAllByte1=new byte[60000];
byte[] myAllByte2=new byte[60000];
byte[] myByte1;
byte[] myByte2;
Texture2D texture=null;
String path=null;
BufferedStream myBS=null;
byte[] myByte;
byte[] myFileHead;
int bmpFileLen;
int bmpWidth;
int bmpHeight;
int x=0;
int y=0;
IEnumerator Start ()
{
filePath= System.IO.Path.Combine(Application.streamingAssetsPath, "0.bmp");
myAllByte2[0]=0;
myAllByte1[0]=0;
WWW www = new WWW(filePath);
yield return www;
myAllByte2 = www.bytes;
//myAllByte1 = System.IO.File.ReadAllBytes(filePath);
/*
myFileHead=new byte[0x46];
for(int i = 0 ; i<0x46;i++)
{
myFileHead = myAllByte1;
}
//读位图的数据
bmpWidth = myFileHead[0x15]*0x1000000+myFileHead[0x14]*0x10000+myFileHead[0x13]*0x100+myFileHead[0x12];
bmpHeight = myFileHead[0x19]*0x1000000+myFileHead[0x18]*0x10000+myFileHead[0x17]*0x100+myFileHead[0x16];
bmpFileLen = myFileHead[0x25]*0x1000000+myFileHead[0x24]*0x10000+myFileHead[0x23]*0x100+myFileHead[0x22];
myByte=new byte[bmpFileLen];
for(int i = 0;i<bmpFileLen;i++)
{
myByte= myAllByte1[0x46+i];
}
texture = new Texture2D(132, 182);
Color tmColor = new Color(0,0,0,0);
for ( x = 0; x <132; x++) {
for ( y = 0; y < 182; y++){
//if(myByte[x*2+y*2*bmpWidth]!=0)
//{
texture.SetPixel(x,y,Color.red);
//}
//else
//{
// texture.SetPixel(x,y,tmColor);
//}
}
}
texture.Apply();
*/
}
/**
*
*
*/
IEnumerator Example()
{
if (filePath.Contains("://"))
{
WWW www = new WWW(filePath);
yield return www;
myAllByte2 = www.bytes;
filePath +="00";
} else
{
//result = System.IO.File.ReadAllText(filePath);
myAllByte1 = System.IO.File.ReadAllBytes(filePath);
filePath +="01";
}
}
void OnGUI()
{
//读取文件中的所有内容
//GUI.DrawTexture(new Rect(0,0,400,400),"1111"+a);
//GUI.DrawTexture(new Rect(0,0,400,400),texture);
byte a = myAllByte2[0];
GUILayout.Button(a.ToString("x"));
byte b = myAllByte2[1];
GUILayout.Button(b.ToString("x"));
a = myAllByte2[2];
GUILayout.Button(a.ToString("x"));
b = myAllByte2[3];
GUILayout.Button(b.ToString("x"));
//if(myAllByte2[0]!=0)
if(myAllByte2[0]!=0)
GUILayout.Button(filePath);
}
}
身体不是自己的是向这个世界借来的。技术不是自己的是向这个世界借来用的,真心付出,不用保留。
|