private static void UpdateNewlyCreatedPrefabs() { //Check if new prefab created and update possible instance to connect PrefabLink target PrefabLink prefab = lastSelectedPrefab.GetComponent <PrefabLink>(); PrefabLink prefabInstance = null; if (lastSelectedGameObject != null) { prefabInstance = lastSelectedGameObject.GetComponent <PrefabLink>(); } else { prefabInstance = GetPrefabInstance(prefab); } if (prefab != null && prefabInstance != null) { prefabInstance.Target = prefab.gameObject; prefab.Target = prefab.gameObject; } lastSelectedPrefab = null; lastSelectedGameObject = null; if (ExtensionMethods.ExtensionMethods.masterVerbose) { Debug.Log("PrefabLink: New prefab (" + prefab.name + ") created from gameObject (" + prefabInstance.gameObject + ")."); } }
public bool Apply(bool applyChildren = true, bool ignoreTopTransform = true, bool ignorePrefabLink = false) { GameObject from = Target; GameObject to = gameObject; if (applyChildren) { foreach (PrefabLink directChildprefabLink in DirectChildPrefabLinks(to)) { directChildprefabLink.Apply(applyChildren, false, ignorePrefabLink); } } GameObject updatedFrom = Apply(from, to, ignoreTopTransform, ignorePrefabLink); PrefabLink toPrefabLink = to.GetComponent <PrefabLink>(); if (toPrefabLink != null) { toPrefabLink.Target = updatedFrom; } if (updatedFrom != null) { PrefabLink fromPrefabLink = updatedFrom.GetComponent <PrefabLink>(); if (fromPrefabLink != null) { fromPrefabLink.Target = updatedFrom; } } return(true); }
private static PrefabLink GetPrefabInstance(PrefabLink prefabLink) { PrefabLink[] allPrefabLinksInScene = FindObjectsOfType <PrefabLink>(); foreach (PrefabLink prefabLinkInSceen in allPrefabLinksInScene) { if (prefabLink.gameObject == PrefabUtility.GetPrefabParent(prefabLinkInSceen.gameObject)) { return(prefabLinkInSceen); } } return(null); }
public override bool Equals(object other) { bool equals = false; if (base.Equals(other)) { equals = true; } else if (other is PrefabLink) { PrefabLink otherPrefabLink = other as PrefabLink; equals = Target == otherPrefabLink.Target; } return(equals); }
private List <PrefabLink> DirectChildPrefabLinks(GameObject gameObject) { List <PrefabLink> directChildPrefabLinks = new List <PrefabLink> { }; foreach (Transform child in gameObject.transform) { PrefabLink directChildPrefabLink = child.gameObject.GetComponent <PrefabLink>(); bool isPrefabLink = directChildPrefabLink != null; if (isPrefabLink) { directChildPrefabLinks.Add(directChildPrefabLink); } else { directChildPrefabLinks.AddRange(DirectChildPrefabLinks(child.gameObject)); } } return(directChildPrefabLinks); }
static void UpdatePrefabInstance(PrefabLink prefabLinkInSceen) { object parentObj = PrefabUtility.GetPrefabParent(prefabLinkInSceen); if (parentObj != null) { GameObject parentGameObject = parentObj as GameObject; if (parentGameObject != null) { List <GameObject> instances = new List <GameObject> { }; if (prefabInstances.ContainsKey(parentGameObject)) { instances = prefabInstances[parentGameObject]; } instances.AddUnique(prefabLinkInSceen.gameObject); prefabInstances[parentGameObject] = instances; } } }
private static void HierarchyWindowChanged() { BuildPrefabInstances(); //Check if prefab instance was just created GameObject activeGameObject = Selection.activeGameObject; if (activeGameObject != null) { if (PrefabUtility.GetPrefabType(activeGameObject) == PrefabType.PrefabInstance) { PrefabLink prefabLink = activeGameObject.GetComponent <PrefabLink>(); if (prefabLink != null) { GameObject prefab = PrefabUtility.GetPrefabParent(prefabLink.gameObject) as GameObject; PrefabUtility.DisconnectPrefabInstance(prefabLink.gameObject); prefabLink.Target = prefab; } } } }
private static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect) { UnityEngine.Object obj = EditorUtility.InstanceIDToObject(instanceID); if (obj != null) { if (obj is GameObject) { GameObject gameObject = obj as GameObject; PrefabLink prefabLink = gameObject.gameObject.GetComponent <PrefabLink>(); if (prefabLink != null) { FontStyle prefabLinkFontStyle = FontStyle.Normal; Color prefabLinkColor = textEditorBlack; Color backgroundColor = editorGray; float timeSinceStartup = (float)EditorApplication.timeSinceStartup; if (timeSinceStartup - prefabLink.updateDirtyStartTime > 1 / PrefabLink.dirtyChecksPerSecond) { prefabLink.UpdateDirty(); prefabLink.updateDirtyStartTime = timeSinceStartup; } bool selected = Selection.instanceIDs.Contains(instanceID); bool disabled = !gameObject.activeInHierarchy; bool noTarget = prefabLink.target == null; bool dirty = prefabLink.Dirty; if (dirty || noTarget) { prefabLinkFontStyle = FontStyle.Bold; } if (selected) { backgroundColor = selectedBackgroundBlue; } if (noTarget) { if (selected) { prefabLinkColor = textNoTargetSelected; } else if (disabled) { prefabLinkColor = textNoTargetDisabled; } else { prefabLinkColor = textNoTargetDefault; } } else { if (selected) { prefabLinkColor = textNormalSelected; } else if (disabled) { prefabLinkColor = textNormalDisabled; } else { prefabLinkColor = textNormalDefault; } } Rect offsetRect = new Rect(selectionRect.position + new Vector2(0, 2), selectionRect.size); EditorGUI.DrawRect(selectionRect, backgroundColor); EditorGUI.LabelField(offsetRect, obj.name, new GUIStyle() { normal = new GUIStyleState() { textColor = prefabLinkColor }, fontStyle = prefabLinkFontStyle }); } } } }
private void Update() { EditorApplication.update -= Update; if (triggerRevert || triggerRevertHierarchy) { foreach (PrefabLink prefabLink in PrefabLinks) { if (PrefabLink.useUnityEditorRevert || prefabLink.gameObject.IsPrefab()) { PrefabUtility.ResetToPrefabState(prefabLink); } else { Undo.RegisterFullObjectHierarchyUndo(prefabLink, "Prefab Link Revert"); prefabLink.StartTime = (float)EditorApplication.timeSinceStartup; prefabLink.Revert(triggerRevertHierarchy); if (prefabLink != null) { EditorUtility.SetDirty(prefabLink); } } } } if (triggerRevertAllInstances) { BuildPrefabInstances(); foreach (PrefabLink prefabLink in PrefabLinks) { foreach (GameObject instance in prefabInstances[prefabLink.gameObject]) { if (PrefabLink.useUnityEditorRevert) { PrefabUtility.ResetToPrefabState(instance); } else { PrefabLink prefabLinkInstance = instance.GetComponent <PrefabLink>(); if (prefabLinkInstance != null) { Undo.RegisterFullObjectHierarchyUndo(prefabLinkInstance, "Prefab Link Revert"); prefabLinkInstance.StartTime = (float)EditorApplication.timeSinceStartup; prefabLinkInstance.Revert(triggerRevertHierarchy); EditorUtility.SetDirty(prefabLinkInstance); } } } } } if (triggerApply || triggerApplyAll) { foreach (PrefabLink prefabLink in PrefabLinks) { if (PrefabLink.useUnityEditorApply) { //Undo for applying prefabse is broken //https://issuetracker.unity3d.com/issues/reverting-changes-on-applied-prefab-crashes-unity if (prefabLink.Target == null) { Debug.LogWarning("Cannot apply changes to a null prefab target."); } else if (!prefabLink.Target.IsPrefab()) { Debug.LogWarning("Cannot use UnityEditorApply on a scene object. Only Unity prefab are allowed."); } else { GameObject newPrefab = PrefabUtility.ReplacePrefab(prefabLink.gameObject, prefabLink.Target); prefabLink.Target = newPrefab; } } else { if (prefabLink.Target != null) { Undo.RegisterFullObjectHierarchyUndo(prefabLink.Target, "Prefab Link Apply"); } prefabLink.Apply(triggerApplyAll); if (prefabLink.Target != null) { EditorUtility.SetDirty(prefabLink.Target); } } } } Undo.FlushUndoRecordObjects(); triggerRevert = false; triggerRevertHierarchy = false; triggerApply = false; triggerApplyAll = false; triggerRevertAllInstances = false; }