找回密码
 立即注册
楼主: 小米

[脚本] 解决 unity 生成 android apk read Resources

[复制链接]
发表于 2013-5-20 10:30 | 显示全部楼层 |阅读模式
本帖最后由 小米 于 2013-5-20 10:52 编辑

  1. TextAsset t = (TextAsset)Resources.Load("skill2");
  2.         XmlDocument xmlDoc = new XmlDocument();
  3.         xmlDoc.LoadXml(t.text.ToString().Trim());
  4.          [/backcolor][/align][align=left][backcolor=#ffffff]        XmlElement n = (XmlElement)xmlDoc.SelectSingleNode("/datas/data[@skillID='1002']");  
  5.         if (n != null)
  6.         {
  7.             print("+++++++++++++++++++++++++++++++++++++++++++++++++");
  8.             data += n.GetAttribute("skillID");
  9.         }
复制代码

解决 unity 生成 android apk read Resources
试了很多方法都找不到 unity 的目录,,最后没有办法了,放在 Resources 目录里。。。
发觉只能读不能写,,汗,,,白费了,又重新找。。。。
续:

找到了 android 工程模板了。。。。
Unity\Editor\Data\PlaybackEngines\androidplayer\
就像 web 模板一样
把它复制出来,导入eclipse
好了,只能拼 android 基础了。。。。
做好放在

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
解决了,,,下面说下步骤
test.txt 路径如图

  1. [/align][/backcolor][backcolor=#f5f5f5]void readFile(string file)
  2.     {
  3.         path = "jar:file://" + Application.dataPath + "!/assets/" + file;[/backcolor]
  4. [backcolor=#f5f5f5]        if (File.Exists(path))
  5.         {
  6.             stringToEdit = "yes";
  7.         }
  8.         else
  9.         {
  10.             stringToEdit = "no";[/backcolor]
  11. [backcolor=#f5f5f5]            WWW www = new WWW(path);
  12.             while (!www.isDone) { }[/backcolor]
  13. [backcolor=#f5f5f5]            stringToEdit += " " + [url=http://www.text]www.text[/url];[/backcolor]
  14. [backcolor=#f5f5f5]        }
  15.     }
复制代码


不知道为什么 File.Exists(path)会检测不到文件,,,不过能读取就可以了
说下
Application.persistentDataPath 是指向 android /data/data/xxx.xxx.xxx/files/Application.dataPath 是指向 /data/app/xxx.xxx.xxx.apk
汗:http://game.ceeger.com/Manual/StreamingAssets.html 这是unity圣典翻译好了,写得很详细了,,,
难怪会检测不到文件..现在还在解决解包问题
然后复制到 Application.persistentDataPath


/////////////////////////////////////最终方式/////////////////////////////////////////


using UnityEngine;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Core;
using System;
public class Test : MonoBehaviour
{
    string path = "";
    string zip_file = "d:\\t.zip";
    void Start()
    {
        path = "jar:file://" + Application.dataPath + "!/assets/";
        //readFile("test.txt");
       // File.WriteAllText(Application.persistentDataPath + "/xx2.txt", "xxxxxxxxxxxxxxxxxxxx");
      
       UnZipFile(Application.dataPath, "assets");
      
    }
    #region 解压资料处理fn
   
    //判断是否已经解压
    bool checkDecompressingFiles(string flag_file)
    {
        if (!File.Exists(Application.persistentDataPath + "/" + flag_file))
        {
            File.WriteAllText(Application.persistentDataPath + "/" + flag_file, "a");
            return false;
        }
        return true;
    }
/*
* 没有测试过
ICSharpCode.SharpZipLib.Zip.FastZip zip = new ICSharpCode.SharpZipLib.Zip.FastZip();
zip.CreateZip("C:\\foo.zip", "C:\\待压缩的文件夹", true, ""); //第三个参数表示是否压缩子文件夹
zip.ExtractZip("C:\\foo.zip", "C:\\压缩后存放的文件夹", "");
如果我们只想压缩目录中的一个或部分文件,或者我们只想解压文件中的一个或部分文件,怎么办呢?那就得把最后一个参数用上,它是字符串类型的正则表达式,比如只压缩 ini 文件用:"^.*(.ini)$"。
*/
    //用第三方工具去读取 zip
    void UnZipFile(string zipFilePath,string dir)
    {
        if (checkDecompressingFiles("init.txt")) {
            stringToEdit += " 已经解压过了";
            return;
        }else
            stringToEdit += " 第一次解压";
        using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))
        {         
            ZipEntry theEntry;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);
               // print("directoryName:" + directoryName);
               // print("fileName:" + fileName);
                if (directoryName.Length > 0 && directoryName.Contains(dir))
                {
                    //stringToEdit += " directoryName:" + directoryName+"\n";
                    Directory.CreateDirectory(Application.persistentDataPath + "/" + directoryName);
                    if (fileName != string.Empty)
                    {
                        using (FileStream streamWriter = File.Create(Application.persistentDataPath + "/" + theEntry.Name))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)                                
                                    streamWriter.Write(data, 0, size);                                
                                else                                
                                    break;                                
                            }
                        }
                    }
                }
               
               
            }
        }
    }
    //没有测试过下面
    void CreateZipFile(string filesPath, string zipFilePath)
    {
        if (!Directory.Exists(filesPath))
        {
            
            return;
        }
        try
        {
            string[] filenames = Directory.GetFiles(filesPath);
            using (ZipOutputStream s = new ZipOutputStream(File.Create(zipFilePath)))
            {
                s.SetLevel(9); // 压缩级别 0-9
                //s.Password = "123"; //Zip压缩文件密码
                byte[] buffer = new byte[4096]; //缓冲区大小
                foreach (string file in filenames)
                {
                    ZipEntry entry = new ZipEntry(Path.GetFileName(file));
                    //entry.DateTime = DateTime.Now;
                    s.PutNextEntry(entry);
                    using (FileStream fs = File.OpenRead(file))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            s.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
                s.Finish();
                s.Close();
            }
        }
        catch (Exception ex)
        {
           
        }
    }
    //直接用 unty www 去读取 jar 跟 zip 标准差不多吧
    void readFile(string file)
    {
        path+= file;
        if (File.Exists(path))
        {
            stringToEdit = "yes";
        }
        else
        {
            stringToEdit = "no";
            WWW www = new WWW(path);
            while (!www.isDone) { }
            stringToEdit += " " + www.text;

            stringToEdit+=" ReadAllText:"+File.ReadAllText(path);
        }
    }
    #endregion
    //请输入一个字符串
    private string stringToEdit = "";
    void Update ()
    {
        //点击手机返回键关闭应用程序
        if (Input.GetKeyDown(KeyCode.Escape) || Input.GetKeyDown(KeyCode.Home) )
          {
               Application.Quit();
          }
    }
   
    void OnGUI()
    {
        GUILayout.TextField("persistentDataPath:" + Application.persistentDataPath);
        GUILayout.TextField("dataPath:" + Application.dataPath);
        GUILayout.TextField("temporaryCachePath:" + Application.temporaryCachePath);
        stringToEdit = GUILayout.TextArea (stringToEdit, GUILayout.Width(300),GUILayout.Height(250));         
    }   
   
}
//用到解压的库。。。http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx
//感谢:雨松MOMO http://www.xuanyusong.com/
//示例demo http://files.cnblogs.com/solq/android_read_Resources.zip

本帖子中包含更多资源

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

×

评分

参与人数 4鲜花 +2 +2 收起 理由
ao1shib1234 + 1
afteryunlei + 1 赞一个!
爱本是恨的来处 + 1
jerry6084 + 1 很给力!

查看全部评分

发表于 2013-5-21 09:46 | 显示全部楼层

感谢楼主的无私分享!{:soso__11402694654016840197_7:}
发表于 2013-5-21 12:18 | 显示全部楼层
{:5_376:}{:5_427:}{:5_435:}
发表于 2013-5-22 00:11 | 显示全部楼层
学习一下 谢谢分享
- 本文出自Unity3D联盟,原文地址:http://www.u3dchina.com/t-3959-1-1.html
发表于 2013-6-5 10:39 | 显示全部楼层
好的教學~~ 感謝
发表于 2017-5-1 10:02 | 显示全部楼层
很不错
发表于 2017-5-1 09:26 | 显示全部楼层
真心顶
发表于 2017-5-1 10:07 | 显示全部楼层
说的非常好
发表于 2017-5-1 10:19 | 显示全部楼层
很好哦
发表于 2017-5-1 09:29 | 显示全部楼层
不错不错
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2024-5-5 06:22 , Processed in 0.170005 second(s), 30 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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