public void Display(NestedPrefab parent, Vector3 localToWorld, int index) { _parent = parent; Gizmos.color = Color; var mesh = Prefab.GetComponent <MeshFilter>(); //Handles.Label(pos + Vector3.up, string.Format("{0} {1}", Prefab.name, index)); NestedPrefab nestedPrefab = null; if (mesh == null) { nestedPrefab = Prefab.GetComponent <NestedPrefab>(); } for (int i = 0; i < ChildPositions.Length; i++) { if (mesh != null) { Gizmos.DrawWireMesh(mesh.sharedMesh, localToWorld + ChildPositions[i], Quaternion.Euler(Rotation), Prefab.transform.localScale); } else if (nestedPrefab != null) { nestedPrefab.DrawGizmos(parent.transform, RootPosition + ChildPositions[i]); } else { var cubeSize = Vector3.one; var collider = Prefab.GetComponent <Collider>(); if (collider != null) { cubeSize = collider.bounds.size; } Gizmos.DrawWireCube(localToWorld, cubeSize); } } }
void IterateAllObjects() { NestedPrefab prefab = (NestedPrefab)target; // Get all game objects in scene. GameObject[] objs = GameObject.FindObjectsOfType <GameObject> (); // Iterate all over them. foreach (GameObject obj in objs) { if ((obj != null) && obj.name.StartsWith(prefab.name)) { if (!prefab.m_isUpdateChildren) { // Just update components of game object. RemoveComponentsFromObject(obj); CopyComponentsFromPrefab(obj); CopyTagFromPrefab(obj); } else { // Substitute an instance of prefab for game object in the scene. ChangeGameObject(obj); } } } }
public void Spawn() { if (instantiated != null || isSpawning) { return; } NestedPrefab rootParent = FindRootNested(this); if (rootParent != null && rootParent != this) { rootParent.Spawn(); return; } isSpawning = true; //Dbg.LogRelease(this, "Spawning from {0}", this); using (var previouslyInstantiated = TempList <GameObject> .Get()) { WalkRecursive(transform, previouslyInstantiated.buffer); } isSpawning = false; }
void GeneratePrefab(NestedPrefabData prefabData) { // Debug.Log("NestedPrefab::GeneratePrefab - prefabData " + prefabData.prefabPath + " "); Transform parent = GetHierarchyTransform(prefabData.hierarchyPathId); GameObject clone = prefabGenerator.GenerateFrom(prefabData.prefabPath); if (clone == null) { Debug.LogError("Error when trying to generate prefab " + prefabData.prefabPath + " !", this); } clone.transform.parent = parent; prefabData.CopyDataTo(clone.transform); // Recursive generation NestedPrefab nestedPrefab = clone.GetComponent <NestedPrefab>(); if (nestedPrefab != null) { nestedPrefab.prefabGenerator = prefabGenerator; nestedPrefab.GeneratePrefabs(false); // Avoid to ask permission twice } }
public override void OnInspectorGUI() { base.OnInspectorGUI(); NestedPrefab nestedPrefab = target as NestedPrefab; if (nestedPrefab == null) { return; } if (!Application.isPlaying) { nestedPrefab.prefabGenerator = new DefaultPrefabGeneration(); } if (GUILayout.Button("Save Nested Prefabs")) { nestedPrefab.SavePrefabData(); } if (GUILayout.Button("Generate Prefabs")) { nestedPrefab.GeneratePrefabs(); } }
static bool SpawnInternal( NestedPrefab prefabInstance, List <GameObject> previouslyInstantiated ) { GameObject instantiated = prefabInstance.InstantiateSelf(previouslyInstantiated); if (instantiated == null) { return(false); } prefabInstance.Setup(); // BUG: If not set dirty here .instantiated is not properly saved #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.SetDirty(prefabInstance); EditorUtility.SetDirty(prefabInstance.gameObject); } #endif // UNITY_EDITOR return(true); }
static void UpdateSceneObjects(GameObject prefab, List <NestedPrefab> allSceneObjects) { for (int i = allSceneObjects.Count - 1; i >= 0; --i) { NestedPrefab sceneNested = allSceneObjects[i]; // May be destroyed by a parent if (sceneNested == null) { continue; } var pfType = PrefabUtility.GetPrefabType(sceneNested); if (pfType != PrefabType.PrefabInstance) { continue; } var scenePrefabRoot = PrefabUtility.FindRootGameObjectWithSameParentPrefab(sceneNested.gameObject); if (scenePrefabRoot == null) { continue; } var scenePrefab = PrefabUtility.GetPrefabParent(scenePrefabRoot) as GameObject; if (scenePrefab != prefab) { continue; } Transform transform = sceneNested.transform; if (transform.childCount == 0) { continue; } Dbg.Log(sceneNested.gameObject, "Destroying all children under {0} (prefab = {1})", sceneNested.gameObject.name, prefab); for (int childIndex = transform.childCount - 1; childIndex >= 0; --childIndex) { Transform child = transform.GetChild(childIndex); DestroyImmediate(child.gameObject); } // TODO: Not sure if this is needed EditorUtility.SetDirty(sceneNested.gameObject); // Prefab connection is lost when destroying children // Prefab connection was not "knowingly" disconnected // since checking PrefabType before, so reconnecting // should be safe PrefabUtility.ConnectGameObjectToPrefab(scenePrefabRoot, scenePrefab); allSceneObjects.RemoveAt(i); } }
protected override void Draw() { NestedPrefab.editorSelectionAllowed = EditorGUILayout.Toggle("Selection Allowed", NestedPrefab.editorSelectionAllowed); if (GUILayout.Button("Respawn All")) { NestedPrefab.EditorRespawnAll(); } }
// Use this for initialization void Start() { //prefab = GetComponent<GameObject>(); nestedBonusDogPrefab = GameObject.FindObjectOfType <NestedPrefab>(); //transform = nestedBonusDogPrefab.transform; //Instantiate(transform); //Instantiate(prefab); InvokeRepeating("createBonusObject", spawnTime, spawnTime); }
public void SelectChunk() { if (currentScrollObject == null) { currentScrollObject = GetComponent <ScrollingObject>(); } lastSelectedChunck = chunks.GetRandom(); currentScrollObject.size = lastSelectedChunck.GetComponent <ScrollingObject>().size; }
public void SelectChunk() { if (currentScrollObject == null) { currentScrollObject = GetComponent <ScrollingObject> (); } lastSelectedChunk = chunks.GetRandom(); //Pega o tamanho do chunck atual currentScrollObject.size = lastSelectedChunk.GetComponent <ScrollingObject> ().size; }
public static void EditorRespawnAll() { using (var prefabs = TempList <NestedPrefab> .Get()) { EditorHelper.FindSceneObjects(prefabs.buffer); for (int i = 0; i < prefabs.Count; i++) { NestedPrefab pf = prefabs[i]; pf.Respawn(); } } }
void CopyComponentsFromPrefab(GameObject obj) { NestedPrefab prefab = (NestedPrefab)target; Component[] components = prefab.GetComponents <Component> (); foreach (Component component in components) { if ((component.GetType() != typeof(Transform)) && (component.GetType() != typeof(NestedPrefab))) { CopyComponentFromPrefab(component, obj); } } }
public void SelectChunk() { if (currentScrollObject == null) { currentScrollObject = GetComponent <ScrollingObject> (); } lastSelectedChunk = chunks.GetRandom(); // Pega o tamanho do chunk atual currentScrollObject.size = lastSelectedChunk.GetComponent <ScrollingObject> ().size; //currentScrollObject.GetComponent<Transform>().rotation = new Quaternion (0f,0f,Random.Range(0f,360f),0f); }
static void SpawnInternal( NestedPrefab prefabInstance, List <NestedPrefab> queue, List <GameObject> previouslyInstantiated ) { GameObject instantiated = prefabInstance.InstantiateSelf(previouslyInstantiated); if (instantiated == null) { return; } prefabInstance.Setup(); // BUG: If not set dirty here .instantiated is not properly saved #if UNITY_EDITOR if (!Application.isPlaying) { EditorUtility.SetDirty(prefabInstance); EditorUtility.SetDirty(prefabInstance.gameObject); } #endif // UNITY_EDITOR // Enqueue all child NestedPrefab prefabInstance.GetComponentsInChildren <NestedPrefab>( includeInactive: true, result: queue ); queue.Remove(prefabInstance); // TODO: Fix this //// Remove all NestedPrefab that shares the //// same prefab (this includes the prefab itself //// and any child that may be nested) //for(int i = queue.Count - 1; i >= 0; --i) //{ // NestedPrefab iter = queue[i]; // if(iter.prefab == prefabInstance.prefab) // { // Dbg.LogErrorIf( // iter != prefabInstance, // iter, // "{0} was already instantiated in this hierarchy. It will not be instantiated to avoid recursion.", // iter.prefab // ); // queue.RemoveAt(i); // } //} }
public static Dictionary <string, GameObject> CollectNestedDictionary(NestedPrefab[] allNestedObjects, List <NestedPrefab> nestedPrefabsForSearch) { Dictionary <string, GameObject> nestedDictionary = new Dictionary <string, GameObject>(); foreach (NestedPrefab nestedCurrent in nestedPrefabsForSearch) { if (!nestedDictionary.ContainsKey(nestedCurrent.prefabGuid)) { // find prefabs for selected foreach (NestedPrefab np in allNestedObjects) { GameObject gameobject = np.gameObject; if (PrefabUtility.GetPrefabType(gameobject) == PrefabType.Prefab && PrefabUtility.FindPrefabRoot(gameobject) == gameobject) { NestedPrefab nestedSource = gameobject.GetComponent <NestedPrefab>(); if (nestedSource != null) { if (nestedSource.prefabGuid == nestedCurrent.prefabGuid) { if (!nestedDictionary.ContainsKey(nestedSource.prefabGuid)) { nestedDictionary.Add(nestedSource.prefabGuid, nestedSource.gameObject); } else { CustomDebug.LogError("Duplicate GUID : " + nestedSource.gameObject + " + " + nestedDictionary[nestedSource.prefabGuid] + " -> Solution : дать леща"); } } } else { CustomDebug.Log("NestedPrefab component missing"); } } } if (!nestedDictionary.ContainsKey(nestedCurrent.prefabGuid)) { CustomDebug.LogError("can't find prefab for " + nestedCurrent.name); } } } return(nestedDictionary); }
public override void OnInspectorGUI() { base.OnInspectorGUI(); NestedPrefab nestedPrefab = target as NestedPrefab; if (nestedPrefab == null) { return; } nestedPrefab.prefabGenerator = new DefaultPrefabGeneration(); if (GUILayout.Button("Save Nested Prefabs")) { nestedPrefab.SavePrefabData(); } if (GUILayout.Button("Revert Prefabs")) { nestedPrefab.GeneratePrefabs(); } if (nestedPrefab.prefabDetailsVisibility) { EditorGUILayout.Space(); EditorGUILayout.LabelField("List of Nested Prefabs:", EditorStyles.boldLabel); EditorGUILayout.Space(); for (int i = 0; i < nestedPrefab.nestedPrefabsData.Count; i++) { NestedPrefabData prefabData = nestedPrefab.nestedPrefabsData[i]; // GameObject prefabParent = PrefabUtility.GetPrefabParent(prefabData.prefabObject) as GameObject; // string prefabPath = AssetDatabase.GetAssetPath(prefabParent); EditorGUILayout.LabelField("" + i + ". " + prefabData.hierarchyPath + " from " + prefabData.prefabPath); // PrefabUtility.GetPropertyModifications } } }
public static void Bake(NestedPrefab np) { if (!np.Prefab || !np.enabled) { return; } np.enabled = false; //GameObject go = PrefabUtility.InstantiatePrefab(np.Prefab) as GameObject; GameObject go = GameObject.Instantiate(np.Prefab) as GameObject; Quaternion rot = go.transform.localRotation; Vector3 scale = go.transform.localScale; go.transform.parent = np.transform; go.transform.localPosition = Vector3.zero; go.transform.localScale = scale; go.transform.localRotation = rot; np.Prefab = null; foreach (NestedPrefab child in go.GetComponentsInChildren<NestedPrefab>()) { Bake(child); } }
void ChangeGameObject(GameObject obj) { Transform parent = obj.transform.parent; string name = obj.name; Vector3 pos = obj.transform.localPosition; Quaternion rot = obj.transform.localRotation; Vector3 scale = obj.transform.localScale; NestedPrefab prefab = (NestedPrefab)target; DestroyImmediate(obj); GameObject newObj = Instantiate(prefab.gameObject); newObj.name = name; newObj.transform.parent = parent; newObj.transform.localPosition = pos; newObj.transform.localRotation = rot; newObj.transform.localScale = scale; // Remove redundant component. DestroyImmediate(newObj.GetComponent <NestedPrefab>()); }
public void Spawn() { if (instantiated != null || isSpawning) { return; } NestedPrefab rootParent = FindRootNested(this); if (rootParent != null && rootParent != this) { rootParent.Spawn(); return; } isSpawning = true; //Dbg.LogRelease(this, "Spawning from {0}", this); using (var queue = TempList <NestedPrefab> .Get()) using (var previouslyInstantiated = TempList <GameObject> .Get()) { queue.Add(this); while (queue.Count > 0) { NestedPrefab prefab = queue[0]; queue.RemoveAt(0); SpawnInternal( prefab, queue.buffer, previouslyInstantiated.buffer ); } } isSpawning = false; }
static NestedPrefab FindRootNested(NestedPrefab prefabInstance) { NestedPrefab rootParent = prefabInstance; // Find a parent, any will do (that is not us, // GetComponentsInParent includes the same GameObject) using (var parents = TempList <NestedPrefab> .Get()) { while (true) { var thisParent = rootParent; rootParent.GetComponentsInParent <NestedPrefab>( includeInactive: true, results: parents.buffer ); for (int i = parents.Count - 1; i >= 0; --i) { var parent = parents[i]; if (parent != thisParent) { rootParent = parent; break; } } // No new parent found if (rootParent == thisParent) { break; } } } return(rootParent); }
public void Revert() { ReportProgress("", 0); AssetUtility.PreloadAssets("Prefabs", typeof(GameObject)); NestedPrefab[] nestedObjects = Resources.FindObjectsOfTypeAll <NestedPrefab>(); List <NestedPrefab> prefabsForSearch = new List <NestedPrefab>(); foreach (Object subtarget in targets) { NestedPrefab nestedCurrent = (subtarget as NestedPrefab); prefabsForSearch.Add(nestedCurrent); } Dictionary <string, GameObject> nestedDictionary = CollectNestedDictionary(nestedObjects, prefabsForSearch); Dictionary <GameObject, List <GameObject> > subPrefabsDictionary = new Dictionary <GameObject, List <GameObject> >(); foreach (Object subtarget in targets) { NestedPrefab targetNestedPrefab = (subtarget as NestedPrefab); GameObject nestedPrefabForReplace = targetNestedPrefab.gameObject; GameObject prefab = PrefabUtility.FindPrefabRoot(nestedPrefabForReplace); List <GameObject> prefabChilds = null; if (!subPrefabsDictionary.ContainsKey(prefab)) { prefabChilds = new List <GameObject>(); subPrefabsDictionary.Add(prefab, prefabChilds); } else { prefabChilds = subPrefabsDictionary[prefab]; } prefabChilds.Add(nestedPrefabForReplace); } float progress = 0; float prefabPercent = 1f / subPrefabsDictionary.Keys.Count; foreach (GameObject prefab in subPrefabsDictionary.Keys) { List <GameObject> prefabChilds = subPrefabsDictionary[prefab]; if (PrefabUtility.GetPrefabType(prefab) == PrefabType.PrefabInstance) { float childProgress = 1f / prefabChilds.Count; foreach (GameObject child in prefabChilds) { progress += childProgress * prefabPercent; ReportProgress("Updated " + child.name + " in " + prefab.name, progress); CustomDebug.Log("Updated " + child.name + " in " + prefab.name); NestedPrefab targetNestedPrefab = child.GetComponent <NestedPrefab>(); if (nestedDictionary.ContainsKey(targetNestedPrefab.prefabGuid)) { ReplaceGameobject(child, nestedDictionary[targetNestedPrefab.prefabGuid]); } DestroyImmediate(child); } } else if (PrefabUtility.GetPrefabType(prefab) == PrefabType.Prefab) { List <int> indexSet = new List <int>(); foreach (GameObject childPrefab in prefabChilds) { for (int i = 0; i < prefab.transform.childCount; i++) { GameObject child = prefab.transform.GetChild(i).gameObject; if (child == childPrefab) { indexSet.Add(i); } } } GameObject newInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; float childProgress = 1f / indexSet.Count; for (int i = 0; i < indexSet.Count; i++) { GameObject childInstance = newInstance.transform.GetChild(indexSet[i]).gameObject; NestedPrefab targetNestedPrefab = prefabChilds[i].GetComponent <NestedPrefab>(); ReplaceGameobject(childInstance, nestedDictionary[targetNestedPrefab.prefabGuid]); DestroyImmediate(childInstance); progress += childProgress * prefabPercent; ReportProgress("Updated " + childInstance.name + " in " + prefab.name, progress); CustomDebug.Log("Updated " + childInstance.name + " in " + prefab.name); } PrefabUtility.ReplacePrefab(newInstance, prefab); DestroyImmediate(newInstance); } else { CustomDebug.Log(prefab.name + " : prefab is broken"); } } EditorUtility.ClearProgressBar(); }
public void UpdatePrefabs() { ReportProgress("", 0); AssetUtility.PreloadAssets("Prefabs", typeof(GameObject)); NestedPrefab[] nestedObjects = Resources.FindObjectsOfTypeAll <NestedPrefab>(); List <GameObject> modifiedPrefabs = new List <GameObject>(); Dictionary <string, GameObject> nestedDictionary = new Dictionary <string, GameObject>(); foreach (Object subtarget in targets) { GameObject gameobject = (subtarget as NestedPrefab).gameObject; //find prefab in assets and check GameObject sourcePrefab = PrefabUtility.GetPrefabType(gameobject) == PrefabType.Prefab ? gameobject : (PrefabUtility.GetPrefabParent(gameobject) as GameObject); if (sourcePrefab == null) { CustomDebug.Log("SourcePrefab missing"); return; } NestedPrefab nestedSource = sourcePrefab.GetComponent <NestedPrefab>(); if (nestedSource == null) { CustomDebug.Log("NestedPrefab missing"); return; } nestedDictionary.Add(nestedSource.prefabGuid, sourcePrefab); ReportProgress("Find Prefabs", 0); //find all prefabs with nested prefab foreach (NestedPrefab nestedTarget in nestedObjects) { if (PrefabUtility.GetPrefabType(nestedTarget.gameObject) == PrefabType.Prefab) { if (string.Equals(nestedTarget.prefabGuid, nestedSource.prefabGuid)) { GameObject findedPrefab = PrefabUtility.FindPrefabRoot(nestedTarget.gameObject); if (findedPrefab != sourcePrefab) { if (!modifiedPrefabs.Contains(findedPrefab)) { ReportProgress("Find Prefabs : " + findedPrefab.name, 0); modifiedPrefabs.Add(findedPrefab); } // CustomDebug.Log(AssetDatabase.GetAssetPath(findedPrefab)); } } } } } float progress = 0; float prefabPercent = 1f / modifiedPrefabs.Count; List <GameObject> objectsForRemove = new List <GameObject>(); for (int index = 0; index < modifiedPrefabs.Count; index++) { int i = index; System.Action action = () => { GameObject prefab = modifiedPrefabs[i]; GameObject newInstance = PrefabUtility.InstantiatePrefab(prefab) as GameObject; ReportProgress("Updating : " + prefab.name, progress); NestedPrefab[] children = newInstance.GetComponentsInChildren <NestedPrefab>(); float childProgress = 1f / children.Length; foreach (NestedPrefab childForReplace in children) { if (nestedDictionary.ContainsKey(childForReplace.prefabGuid)) { GameObject nestedSource = nestedDictionary[childForReplace.prefabGuid]; ReportProgress("Updated " + nestedSource.name + " in " + prefab.name, progress); CustomDebug.Log("Updated " + nestedSource.name + " in " + prefab.name); objectsForRemove.Add(childForReplace.gameObject); ReplaceGameobject(childForReplace.gameObject, nestedSource); } progress += childProgress * prefabPercent; } objectsForRemove.ForEach(DestroyImmediate); objectsForRemove.Clear(); PrefabUtility.ReplacePrefab(newInstance, prefab); DestroyImmediate(newInstance); }; actions.Add(action); } actions.Add(EditorUtility.ClearProgressBar); }
void CopyTagFromPrefab(GameObject obj) { NestedPrefab prefab = (NestedPrefab)target; obj.tag = prefab.tag; }