Skip to content

Instantly share code, notes, and snippets.

@johnsoncodehk
Last active February 24, 2020 06:44
Show Gist options
  • Save johnsoncodehk/6941e0cefd7b62c505d1a95aca249fa6 to your computer and use it in GitHub Desktop.
Save johnsoncodehk/6941e0cefd7b62c505d1a95aca249fa6 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;
}
}
@johnsoncodehk
Copy link
Author

johnsoncodehk commented Jan 19, 2017

Required:

Use:

  1. Create prefab_datas.json file on Assets/
  2. Open project with Unity 5.5
  3. Click Fix55To54/Write on menu
  4. Close Unity 5.5
  5. Open project with Unity 5.4
  6. Click Fix55To54/Read on menu
  7. Delete prefab_datas.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment