Ejemplo n.º 1
0
        public void     FixMissingComponent(NGMissingScriptRecoveryWindow window, string componentID, Type type)
        {
            TypeIdentifiers identifiers = MetadataDatabase.GetIdentifiers(type);

            if (identifiers != null)
            {
                this.FixLine(window, componentID, type, "  m_Script: {fileID: " + identifiers.localIdentifier + ", guid: " + identifiers.guid + ", type: 3}");
                return;
            }

            GameObject tempPrefab = new GameObject("MissingComponentRecovery", type);

            PrefabUtility.CreatePrefab(NGMissingScriptRecoveryWindow.TempPrefabPath, tempPrefab, ReplacePrefabOptions.ConnectToPrefab);

            if (File.Exists(NGMissingScriptRecoveryWindow.TempPrefabPath) == true)
            {
                AssetDatabase.SaveAssets();
                this.FixLine(window, componentID, type);
            }

            Object.DestroyImmediate(tempPrefab);
            AssetDatabase.DeleteAsset(NGMissingScriptRecoveryWindow.TempPrefabPath);

            Selection.activeGameObject = null;

            // Select again few updates after, to avoid Inspector to crash.
            EditorApplication.delayCall += () => EditorApplication.delayCall += () => Selection.activeGameObject = window.Target;
        }
Ejemplo n.º 2
0
        public override void    OnInspectorGUI()
        {
            if (this.isNull == true)
            {
                EditorGUILayout.HelpBox("The associated script can not be loaded.\nPlease fix any compile errors\nand assign a valid script.", MessageType.Warning);

                if (this.isPrefab == true)
                {
                    if (GUILayout.Button("Recover Missing Component") == true)
                    {
                        NGMissingScriptRecoveryWindow instance = EditorWindow.GetWindow <NGMissingScriptRecoveryWindow>(true, NGMissingScriptRecoveryWindow.NormalTitle);

                        if (instance.IsRecovering == false)
                        {
                            instance.Diagnose(Selection.activeGameObject);
                            instance.tab = NGMissingScriptRecoveryWindow.Tab.Selection;
                            instance.Show();
                        }
                        else
                        {
                            EditorUtility.DisplayDialog(NGMissingScriptRecoveryWindow.NormalTitle, "A recovery process is still running.", "OK");
                        }
                    }
                }
                else
                {
                    EditorGUILayout.HelpBox(NGMissingScriptRecoveryWindow.NormalTitle + " can only recover from prefabs.\nCreate a prefab of your broken asset and recover from it.", MessageType.Warning);
                }
            }
            else
            {
                base.OnInspectorGUI();
            }
        }
Ejemplo n.º 3
0
        private static void     Diagnose(MenuCommand command)
        {
            // We assume that the context menu is opened from the active GameObject.
            PrefabType prefabType = PrefabUtility.GetPrefabType(Selection.activeGameObject);
            Object     prefab     = null;

            if (prefabType == PrefabType.Prefab)
            {
                prefab = Selection.activeGameObject;
            }
            else if (prefabType == PrefabType.PrefabInstance)
            {
                prefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject);
            }
            else if (prefabType == PrefabType.DisconnectedPrefabInstance)
            {
                prefab = PrefabUtility.GetPrefabParent(Selection.activeGameObject);
            }

            if (prefab == null)
            {
                EditorUtility.DisplayDialog(NGMissingScriptRecoveryWindow.NormalTitle, "Recover a missing script is only possible on prefab.\n\nYou may temporary create a prefab, recover and then destroy the prefab.", "OK");
                return;
            }

            NGMissingScriptRecoveryWindow instance = EditorWindow.GetWindow <NGMissingScriptRecoveryWindow>(true, NGMissingScriptRecoveryWindow.ShortTitle);

            if (instance.isRecovering == false)
            {
                instance.Diagnose(Selection.activeGameObject);
                instance.tab = Tab.Selection;
                instance.Show();
            }
            else
            {
                EditorUtility.DisplayDialog(NGMissingScriptRecoveryWindow.NormalTitle, "A recovery process is still running.", "OK");
            }
        }
Ejemplo n.º 4
0
        public void     FixLine(NGMissingScriptRecoveryWindow window, string componentID, Type type, string fixedLine = null)
        {
            string prefabPath = AssetDatabase.GetAssetPath(window.Target);

            string[] lines = File.ReadAllLines(prefabPath);

            for (int m = 0; m < lines.Length; m++)
            {
                if (lines[m].Length > 12 && lines[m][0] == '-' && lines[m][2] == '-' && lines[m].EndsWith(componentID) == true)
                {
                    for (; m < lines.Length; m++)
                    {
                        if (lines[m].StartsWith("  m_Script") == true)
                        {
                            if (fixedLine == null)
                            {
                                CachedLineFix lineFix = window.FindCachedComponentFixes(lines[m]);

                                // Get the cached line only if the Type matches.
                                if (lineFix != null && lineFix.type == type)
                                {
                                    fixedLine = lineFix.fixedLine;
                                }
                                else
                                {
                                    fixedLine = this.ExtractFixGUIDFromTempPrefab();

                                    if (string.IsNullOrEmpty(fixedLine) == true)
                                    {
                                        InternalNGDebug.LogError("Impossible to extract new GUID from temp prefab. Recovery aborted.");
                                        return;
                                    }

                                    if (lineFix != null)
                                    {
                                        lineFix.fixedLine = fixedLine;
                                        lineFix.type      = type;
                                    }
                                    else
                                    {
                                        window.AddCachedComponentFixes(new CachedLineFix()
                                        {
                                            brokenLine = lines[m], fixedLine = fixedLine, type = type
                                        });
                                    }
                                }
                            }

                            lines[m] = fixedLine;

                            try
                            {
                                File.WriteAllLines(prefabPath, lines);
                                AssetDatabase.Refresh();
                            }
                            catch (IOException ex)
                            {
                                InternalNGDebug.LogException("Recovering \"" + prefabPath + "\" failed. File seems to be locked. Please try to recover again or restart Unity or your computer.", ex);
                            }
                            catch (Exception ex)
                            {
                                InternalNGDebug.LogException("Recovering \"" + prefabPath + "\" failed. Please try to recover again.", ex);
                            }

                            break;
                        }
                    }

                    break;
                }
            }
        }
Ejemplo n.º 5
0
        public void     Draw(NGMissingScriptRecoveryWindow window, bool force = false)
        {
            if (this.cachedFields == null)
            {
                StringBuilder buffer = Utility.GetBuffer();

                for (int i = 0; i < this.fields.Count; i++)
                {
                    buffer.AppendLine(this.fields[i]);
                }

                if (buffer.Length > 0)
                {
                    buffer.Length -= Environment.NewLine.Length;
                }

                this.cachedFields = Utility.ReturnBuffer(buffer);
            }

            EditorGUI.BeginDisabledGroup(this.fields.Count <= 0);
            {
                Utility.content.text  = this.name;
                Utility.content.image = Utility.GetIcon(this.asset ? this.asset.GetInstanceID() : 0);
                this.open             = EditorGUILayout.Foldout(this.open, Utility.content) && this.fields.Count > 0 || force;
                Utility.content.image = null;
            }
            EditorGUI.EndDisabledGroup();

            if (this.fields.Count == 0 && this.asset == null)
            {
                EditorGUILayout.HelpBox("Component seems to have absolutely no fields. Recovery can not proceed.", MessageType.Warning);
            }

            if (this.open == false)
            {
                return;
            }

            ++EditorGUI.indentLevel;
            this.fieldsOpen = EditorGUILayout.Foldout(this.fieldsOpen, "Fields found (" + this.fields.Count + ")");
            if (this.fieldsOpen == true)
            {
                ++EditorGUI.indentLevel;
                Utility.content.text = this.cachedFields;
                float h = EditorStyles.label.CalcHeight(Utility.content, window.position.width - 60F);
                //float h = this.fields.Count * 15F;
                float totalH = h;

                if (h >= NGMissingScriptRecoveryWindow.MaxFieldsHeight)
                {
                    h = NGMissingScriptRecoveryWindow.MaxFieldsHeight;
                }

                //Utility.content.text = this.cachedFields;
                //Rect	viewRect = new Rect();
                //Rect	body = new Rect(4F, GUILayoutUtility.GetLastRect().yMax, window.position.width, 0F);
                //Rect	r2 = GUILayoutUtility.GetRect(0F, h);
                //body.height = r2.height;
                //viewRect.height = EditorStyles.label.CalcSize(Utility.content).y;

                //EditorGUI.DrawRect(body, Color.blue);
                //this.scrollPositionFields = GUI.BeginScrollView(body, this.scrollPositionFields, viewRect);
                //{
                //	Rect	selectableRect = body;
                //	selectableRect.x = 0F;
                //	selectableRect.y = 0F;
                //	selectableRect.width = body.width - (viewRect.height > body.height ? 16F : 0F);
                //	selectableRect.height = viewRect.height > body.height ? EditorStyles.label.CalcHeight(Utility.content, selectableRect.width) : viewRect.height;
                //	EditorGUI.SelectableLabel(selectableRect, this.cachedFields, EditorStyles.label);
                //}
                //GUI.EndScrollView();

                // TODO Fix bottom margin/padding?
                EditorGUI.HelpBox(new Rect(30F, GUILayoutUtility.GetLastRect().yMax, window.position.width - 60F, h + 10F), string.Empty, MessageType.None);
                //if (useScrollbar == true)
                this.scrollPositionFields = EditorGUILayout.BeginScrollView(this.scrollPositionFields, GUILayoutOptionPool.Height(h + 5F), GUILayoutOptionPool.Width(window.position.width - 30F));
                //else
                //EditorGUI.HelpBox(new Rect(4F, GUILayoutUtility.GetLastRect().yMax, window.position.width, h), string.Empty, MessageType.None);
                {
                    EditorGUILayout.SelectableLabel(this.cachedFields, EditorStyles.label, GUILayoutOptionPool.Height(totalH));
                    //for (int j = 0; j < this.fields.Count; j++)
                    //	EditorGUILayout.LabelField(this.fields[j]);
                }
                //if (useScrollbar == true)
                EditorGUILayout.EndScrollView();
                --EditorGUI.indentLevel;
            }

            CachedLineFix lineFix = window.FindCachedComponentFixes(this.line);

            if (lineFix != null)
            {
                if (GUILayout.Button("Recover From Cache (" + lineFix.type + ")") == true)
                {
                    this.FixLine(window, this.componentID, lineFix.type);
                    window.Diagnose(window.Target);
                }
            }

            EditorGUILayout.LabelField("Potential types:");
            ++EditorGUI.indentLevel;
            if (this.potentialTypes.Count == 0)
            {
                EditorGUILayout.LabelField("No type available.");
            }
            else
            {
                for (int l = 0; l < this.potentialTypes.Count; l++)
                {
                    EditorGUILayout.BeginHorizontal();
                    {
                        GUILayout.Space(30F);

                        if (GUILayout.Button(this.potentialTypes[l].type.FullName, GUILayoutOptionPool.ExpandWidthFalse) == true)
                        {
                            this.FixMissingComponent(window, this.componentID, this.potentialTypes[l].type);
                            window.Diagnose(window.Target);
                            return;
                        }

                        if (this.potentialTypes[l].matchingFields == this.fields.Count &&
                            this.potentialTypes[l].fields.Length == this.fields.Count)
                        {
                            GUILayout.Label("(Perfect match)", GUILayoutOptionPool.ExpandWidthFalse);
                        }
                        else
                        {
                            int extraFields = this.potentialTypes[l].fields.Length == this.potentialTypes[l].matchingFields ? this.potentialTypes[l].matchingFields - this.fields.Count : this.potentialTypes[l].fields.Length - this.potentialTypes[l].matchingFields;
                            GUILayout.Label("(Fields: " + this.potentialTypes[l].matchingFields + " matching" + (extraFields > 0 ? ", +" + extraFields + " extra" : (extraFields < 0 ? ", " + extraFields + " missing" : string.Empty)) + ")", GUILayoutOptionPool.ExpandWidthFalse);
                        }

                        if (Event.current.type == EventType.MouseMove)
                        {
                            Rect r = GUILayoutUtility.GetLastRect();

                            r.xMin = 0F;
                            if (r.Contains(Event.current.mousePosition) == true)
                            {
                                if (this.hoverPopup != null)
                                {
                                    if (this.hoverPopup.potentialType.type != this.potentialTypes[l].type)
                                    {
                                        if (this.hoverPopup.editorWindow != null)
                                        {
                                            this.hoverPopup.editorWindow.Close();
                                        }
                                        this.hoverPopup = new HighlightMatchesPopup(this, this.potentialTypes[l]);
                                        r.x             = 30F;
                                        r.y            += 5F;
                                        PopupWindow.Show(r, this.hoverPopup);
                                    }
                                }
                                else
                                {
                                    this.hoverPopup = new HighlightMatchesPopup(this, this.potentialTypes[l]);
                                    r.x             = 30F;
                                    r.y            += 5F;
                                    PopupWindow.Show(r, this.hoverPopup);
                                }
                            }
                            else
                            {
                                if (this.hoverPopup != null && this.hoverPopup.potentialType.type == this.potentialTypes[l].type)
                                {
                                    if (this.hoverPopup.editorWindow != null)
                                    {
                                        this.hoverPopup.editorWindow.Close();
                                    }
                                    this.hoverPopup = null;
                                }
                            }
                        }
                    }
                    EditorGUILayout.EndHorizontal();
                }
            }
            --EditorGUI.indentLevel;
            --EditorGUI.indentLevel;
        }