StreamingAssets Æú´õ¿¡¼ json ÆÄÀÏÀ» ÀÐ¾î¼ ÆÄ½Ì Çغ¸ÀÚ. Ãß°¡ÀûÀ¸·Î IOS¿¡¼ ÀÚµ¿ À¥ ij½ÌÀ» ¾ÈÇÏ´Â ¹æ¹ý¿¡ ´ëÇؼ ¾Ë¾Æ º»´Ù. 1. StreamingAssets Æú´õ ±¸Çϱâ string streamingPath;
void Start() { #if UNITY_EDITOR || UNITY_STANDALONE_WIN streamingPath = "file://" + Application.streamingAssetsPath + "/"; #elif UNITY_ANDROID streamingPath = Application.streamingAssetsPath + "/"; #elif UNITY_IOS streamingPath = "file://" + Application.streamingAssetsPath + "/"; #endif } 2. Text ÆÄÀÏ ÀбâÆÄÀÏÀ» Àоî¿À´Âµ¥ Ưº°ÇÑ ¹®Á¦Á¡ÀÌ ¾ø´Ù. StartCoroutine(LoadText());
..... IEnumerator LoadText() { WWW www = new WWW(streamingPath + "test.txt"); yield return www; if (string.IsNullOrEmpty(www.error) == false) yield break; Debug.Log(www.text); string description = ""; string txtBuffer = ""; TextReader txtReader = new StringReader(www.text); while ((txtBuffer = txtReader.ReadLine()) != null) { description = description + txtBuffer + ","; } Debug.Log(description); } 3. °£´ÜÇÑ json ÆÄÀÏ ÀбâWWW·Î °¡Á®¿Â json ÆÄÀÏÀ» JsonUtility.FromJson·Î ÆĽÌÇÏ¸é ´ÙÀ½°ú °°Àº ¿¡·¯°¡ ¹ß»ýÇÑ´Ù."JsonUtility.FromJson ArgumentException: JSON parse error: Invalid value" WWW.text ¹®ÀÚ¿¿¡ BOM Äڵ尡 3¹ÙÀÌÆ® Ãß°¡ µÇ¼ ¹ß»ýÇÏ´Â ¹®Á¦ÀÌ´Ù. ´ÙÀ½°ú °°ÀÌ ÀÎÄÚµù ÈÄ »ç¿ëÇØ¾ß ÇÑ´Ù. string jsonStr = System.Text.Encoding.UTF8.GetString(data.bytes, 3, data.bytes.Length - 3); monster.json ÆÄÀÏ {
"name":"orge", "korean":"¿À°Å", "levlel":3 } MonsterList.cs ÆÄÀÏ using System;
using System.Collections.Generic; [Serializable] public class Monster { public string name; public string korean; public int level; } StartCoroutine(LoadJson());
..... IEnumerator LoadJson() { WWW data = new WWW(streamingPath + "monster.json"); yield return data; if (string.IsNullOrEmpty(data.error) == false) yield break; string jsonStr = System.Text.Encoding.UTF8.GetString(data.bytes, 3, data.bytes.Length - 3); Monster monster = JsonUtility.FromJson<Monster>(jsonStr); Debug.Log("monster: " + monster.name + " , " + monster.korean); } 4. json ¸®½ºÆ® ÆÄÀÏ Àбâmonster_list.json ÆÄÀÏ{
"monster":[ { "name":"orge", "korean":"¿À°Å", "levlel":3 }, { "name":"troll", "korean":"Æ®·Ñ", "levlel":10 } ] } MonsterList.cs ÆÄÀÏ using System;
using System.Collections.Generic; [Serializable] public class Monster { public string name; public string korean; public int level; } [Serializable] public class MonsterList { public List<Monster> monster; } StartCoroutine(LoadJsonList());
IEnumerator LoadJsonList() { WWW data = new WWW(streamingPath + "monster_list.json"); yield return data; if (string.IsNullOrEmpty(data.error) == false) yield break; string jsonStr = System.Text.Encoding.UTF8.GetString(data.bytes, 3, data.bytes.Length - 3); MonsterList list = JsonUtility.FromJson<MonsterList>(jsonStr); Monster monster = list.monster[1]; Debug.Log("*** monster list[1]: " + monster.name + " , " + monster.korean); } 5. À¥ ij½Ì ¾ÈÇϱâÀ¥ºê¶ó¿ìÁ®¿¡¼ htmlÀ» ÀÐÀ»¶§ ÀÚµ¿À¸·Î ij½ÌÀÌ µÈ´Ù.ÀÚµ¿ ij½ÌÀ» ¾ÈÇÒ·Á¸é "www.google.com/monster_list.json?123388" ó·³ Àǹ̾ø´Â ·£´ýÇÑ ¼ýÀÚ·Î Äõ¸®¸¦ Ãß°¡Çϰųª ¾Æ·¡Ã³·³ content¿¡ "No-Cache"¸¦ Ãß°¡ ÇϸéµÈ´Ù. Àǹ̾ø´Â Äõ¸® Ãß°¡ ¹æ¹ýÀº Ŭ¶óÀ̾ðÆ®³ª CDN¿¡ ÇÊ¿ä ¾ø´Â ij½ÌÀÌ »ý±æ¼ö ÀÖ´Ù. HTMLÀÎ °æ¿ì
<META http-equiv="Expires" content="-1"> <META http-equiv="Pragma" content="no-cache"> <META http-equiv="Cache-Control" content="No-Cache"> À¯´ÏƼÀÇ WWW´Â IOS¿¡¼ ÀÚµ¿ ij½ÌÀÌ Á¤»óÀûÀ¸·Î ½ÇÇàµÈ´Ù. WWWFormÀ» »ç¿ëÇÏ¿© "No-Cache" Çʵ带 Ãß°¡ÇÑ´Ù. WWWForm form = new WWWForm();
form.AddField("Cache-Control", "no-cache"); WWW data = new WWW("http://wwww.google.com/monster_list.json", form); Âü°í) http://baramlife.tistory.com/7 http://answers.unity3d.com/questions/844423/wwwtext-not-reading-utf-8-text.html https://forum.unity3d.com/threads/jsonutility-fromjson-error-invalid-value.421291/ http: //avoex.com/development/unity-www-textÀÇ-¹®ÀÚ¿ÀÌ-ÆĽÌÀÌ-¾ÈµÇ´Â-°æ¿ì/ ¾ð¾îº° No-Cache ¼³Á¤ http://civan.tistory.com/201 [UNITY & IOS] WWW auto caching ¹®Á¦ ÇØ°áÇϱâ (Cache-Control) http://flystone.tistory.com/m/195 |