static GameObject CreateFromResource(string itemName, string holderName, bool unique = false) { if (unique) { GameObject o = GameObject.Find(MapComponents.FixName(itemName)); if (o != null) { Debug.LogError(itemName + " already exists!"); Selection.activeGameObject = o; return(null); } } GameObject obj = Instantiate(Resources.Load <GameObject>(itemName)); obj.name = itemName; obj.transform.position = GetSpawnPos(); if (holderName != null && holderName.Length > 0) { GameObject holder = GameObject.Find(holderName); if (holder == null) { holder = new GameObject(holderName); } obj.transform.SetParent(holder.transform); } Selection.activeGameObject = obj; return(obj); }
// Serialize prefab state; return true if we did it because we can skip // the rest of normal processing in that case. bool SerializePrefab(GameObject go, LevelObject lo) { if (IsSafeToBake(go)) { return(false); } // If it only has one component which is a light, we can bail - it's the // sun which is extracted in another way. if (go.GetComponents <Component>().Length == 2 && go.GetComponent <Light>() != null) { return(false); } if (MapComponents.FixName(go.name) == "CheckPoint" || MapComponents.FixName(go.name) == "LevelBounds" || go.GetComponent <TutorialMessage>() != null) { lo.prefabItem = GetPrefabID(MapComponents.FixName(go.name), null); return(true); } int validChildren = 0; for (int i = 0; i < go.transform.childCount; i++) { if (go.transform.GetChild(i).GetComponent <IgnoreObject>() == null) { validChildren++; } } if (validChildren == 0 && go.GetComponents <Component>().Length < 2) { lo.prefabItem = GetPrefabID(go.name, null); return(true); } return(false); }
LevelObject SerializeGameObject(GameObject go, bool rejectStatic = true) { var lo = new LevelObject(); lo.name = MapComponents.FixName(go.name); go.name = lo.name; SerializeTransform(go, lo); SerializeTutorial(go, lo); SerializeBoxCollider(go, lo); SerializeMisc(go, lo); SerializeMover(go, lo); if (SerializePrefab(go, lo)) { return(lo); } SerializeMesh(go, lo, rejectStatic); bool skipChildren; bool doKeep = SerializeKnownComponents(go, lo, out skipChildren); lo.LargestChildVertexCount = lo.mesh != null ? lo.mesh.vertexCount : 0; for (var i = 0; i < go.transform.childCount; i++) { var child = go.transform.GetChild(i); if (skipChildren) { continue; } if (child.gameObject.activeInHierarchy == false) { continue; } if (IsGameObjectPrunable(child.gameObject)) { continue; } var result = SerializeGameObject(go.transform.GetChild(i).gameObject); if (result == null) { continue; } lo.children.Add(result); lo.LargestChildVertexCount = Mathf.Max(lo.LargestChildVertexCount, lo.LargestChildVertexCount); } // Bail if we are empty and have no children. if (lo.children.Count == 0 && lo.mesh.vertexCount == 0 && (lo.prefabItem == null || lo.prefabItem == "") && !doKeep) { return(null); } return(lo); }