解決 unity 生成 android apk read Resources


        TextAsset t = (TextAsset)Resources.Load("skill2");
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(t.text.ToString().Trim());
         

        XmlElement n = (XmlElement)xmlDoc.SelectSingleNode("/datas/data[@skillID='1002']");  
        if (n != null)
        {
            print("+++++++++++++++++++++++++++++++++++++++++++++++++");
            data += n.GetAttribute("skillID");
        } 

解決 unity 生成 android apk read Resources 

試了很多方法都找不到 unity 的目錄,,最后沒有辦法了,放在 Resources 目錄里。。。

發覺只能讀不能寫,,汗,,,白費了,又重新找。。。。

續:

http://forum.unity3d.com/threads/97043-Sqlite-for-Android-help-please/page2

http://forum.unity3d.com/threads/114660-Unity-3D-Android-SQLite-Examples

http://unity3d.com/support/documentation/Manual/StreamingAssets.html

 

找到了 android 工程模板了。。。。

Unity\Editor\Data\PlaybackEngines\androidplayer\

就像 web 模板一樣

把它復制出來,導入eclipse

好了,只能拼 android 基礎了。。。。

做好放在 

資料:http://www.xuanyusong.com/archives/676

http://game.ceeger.com/Manual/Plugins.html

 

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

解決了,,,下面說下步驟

test.txt 路徑如圖

 void readFile(string file)
    {
        path = "jar:file://" + Application.dataPath + "!/assets/" + file;

        if (File.Exists(path))
        {
            stringToEdit = "yes";
        }
        else
        {
            stringToEdit = "no";

            WWW www = new WWW(path);
            while (!www.isDone) { }

            stringToEdit += " " + www.text;

        }
    }

不知道為什么 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM