Beispiel #1
0
    public object[] Import(string path)
    {
        if (string.IsNullOrEmpty(path))
        {
            return(null);
        }
        if (!path.EndsWith(".json"))
        {
            path += ".json";
        }

        string txt = FileGameUtil.ReadText(path);

        if (string.IsNullOrEmpty(txt))
        {
            return(null);
        }

        List <object> instanceList = new List <object>();
        ArrayList     dataJson     = MiniJson.jsonDecode(txt) as ArrayList;

        foreach (object obj in dataJson)
        {
            Hashtable itemTab   = obj as Hashtable;
            Type      classType = Type.GetType(Convert.ToString(itemTab["type"]));

            Hashtable dataTab = itemTab["data"] as Hashtable;
            Dictionary <string, object> reflectDic = CopyTo(dataTab);
            object instance = InjectDataBinder.BindingData(classType, reflectDic);
            if (instance != null)
            {
                instanceList.Add(instance);
            }
        }
        return(instanceList.ToArray());
    }
Beispiel #2
0
    public bool Export <T>(string path, object[] objArr) where T : Attribute
    {
        if (objArr == null || objArr.Length <= 0 || string.IsNullOrEmpty(path))
        {
            return(false);
        }

        if (!path.EndsWith(".json"))
        {
            path += ".json";
        }

        StringBuilder sb = new StringBuilder();

        sb.Append("[");
        foreach (object obj in objArr)
        {
            sb.AppendFormat("{{\"type\":\"{0}\",", obj.GetType().Name);
            sb.Append("\"data\":{");
            Dictionary <string, object> reflectDic = InjectDataBinder.ReflectAllData <T>(obj);
            saveJson(sb, reflectDic);
            sb.Append("}},");
        }
        if (sb[sb.Length - 1] == ',')
        {
            sb.Remove(sb.Length - 1, 1);
        }

        sb.Append("]");

        FileGameUtil.WriteText(path, sb.ToString());
#if UNITY_EDITOR
        AssetDatabase.Refresh();
#endif
        return(true);
    }