Unity reads the configuration file in Android’s streamingAssets

First of all, the streamingAssets of Android are not in the hard disk directory, and it cannot be read directly, so direct Application.StreamingAssets+”/file name” is not acceptable, because the files in the original streamingAssets are all packaged into the jar package, jar It is a compressed package, you can decompress it directly with the decompressed file, and you can see the original file in a file called assets

We cannot directly access the content of the streamingassets file by accessing the path, but we can use the www class to read the content and copy it to the Applicaiton.persistentDataPath path, which is the readable and writable path provided by Unity under Android , but it will only appear after the APK is installed, so we can’t create this folder ourselves in the project

Copy the content in streamingAssets to persistentDataPath and store it in the local dictionary, and the specific code to get the value is as follows

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class CommoneData
{
    ///  <summary> 
    /// The name of the configuration file 
    ///  </summary>
     private  static  string configName = "/Config.txt" ;
     ///  <summary> 
    /// The dictionary that holds the configuration file, in Android Can't read ini directly, we use txt 
    ///  </summary>
     private  static Dictionary< string , string > dic = new Dictionary< string , string >();

    /// <summary>
    /// </summary>
    /// <param ></param>
    /// <returns></returns>
    public static string GetConfig(string key)
    {
        //If there is no configuration file in the persistent directory, first copy a copy from streamingAssets to the persistent directory 
        if (!File.Exists(Application.persistentDataPath + configName))
        {
            WWW loadWWW = new WWW(Application.streamingAssetsPath + configName);
            while (!loadWWW.isDone)
            {

            }
            File.WriteAllBytes(Application.persistentDataPath + configName, loadWWW.bytes); 
        }

        //If there is no key-value pair in this dictionary, read all key-value pairs first 
        if (dic.Keys.Count == 0 )
        {
            //Read all configurations and disassemble them into a dictionary 
            string [] allLines = File.ReadAllLines(Application.persistentDataPath + configName);
             for ( int i = 0 ; i < allLines.Length; i++)
            {
                string[] temp = allLines[i].Split('=');
                dic.Add(temp[0].Trim(), temp[1].Trim());
            }
        }

        //find value by key 
        try
        {
            return dic[key];
        }
        catch
        {
            Debug.LogError( "No key in dictionary: " + key);
             return  "" ;
        }
    }

}

Then you can read the content in persistentDataPath just like reading streamingAssets

Additional attention
1 Untiy packaged Android installation directory
2 Android cannot use ini configuration files

Leave a Comment

Your email address will not be published. Required fields are marked *