// ---------------------------------------------------------------------------- // fix missing component // ---------------------------------------------------------------------------- private static void FixMissingComponents(GameObjectIssueRecord issue, GameObject go) { CSObjectTools.SelectGameObject(go, issue.location); ActiveEditorTracker tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject(); tracker.RebuildIfNecessary(); bool touched = false; Editor[] activeEditors = tracker.activeEditors; for (int i = activeEditors.Length - 1; i >= 0; i--) { Editor editor = activeEditors[i]; if (CSObjectTools.GetLocalIdentifierInFileForObject(editor.serializedObject.targetObject) == issue.componentId) { Object.DestroyImmediate(editor.target, true); touched = true; } } if (touched) { #if UNITY_5_0_PLUS if (issue.location == RecordLocation.Scene) { CSSceneTools.MarkSceneDirty(); } else { EditorUtility.SetDirty(go); } #else EditorUtility.SetDirty(go); #endif } //CSObjectTools.SelectGameObject(null, issue.location); }
// ----------------------------------------------------------------------------- // fix missing component // ----------------------------------------------------------------------------- private static bool FixMissingComponents(GameObjectIssueRecord issue, GameObject go, bool alternative) { var touched = false; // TODO: re-check in Unity 2021 // unfortunately RemoveMonoBehavioursWithMissingScript does not works correctly: // https://forum.unity.com/threads/remove-all-missing-components-in-prefabs.897761/ // so it will be enabled back in later Unity versions /*var removedCount = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go); * if (removedCount > 0) * { * touched = true; * }*/ if (!alternative) { CSObjectTools.SelectGameObject(go, issue.Location == RecordLocation.Scene); } var tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject(); if (tracker == null) { Debug.LogError(Maintainer.ConstructError("Can't get active tracker.")); return(false); } tracker.RebuildIfNecessary(); var activeEditors = tracker.activeEditors; for (var i = activeEditors.Length - 1; i >= 0; i--) { var editor = activeEditors[i]; if (editor.serializedObject.targetObject == null) { Object.DestroyImmediate(editor.target, true); touched = true; } } if (alternative) { return(touched); } if (!touched) { // missing script could be hidden with hide flags, so let's try select it directly and repeat var serializedObject = new SerializedObject(go); var componentsArray = serializedObject.FindProperty("m_Component"); if (componentsArray != null) { for (var i = componentsArray.arraySize - 1; i >= 0; i--) { var componentPair = componentsArray.GetArrayElementAtIndex(i); var nestedComponent = componentPair.FindPropertyRelative("component"); if (nestedComponent != null) { if (MissingReferenceDetector.IsPropertyHasMissingReference(nestedComponent)) { var instanceId = nestedComponent.objectReferenceInstanceIDValue; if (instanceId == 0) { var fileId = nestedComponent.FindPropertyRelative("m_FileID"); if (fileId != null) { instanceId = fileId.intValue; } } Selection.instanceIDs = new [] { instanceId }; touched |= FixMissingComponents(issue, go, true); } } else { Debug.LogError(Maintainer.LogPrefix + "Couldn't find component in component pair!"); break; } } if (touched) { CSObjectTools.SelectGameObject(go, issue.Location == RecordLocation.Scene); } } else { Debug.LogError(Maintainer.LogPrefix + "Couldn't find components array!"); } } if (touched) { if (issue.Location == RecordLocation.Scene) { CSSceneTools.MarkSceneDirty(); } else { EditorUtility.SetDirty(go); } } return(touched); }
// ---------------------------------------------------------------------------- // fix missing component // ---------------------------------------------------------------------------- private static bool FixMissingComponents(GameObjectIssueRecord issue, GameObject go, bool alternative) { var touched = false; #if UNITY_2019_1_OR_NEWER var removedCount = GameObjectUtility.RemoveMonoBehavioursWithMissingScript(go); if (removedCount > 0) { touched = true; } #else if (!alternative) { CSObjectTools.SelectGameObject(go, issue.Location == RecordLocation.Scene); } var tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject(); if (tracker == null) { Debug.LogError(Maintainer.ConstructError("Can't get active tracker.")); return(false); } tracker.RebuildIfNecessary(); var activeEditors = tracker.activeEditors; for (var i = activeEditors.Length - 1; i >= 0; i--) { var editor = activeEditors[i]; if (editor.serializedObject.targetObject == null) { Object.DestroyImmediate(editor.target, true); touched = true; } } if (alternative) { return(touched); } if (!touched) { // missing script could be hidden with hide flags, so let's try select it directly and repeat var serializedObject = new SerializedObject(go); var componentsArray = serializedObject.FindProperty("m_Component"); if (componentsArray != null) { for (var i = componentsArray.arraySize - 1; i >= 0; i--) { var componentPair = componentsArray.GetArrayElementAtIndex(i); var nestedComponent = componentPair.FindPropertyRelative("component"); if (nestedComponent != null) { if (MissingReferenceDetector.IsPropertyHasMissingReference(nestedComponent)) { var instanceId = nestedComponent.objectReferenceInstanceIDValue; if (instanceId == 0) { var fileId = nestedComponent.FindPropertyRelative("m_FileID"); if (fileId != null) { instanceId = fileId.intValue; } } Selection.instanceIDs = new [] { instanceId }; touched |= FixMissingComponents(issue, go, true); } } else { Debug.LogError(Maintainer.LogPrefix + "Couldn't find component in component pair!"); break; } } if (touched) { CSObjectTools.SelectGameObject(go, issue.Location == RecordLocation.Scene); } } else { Debug.LogError(Maintainer.LogPrefix + "Couldn't find components array!"); } } #endif if (touched) { if (issue.Location == RecordLocation.Scene) { CSSceneTools.MarkSceneDirty(); } else { EditorUtility.SetDirty(go); } } return(touched); }
public void Show() { GameObject go = null; if (OpenNeededSceneIfNecessary(true)) { go = GetGameObjectWithThisIssue(); } if (go != null) { CSObjectTools.SelectGameObject(go, location); if (location == RecordLocation.Scene) { EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(path, typeof(Object))); }; } else { if (gameObjectPath.Split('/').Length > 2) { EditorApplication.delayCall += () => { EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(path, typeof(Object))); }; } } ActiveEditorTracker tracker = CSEditorTools.GetActiveEditorTrackerForSelectedObject(); tracker.RebuildIfNecessary(); Editor[] editors = tracker.activeEditors; long[] ids = new long[editors.Length]; bool targetFound = false; for (int i = 0; i < editors.Length; i++) { Editor editor = editors[i]; long id = CSObjectTools.GetLocalIdentifierInFileForObject(editor.serializedObject.targetObject); ids[i] = id; if (id == componentId) { targetFound = true; /* known corner cases when editor can't be set to visible via tracker */ if (editor.serializedObject.targetObject is ParticleSystemRenderer) { ParticleSystemRenderer renderer = (ParticleSystemRenderer)editor.serializedObject.targetObject; ParticleSystem ps = renderer.GetComponent <ParticleSystem>(); componentId = CSObjectTools.GetLocalIdentifierInFileForObject(ps); } } } if (targetFound) { for (int i = 0; i < editors.Length; i++) { tracker.SetVisible(i, ids[i] != componentId ? 0 : 1); } } } else { MaintainerWindow.ShowNotification("Couldn't find object " + gameObjectPath); } }