123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- using SimpleJSON;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using UnityEngine;
- public class WJUtility
- {
- private const string CONFIG_FOLDER = "FY/Config/";
- private const string CONFIG_FILE_FORMAT = CONFIG_FOLDER + "{0}";
- private const string NORMAL_EXT_FORMAT = "{0}.json";
- private const string DATA_LOG_FOLDER = "DataLog/";
- /// <summary>
- /// 读配置表
- /// </summary>
- /// <param name="configName">配置文件名称</param>
- /// <returns></returns>
- public static JSONNode LoadJsonConfig(string configName)
- {
- //读取内部配置表
- JSONNode result ;
- TextAsset assetData = Resources.Load(String.Format(CONFIG_FILE_FORMAT, configName)) as TextAsset;
- if (assetData == null)
- {
- Debug.Log("error null file path is " + String.Format(CONFIG_FILE_FORMAT, configName));
- return null;
- }
- string dataTxt = assetData.text;
- if (null == dataTxt)
- {
- Debug.Log("Load : Read JsonFile Error, config = " + String.Format(CONFIG_FILE_FORMAT, configName));
- return null;
- }
- result = JSONClass.Parse(dataTxt);
- if (null == result)
- {
- Debug.Log("Load: could not parse the json config " + String.Format(CONFIG_FILE_FORMAT, configName));
- }
- if (result == null)
- {
- Debug.Log("LoadJsonConfig: read file fail, config: " + configName);
- }
- return result;
- }
- /**
- * 从文件读取json
- */
- public static JSONNode ReadJsonFromFile(string fileName)
- {
- string dataTxt = ReadTxtFromExternalFile(fileName);
- if (dataTxt == null)
- {
- Debug.LogWarning("ReadJsonFromFile : could not parse the json file " + fileName);
- return null;
- }
- JSONNode result = JSONClass.Parse(dataTxt);
- if (null == result)
- {
- Debug.Log("ReadJsonFromFile : could not parse the json file " + fileName);
- }
- return result;
- }
- public static JSONNode ReadJsonFromDataLogFile(string fileName)
- {
- string dataTxt = ReadTxtFromExternalFile(DATA_LOG_FOLDER + fileName);
- JSONNode result = JSONClass.Parse(dataTxt);
- if (null == result)
- {
- Debug.Log("ReadJsonFromFile : could not parse the json file " + fileName);
- }
- return result;
- }
- public static string ReadTxtFromExternalFile(string fileName)
- {
- string finalFileName;
- finalFileName = String.Format(NORMAL_EXT_FORMAT, fileName);
-
- string dataTxt = ReadFile(GetWritablePath(finalFileName));
- if (dataTxt == null)
- {
- return null;
- }
- return dataTxt;
- }
- /**
- * 获取文件路径
- *
- */
- public static string GetWritablePath(string fileName)
- {
- string writePath = "";
- if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- writePath = Application.persistentDataPath + "/" + fileName;
- }
- else if (Application.platform == RuntimePlatform.Android)
- {
- string rootPath = Application.persistentDataPath;
- if (String.IsNullOrEmpty(rootPath) || String.IsNullOrEmpty(rootPath.Trim()))
- {
- writePath = Application.persistentDataPath + "/" + fileName;
- }
- writePath = rootPath + "/" + fileName;
- }
- else
- {
- writePath = Application.dataPath + "/../" + fileName;
- }
- Debug.LogWarning(writePath);
- return writePath;
- }
- public static string ReadFile(string filename)
- {
- if (!File.Exists(filename))
- {
- return null;
- }
- string result = "";
- try
- {
- result = File.ReadAllText(filename, Encoding.UTF8);
- }
- catch (System.Exception )
- {
-
- }
- return result;
- }
- /**
- * 写Json到文件
- *
- */
- public static void WriteJsonToFile(JSONNode jsonData, string fileName)
- {
- WriteJsonToFile(jsonData.ToString(), fileName);
- }
- public static void WriteJsonToDataLogFile(JSONNode jsonData, string fileName)
- {
- string path = GetWritablePath(DATA_LOG_FOLDER);
- if (!Directory.Exists(path))
- {
- Directory.CreateDirectory(path);
- }
- WriteJsonToFile(jsonData.ToString(), DATA_LOG_FOLDER + fileName);
- }
- public static void WriteJsonToFile(string fileData, string fileName)
- {
- string finalFileName;
- finalFileName = String.Format(NORMAL_EXT_FORMAT, fileName);
- string dataTxt = fileData;
- WriteFile(GetWritablePath(finalFileName), dataTxt);
- }
- public static void WriteFile(string filename, string text)
- {
- try
- {
- File.WriteAllText(filename, text, new UTF8Encoding(false));
- }
- catch (System.Exception ex)
- {
- Debug.Log(ex.ToString() + "WriteFile : could not write the file " + filename);
- }
- }
- public static void PurgeChildren (GameObject go,bool instant = false ,bool bIncludeUnactive = false)
- {
- if (go == null){
- return;
- }
- GameObject temp;
- if(!instant){
- for(int i = 0; i < go.transform.childCount; i++)
- {
- temp = go.transform.GetChild(i).gameObject;
- if (bIncludeUnactive || temp.activeSelf)
- {
- GameObject.Destroy(temp);
- }
- }
- }else{
- Transform[] transArray = new Transform[go.transform.childCount];
- for(int i = 0; i < go.transform.childCount; i++)
- {
- transArray[i] = go.transform.GetChild(i);
- }
- for(int i = 0; i < transArray.Length; i++)
- {
- temp = transArray[i].gameObject;
- if (bIncludeUnactive || temp.activeSelf)
- {
- GameObject.DestroyImmediate(temp);
- }
- }
- }
- }
- }
|