// ----------------------------------------------------------------------------
        // fix missing reference
        // ----------------------------------------------------------------------------

        public static FixResult FixMissingReference(Object unityObject, string propertyPath, RecordLocation location)
        {
            var so = new SerializedObject(unityObject);
            var sp = so.FindProperty(propertyPath);

            if (MissingReferenceDetector.IsPropertyHasMissingReference(sp))
            {
                sp.objectReferenceInstanceIDValue = 0;

                var fileId = sp.FindPropertyRelative("m_FileID");
                if (fileId != null)
                {
                    fileId.intValue = 0;
                }

                // fixes dirty scene flag after batch issues fix
                // due to the additional undo action
                so.ApplyModifiedPropertiesWithoutUndo();

                if (location == RecordLocation.Scene)
                {
                    CSSceneTools.MarkSceneDirty();
                }
                else
                {
                    if (unityObject != null)
                    {
                        EditorUtility.SetDirty(unityObject);
                    }
                }
            }

            return(new FixResult(true));
        }
Exemple #2
0
        public static List <IssueRecord> CheckSceneSettingsForMissingReferences(AssetInfo sceneAsset)
        {
            var result = new List <IssueRecord>();

            var sceneSettingsObject = CSSettingsTools.GetInSceneLightmapSettings();

            if (sceneSettingsObject != null)
            {
                var initialInfo = new SerializedObjectTraverseInfo(sceneSettingsObject);
                CSTraverseTools.TraverseObjectProperties(initialInfo, (info, property) =>
                {
                    if (MissingReferenceDetector.IsPropertyHasMissingReference(property))
                    {
                        var record = SceneSettingsIssueRecord.Create(SceneSettingsKind.LightmapSettings, IssueKind.MissingReference, sceneAsset.Path, property.propertyPath);
                        result.Add(record);
                    }
                });
            }

            sceneSettingsObject = CSSettingsTools.GetInSceneRenderSettings();
            if (sceneSettingsObject != null)
            {
                var initialInfo = new SerializedObjectTraverseInfo(sceneSettingsObject);
                CSTraverseTools.TraverseObjectProperties(initialInfo, (info, property) =>
                {
                    if (MissingReferenceDetector.IsPropertyHasMissingReference(property))
                    {
                        var record = SceneSettingsIssueRecord.Create(SceneSettingsKind.RenderSettings, IssueKind.MissingReference, sceneAsset.Path, property.propertyPath);
                        result.Add(record);
                    }
                });
            }

            return(result);
        }
Exemple #3
0
        public static void Init(List <IssueRecord> issues)
        {
            missingReferenceDetector = new MissingReferenceDetector(issues);

            missingComponentDetector        = new MissingComponentDetector(issues);
            duplicateComponentDetector      = new DuplicateComponentDetector(issues);
            inconsistentTerrainDataDetector = new InconsistentTerrainDataDetector(issues);
            missingPrefabDetector           = new MissingPrefabDetector(issues);

            hugePositionDetector   = new HugePositionDetector(issues);
            emptyLayerNameDetector = new EmptyLayerNameDetector(issues);

            duplicateLayersDetector = new DuplicateLayersDetector(issues);
#if UNITY_2019_1_OR_NEWER
            shaderErrorDetector = new ShaderErrorDetector(issues);
#endif
        }
Exemple #4
0
        public static List <IssueRecord> CheckSettingsAssetForMissingReferences(AssetInfo asset, AssetSettingsKind kind)
        {
            var result = new List <IssueRecord>();

            // include only supported settings files with object references

            if (kind != AssetSettingsKind.EditorSettings &&
                kind != AssetSettingsKind.GraphicsSettings &&
                kind != AssetSettingsKind.DynamicsManager &&
                kind != AssetSettingsKind.Physics2DSettings &&
                kind != AssetSettingsKind.PresetManager &&
                kind != AssetSettingsKind.VFXManager)
            {
                return(result);
            }

            var allAssets = AssetDatabase.LoadAllAssetsAtPath(asset.Path);

            if (allAssets == null || allAssets.Length <= 0)
            {
                return(result);
            }

            foreach (var assetObject in allAssets)
            {
                if (assetObject == null)
                {
                    return(result);
                }

                var traverseInfo = new SerializedObjectTraverseInfo(assetObject);

                CSTraverseTools.TraverseObjectProperties(traverseInfo, (info, property) =>
                {
                    if (MissingReferenceDetector.IsPropertyHasMissingReference(property))
                    {
                        var issue = SettingsIssueRecord.Create(kind, IssueKind.MissingReference, asset.Path,
                                                               property.propertyPath);
                        result.Add(issue);
                    }
                });
            }

            return(result);
        }
        // ----------------------------------------------------------------------------
        // 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);
        }
Exemple #6
0
        // -----------------------------------------------------------------------------
        // 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);
        }