Last active
February 24, 2020 06:44
-
-
Save johnsoncodehk/6941e0cefd7b62c505d1a95aca249fa6 to your computer and use it in GitHub Desktop.
Fix Unity 5.5 Downgrading to 5.4 missing GameObject name, activeSelf.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Required:
Use: