WJUtility.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using SimpleJSON;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Text;
  7. using UnityEngine;
  8. public class WJUtility
  9. {
  10. private const string CONFIG_FOLDER = "FY/Config/";
  11. private const string CONFIG_FILE_FORMAT = CONFIG_FOLDER + "{0}";
  12. private const string NORMAL_EXT_FORMAT = "{0}.json";
  13. private const string DATA_LOG_FOLDER = "DataLog/";
  14. /// <summary>
  15. /// 读配置表
  16. /// </summary>
  17. /// <param name="configName">配置文件名称</param>
  18. /// <returns></returns>
  19. public static JSONNode LoadJsonConfig(string configName)
  20. {
  21. //读取内部配置表
  22. JSONNode result ;
  23. TextAsset assetData = Resources.Load(String.Format(CONFIG_FILE_FORMAT, configName)) as TextAsset;
  24. if (assetData == null)
  25. {
  26. Debug.Log("error null file path is " + String.Format(CONFIG_FILE_FORMAT, configName));
  27. return null;
  28. }
  29. string dataTxt = assetData.text;
  30. if (null == dataTxt)
  31. {
  32. Debug.Log("Load : Read JsonFile Error, config = " + String.Format(CONFIG_FILE_FORMAT, configName));
  33. return null;
  34. }
  35. result = JSONClass.Parse(dataTxt);
  36. if (null == result)
  37. {
  38. Debug.Log("Load: could not parse the json config " + String.Format(CONFIG_FILE_FORMAT, configName));
  39. }
  40. if (result == null)
  41. {
  42. Debug.Log("LoadJsonConfig: read file fail, config: " + configName);
  43. }
  44. return result;
  45. }
  46. /**
  47. * 从文件读取json
  48. */
  49. public static JSONNode ReadJsonFromFile(string fileName)
  50. {
  51. string dataTxt = ReadTxtFromExternalFile(fileName);
  52. if (dataTxt == null)
  53. {
  54. Debug.LogWarning("ReadJsonFromFile : could not parse the json file " + fileName);
  55. return null;
  56. }
  57. JSONNode result = JSONClass.Parse(dataTxt);
  58. if (null == result)
  59. {
  60. Debug.Log("ReadJsonFromFile : could not parse the json file " + fileName);
  61. }
  62. return result;
  63. }
  64. public static JSONNode ReadJsonFromDataLogFile(string fileName)
  65. {
  66. string dataTxt = ReadTxtFromExternalFile(DATA_LOG_FOLDER + fileName);
  67. JSONNode result = JSONClass.Parse(dataTxt);
  68. if (null == result)
  69. {
  70. Debug.Log("ReadJsonFromFile : could not parse the json file " + fileName);
  71. }
  72. return result;
  73. }
  74. public static string ReadTxtFromExternalFile(string fileName)
  75. {
  76. string finalFileName;
  77. finalFileName = String.Format(NORMAL_EXT_FORMAT, fileName);
  78. string dataTxt = ReadFile(GetWritablePath(finalFileName));
  79. if (dataTxt == null)
  80. {
  81. return null;
  82. }
  83. return dataTxt;
  84. }
  85. /**
  86. * 获取文件路径
  87. *
  88. */
  89. public static string GetWritablePath(string fileName)
  90. {
  91. string writePath = "";
  92. if (Application.platform == RuntimePlatform.IPhonePlayer)
  93. {
  94. writePath = Application.persistentDataPath + "/" + fileName;
  95. }
  96. else if (Application.platform == RuntimePlatform.Android)
  97. {
  98. string rootPath = Application.persistentDataPath;
  99. if (String.IsNullOrEmpty(rootPath) || String.IsNullOrEmpty(rootPath.Trim()))
  100. {
  101. writePath = Application.persistentDataPath + "/" + fileName;
  102. }
  103. writePath = rootPath + "/" + fileName;
  104. }
  105. else
  106. {
  107. writePath = Application.dataPath + "/../" + fileName;
  108. }
  109. Debug.LogWarning(writePath);
  110. return writePath;
  111. }
  112. public static string ReadFile(string filename)
  113. {
  114. if (!File.Exists(filename))
  115. {
  116. return null;
  117. }
  118. string result = "";
  119. try
  120. {
  121. result = File.ReadAllText(filename, Encoding.UTF8);
  122. }
  123. catch (System.Exception )
  124. {
  125. }
  126. return result;
  127. }
  128. /**
  129. * 写Json到文件
  130. *
  131. */
  132. public static void WriteJsonToFile(JSONNode jsonData, string fileName)
  133. {
  134. WriteJsonToFile(jsonData.ToString(), fileName);
  135. }
  136. public static void WriteJsonToDataLogFile(JSONNode jsonData, string fileName)
  137. {
  138. string path = GetWritablePath(DATA_LOG_FOLDER);
  139. if (!Directory.Exists(path))
  140. {
  141. Directory.CreateDirectory(path);
  142. }
  143. WriteJsonToFile(jsonData.ToString(), DATA_LOG_FOLDER + fileName);
  144. }
  145. public static void WriteJsonToFile(string fileData, string fileName)
  146. {
  147. string finalFileName;
  148. finalFileName = String.Format(NORMAL_EXT_FORMAT, fileName);
  149. string dataTxt = fileData;
  150. WriteFile(GetWritablePath(finalFileName), dataTxt);
  151. }
  152. public static void WriteFile(string filename, string text)
  153. {
  154. try
  155. {
  156. File.WriteAllText(filename, text, new UTF8Encoding(false));
  157. }
  158. catch (System.Exception ex)
  159. {
  160. Debug.Log(ex.ToString() + "WriteFile : could not write the file " + filename);
  161. }
  162. }
  163. public static void PurgeChildren (GameObject go,bool instant = false ,bool bIncludeUnactive = false)
  164. {
  165. if (go == null){
  166. return;
  167. }
  168. GameObject temp;
  169. if(!instant){
  170. for(int i = 0; i < go.transform.childCount; i++)
  171. {
  172. temp = go.transform.GetChild(i).gameObject;
  173. if (bIncludeUnactive || temp.activeSelf)
  174. {
  175. GameObject.Destroy(temp);
  176. }
  177. }
  178. }else{
  179. Transform[] transArray = new Transform[go.transform.childCount];
  180. for(int i = 0; i < go.transform.childCount; i++)
  181. {
  182. transArray[i] = go.transform.GetChild(i);
  183. }
  184. for(int i = 0; i < transArray.Length; i++)
  185. {
  186. temp = transArray[i].gameObject;
  187. if (bIncludeUnactive || temp.activeSelf)
  188. {
  189. GameObject.DestroyImmediate(temp);
  190. }
  191. }
  192. }
  193. }
  194. }