// ----------------------------------------- // DumpPrefab // ----------------------------------------- public JSON_Prefab DumpPrefab(GameObject _prefab) { JSON_Prefab result = new JSON_Prefab(); List <GameObject> nodes = new List <GameObject>(); bool isAnimPrefab = Utils.IsAnimPrefab(_prefab); // collect nodes Utils.Walk(new List <GameObject> { _prefab }, _go => { if (isAnimPrefab) { // this is a joint, skip it. if (_go.GetComponents <Component>().Length == 1) { return(false); } } nodes.Add(_go); return(true); }); // dump entities foreach (GameObject go in nodes) { JSON_Entity ent = DumpEntity(go, nodes); result.entities.Add(ent); } return(result); }
// ----------------------------------------- // DumpScene // ----------------------------------------- JSON_Scene DumpScene(List <GameObject> _nodes) { JSON_Scene scene = new JSON_Scene(); // dump entities int index = 0; foreach (GameObject go in _nodes) { JSON_Entity ent = DumpEntity(go, _nodes); if (go.transform.parent == null) { scene.children.Add(index); } scene.entities.Add(ent); index += 1; } return(scene); }
// ----------------------------------------- // DumpEntity // ----------------------------------------- JSON_Entity DumpEntity(GameObject _go, List <GameObject> _nodes) { JSON_Entity result = new JSON_Entity(); // name result.name = _go.name; result.enabled = _go.activeSelf; // NOTE: skinned mesh node will use identity matrix if (_go.GetComponent <SkinnedMeshRenderer>() != null || _go.GetComponent <Canvas>() != null) { // translation result.translation[0] = 0.0f; result.translation[1] = 0.0f; result.translation[2] = 0.0f; // rotation result.rotation[0] = 0.0f; result.rotation[1] = 0.0f; result.rotation[2] = 0.0f; result.rotation[3] = 1.0f; // scale result.scale[0] = 1.0f; result.scale[1] = 1.0f; result.scale[2] = 1.0f; } else { // translation // NOTE: convert LH to RH result.translation[0] = _go.transform.localPosition.x; result.translation[1] = _go.transform.localPosition.y; result.translation[2] = -_go.transform.localPosition.z; // rotation // NOTE: convert LH to RH result.rotation[0] = -_go.transform.localRotation.x; result.rotation[1] = -_go.transform.localRotation.y; result.rotation[2] = _go.transform.localRotation.z; result.rotation[3] = _go.transform.localRotation.w; // scale result.scale[0] = _go.transform.localScale.x; result.scale[1] = _go.transform.localScale.y; result.scale[2] = _go.transform.localScale.z; } // children result.children = new List <int>(); foreach (Transform child in _go.transform) { int idx = _nodes.IndexOf(child.gameObject); if (idx != -1) { result.children.Add(idx); } } // check if it is a prefab GameObject prefab = PrefabUtility.GetPrefabParent(_go) as GameObject; if (prefab) { // DELME: DO WE NEED THIS?? PLEASE CONFIRM! // prefab = prefab.transform.root.gameObject; string id = Utils.AssetID(prefab); result.prefab = id; result.modifications = DumpModifications(_go, prefab); } else { // NOTE: if we are prefab, do not serailize its components // serialize components result.components = DumpComponents(_go); } return(result); }