Skip to content

Instantly share code, notes, and snippets.

@mousedoc
Forked from johnsoncodehk/Fix55To54.cs
Created December 5, 2017 05:09
Show Gist options
  • Save mousedoc/333304fd0151277845a6583096a7c48f to your computer and use it in GitHub Desktop.
Save mousedoc/333304fd0151277845a6583096a7c48f to your computer and use it in GitHub Desktop.
Fix Unity 5.5 Downgrading to 5.4 missing GameObject name, activeSelf.
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class Fix55To54 {
[System.SerializableAttribute]
public class GameObjectData {
public bool activeSelf;
public string name;
public void SetData (GameObject go, object dataObj) {
go.SetActive (this.activeSelf);
go.name = this.name;
List<object> childsList = (dataObj as Dictionary<string, object>)["childs"] as List<object>;
for (int i = 0; i < childsList.Count; i++) {
Dictionary<string, object> childDict = childsList[i] as Dictionary<string, object>;
GameObjectData childData = JsonUtility.FromJson<GameObjectData> (MiniJSON.Json.Serialize (childDict));
Transform child = go.transform.GetChild (i);
childData.SetData (child.gameObject, childDict);
}
}
}
public static string path = "Assets/prefab_datas.json";
[MenuItem ("Fix55To54/Read")]
public static void Read () {
string json = File.ReadAllText (path);
Debug.Log (json);
Dictionary<string, object> data = MiniJSON.Json.Deserialize (json) as Dictionary<string, object>;
foreach (var goDataDict in data) {
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject> (goDataDict.Key);
if (go == null) {
Debug.LogWarning (goDataDict.Key);
continue;
}
Debug.Log (goDataDict.Key);
GameObjectData goData = JsonUtility.FromJson<GameObjectData> (MiniJSON.Json.Serialize (goDataDict.Value));
goData.SetData (go, goDataDict.Value);
}
AssetDatabase.SaveAssets ();
AssetDatabase.Refresh ();
}
[MenuItem ("Fix55To54/Write")]
public static void Write () {
Dictionary<string, object> data = new Dictionary<string, object> ();
foreach (string assetGuid in AssetDatabase.FindAssets("t:prefab")) {
string assetPath = AssetDatabase.GUIDToAssetPath(assetGuid);
GameObject go = AssetDatabase.LoadAssetAtPath<GameObject> (assetPath);
data[assetPath] = GetGameObjectData (go);
}
string json = MiniJSON.Json.Serialize (data);
FileInfo file = new FileInfo (path);
if (!file.Directory.Exists) {
file.Directory.Create ();
}
if (!file.Exists) {
file.Create ();
}
File.WriteAllText (path, json);
}
private static Dictionary<string, object> GetGameObjectData (GameObject go) {
List<object> childs = new List<object> ();
for (int i = 0; i < go.transform.childCount; i++) {
Transform child = go.transform.GetChild (i);
childs.Add (GetGameObjectData (child.gameObject));
}
Dictionary<string, object> data = new Dictionary<string, object> () {
{ "name", go.name },
{ "activeSelf", go.activeSelf },
{ "childs", childs },
};
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment