找回密码
 立即注册
查看: 4026|回复: 58

[脚本] unity3d 在android机器里读二进制文件里的数据

[复制链接]
发表于 2013-6-17 11:38 | 显示全部楼层 |阅读模式
本帖最后由 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);

    }   
        
}

身体不是自己的是向这个世界借来的。技术不是自己的是向这个世界借来用的,真心付出,不用保留。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
发表于 2013-6-18 08:37 | 显示全部楼层
此贴是应该放在教程区
 楼主| 发表于 2013-6-18 09:17 | 显示全部楼层
小米 发表于 2013-6-18 08:37
此贴是应该放在教程区


不错 不错 不错{:soso__3922851084632044791_6:}
发表于 2013-6-18 09:35 | 显示全部楼层
噢 学习了{:5_435:}
发表于 2017-4-1 09:36 | 显示全部楼层
顶顶多好
发表于 2017-4-1 09:59 | 显示全部楼层
真心顶
发表于 2017-4-1 09:52 | 显示全部楼层
说的非常好
发表于 2017-4-1 10:20 | 显示全部楼层
很好哦
发表于 2017-4-1 10:04 | 显示全部楼层
LZ真是人才
发表于 2017-5-10 16:09 | 显示全部楼层
很不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-5-3 07:43 , Processed in 0.108001 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表