// Model export generates meshes in world coordinates // In order to retain information, each prefab is replaced with a transformed tetrahedron static void ConfigurePrefab(Transform placeholder, CachedPrefab cached) { var prefab = (PrefabUtility.InstantiatePrefab(cached.prefab) as GameObject).transform; EP.SetParent(prefab, placeholder.parent); prefab.localPosition = placeholder.localPosition; prefab.localRotation = placeholder.localRotation; prefab.localScale = placeholder.localScale; prefab.name = placeholder.name; }
static Dictionary <string, CachedPrefab> GetPrefabs(string searchRoot) { var prefabs = new Dictionary <string, CachedPrefab>(); var prefabGUIDs = AssetDatabase.FindAssets("t:GameObject", new[] { searchRoot }); foreach (var guid in prefabGUIDs) { var cached = new CachedPrefab(guid); if (cached.type != "prefab") { continue; } if (prefabs.ContainsKey(cached.name)) { Debug.LogWarning($"Repeated prefab key {cached.name} at {AssetDatabase.GUIDToAssetPath(guid)} and {prefabs[cached.name].path}"); continue; } prefabs.Add(cached.name, cached); } return(prefabs); }
static void ReplacePlaceholders(string prefabPath, GameObject gameObject) { Dictionary <string, CachedPrefab> prefabs = GetPrefabs(prefabPath); var children = gameObject.Children(true); foreach (var child in children) { // Do not modify existing child prefabs var childPrefab = PrefabUtility.GetNearestPrefabInstanceRoot(child); if (childPrefab != null && childPrefab != gameObject) { continue; } // Placeholder names are constructed as "prefab_name=object_name" var name_parts = child.name.Split('='); if (name_parts.Length == 1) { continue; } // Create an placeholder prefab that can be modified after import if (!prefabs.ContainsKey(name_parts[0])) { var placeholder = EP.Instantiate(); placeholder.name = name_parts[0]; var placeholderPath = prefabPath + "/" + placeholder.name + ".prefab"; var placeholderAsset = PrefabUtility.SaveAsPrefabAsset(placeholder, placeholderPath); prefabs[name_parts[0]] = new CachedPrefab(placeholderAsset); EP.Destroy(placeholder); //Debug.Log($"Missing prefab in {gameObject.name} for {child.Path()} -> created placeholder"); } ConfigurePrefab(child.transform, prefabs[name_parts[0]]); EP.Destroy(child); } }
public static void CacheGOs(bool includePlayer) { List<CachedPrefab> loadedRoomGOs = new List<CachedPrefab>(); foreach (KeyValuePair<string, GameObject> kvp in loadedGOs) { CachedPrefab cachedGO = new CachedPrefab(); cachedGO.source = kvp.Key; GameObject go = kvp.Value; if (go == null || go.transform.parent != null || go.tag == "DontSave" || !includePlayer && go.tag == "Player") { continue; } MonoHelper goHelper = MonoHelper.GetMonoHelper(go); cachedGO.hash = goHelper.monoID; cachedGO.position = go.transform.position; cachedGO.scale = go.transform.localScale; cachedGO.rotation = go.transform.rotation.eulerAngles; JSONClass components = new JSONClass(); MonoHelper[] helpers = go.GetComponentsInChildren<MonoHelper>(); foreach (MonoHelper helper in helpers) { JSONClass helperJSON = helper.ToJSON(); if (helperJSON == null) { continue; } string helperName = helper.GetType().Name; components[helperName] = helperJSON; } cachedGO.components = components; loadedRoomGOs.Add(cachedGO); } string levelName = Application.loadedLevelName; if (roomGOs.ContainsKey(levelName)) { roomGOs[levelName] = loadedRoomGOs; } else { roomGOs.Add(Application.loadedLevelName, loadedRoomGOs); } }
private static MonoHelper LoadFromCache(CachedPrefab cachedGO) { GameObject go = Load(cachedGO.source) as GameObject; MonoHelper goMonoHelper = MonoHelper.GetMonoHelper(go); goMonoHelper.LoadFromCache(cachedGO); return goMonoHelper; }
public static void FromJSON(JSONClass json, string roomName) { roomGOs.Clear(); json = json["roomdata"].AsObject; for (int i = 0; i < json.Count; i++) { string thisRoom = json.GetKey(i); JSONArray roomObjects = json[i].AsArray; List<CachedPrefab> cachedRoomGOs = new List<CachedPrefab>(); for (int j = 0; j < roomObjects.Count; j++) { JSONClass roomObject = roomObjects[j].AsObject; CachedPrefab cachedGO = new CachedPrefab(); cachedGO.source = roomObject["source"]; cachedGO.hash = roomObject["hash"]; cachedGO.position = SaveGame.ConvertVector3(roomObject["position"].AsObject); cachedGO.scale = SaveGame.ConvertVector3(roomObject["scale"].AsObject); cachedGO.rotation = SaveGame.ConvertVector3(roomObject["rotation"].AsObject); cachedGO.components = roomObject["components"].AsObject; cachedRoomGOs.Add(cachedGO); } roomGOs.Add(thisRoom, cachedRoomGOs); } ReloadLevel(roomName); }
public void LoadFromCache(CachedPrefab cachedGO) { json.LoadFromCache(cachedGO); }