Пример #1
0
        public static void GenerateAGXFileAsPrefab()
        {
            foreach (var obj in Selection.objects)
            {
                var info = new IO.AGXFileInfo(AssetDatabase.GetAssetPath(obj));
                if (info.Type != IO.AGXFileInfo.FileType.AGXBinary && info.Type != IO.AGXFileInfo.FileType.AGXAscii)
                {
                    continue;
                }

                AssetPostprocessorHandler.ReadAGXFile(info);
            }
        }
Пример #2
0
        private static void OnHierarchyWindowChanged()
        {
            var scene = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene();

            if (scene != null && scene.name != m_currentSceneName)
            {
                EditorData.Instance.GC();

                m_currentSceneName = scene.name;

                // - Verifies so that our shapes doesn't have multiple debug rendering components.
                // - Verifies version of OnSelectionProxy and patches it if Target == null.
                AgXUnity.Collide.Shape[] shapes = UnityEngine.Object.FindObjectsOfType <AgXUnity.Collide.Shape>();
                foreach (var shape in shapes)
                {
                    OnSelectionProxy selectionProxy = shape.GetComponent <OnSelectionProxy>();
                    if (selectionProxy != null && selectionProxy.Target == null)
                    {
                        selectionProxy.Component = shape;
                    }

                    AgXUnity.Rendering.ShapeDebugRenderData[] data = shape.GetComponents <AgXUnity.Rendering.ShapeDebugRenderData>();
                    if (data.Length > 1)
                    {
                        Debug.Log("Shape has several ShapeDebugRenderData. Removing/resetting.", shape);
                        foreach (var instance in data)
                        {
                            Component.DestroyImmediate(instance);
                        }
                        data = null;
                    }
                }

                // We're back to ScriptComponent version of MassProperties.
                AgXUnity.RigidBody[] bodies = UnityEngine.Object.FindObjectsOfType <AgXUnity.RigidBody>();
                foreach (var rb in bodies)
                {
                    if (!rb.PatchMassPropertiesAsComponent())
                    {
                        continue;
                    }

                    Debug.Log("Updated RigidBody: " + rb.name + " to new MassProperties version.", rb);

                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                }

                // Patching constraints where we removed ElementaryConstraint as component.
                // Now we're back and I hope the components will still show up as null now
                // that AgXUnity.ElementaryConstraint is a component again.
                {
                    AgXUnity.Constraint[] constraints = UnityEngine.Object.FindObjectsOfType <AgXUnity.Constraint>();
                    foreach (var constraint in constraints)
                    {
                        bool isOldVersion = (from component in constraint.GetComponents <Component>() where component == null select component).ToArray().Length > 0 &&
                                            constraint.ElementaryConstraints.Length == 0;
                        if (!isOldVersion)
                        {
                            continue;
                        }

                        if (EditorUtility.DisplayDialog("Update \"" + constraint.name + "\" (type: " + constraint.Type + ") to the new version?",
                                                        "The game object will be deleted and a new will be created with the same Reference/Connected setup. All data such as compliance, damping, motor speed etc. will be lost.",
                                                        "Update", "Ignore"))
                        {
                            AgXUnity.Constraint newConstraint = AgXUnity.Constraint.Create(constraint.Type);

                            newConstraint.AttachmentPair.ReferenceObject = constraint.AttachmentPair.ReferenceObject;
                            newConstraint.AttachmentPair.ReferenceFrame.LocalPosition = constraint.AttachmentPair.ReferenceFrame.LocalPosition;
                            newConstraint.AttachmentPair.ReferenceFrame.LocalRotation = constraint.AttachmentPair.ReferenceFrame.LocalRotation;

                            newConstraint.AttachmentPair.Synchronized = constraint.AttachmentPair.Synchronized;

                            newConstraint.AttachmentPair.ConnectedObject = constraint.AttachmentPair.ConnectedObject;
                            newConstraint.AttachmentPair.ConnectedFrame.LocalPosition = constraint.AttachmentPair.ConnectedFrame.LocalPosition;
                            newConstraint.AttachmentPair.ConnectedFrame.LocalRotation = constraint.AttachmentPair.ConnectedFrame.LocalRotation;

                            newConstraint.name = constraint.name;

                            GameObject.DestroyImmediate(constraint.gameObject);

                            Debug.Log("Constraint: " + newConstraint.name + " updated.", newConstraint);

                            UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                        }
                    }
                }

                // Patching constraints where ElementaryConstraint is a ScriptAsset.
                {
                    AgXUnity.Constraint[] constraints = UnityEngine.Object.FindObjectsOfType <AgXUnity.Constraint>();
                    foreach (var constraint in constraints)
                    {
                        bool isOldVersion = constraint.ElementaryConstraints.Length > 0 && constraint.GetComponents <AgXUnity.ElementaryConstraint>().Length == 0;
                        if (!isOldVersion)
                        {
                            continue;
                        }

                        // Updating to new attachment pair.
                        var attachmentPair = constraint.AttachmentPair;

                        // Updating to where ElementaryConstraints are components.
                        constraint.TransformToComponentVersion();

                        Debug.Log("Constraint: " + constraint.name + " updated to new version.", constraint);

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                    }
                }

                // Patching old constraints to new versions.
                {
                    AgXUnity.Constraint[] constraints = UnityEngine.Object.FindObjectsOfType <AgXUnity.Constraint>();
                    foreach (var constraint in constraints)
                    {
                        if (!constraint.VerifyImplementation())
                        {
                            continue;
                        }

                        Debug.Log("Constraint: " + constraint.name + " successfully updated to new version.", constraint);

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                    }
                }

                // Patching OnSelectionProxy (Target == null) in the wire rendering SegmentSpawner.
                {
                    AgXUnity.Wire[] wires = UnityEngine.Object.FindObjectsOfType <AgXUnity.Wire>();
                    foreach (var wire in wires)
                    {
                        AgXUnity.Rendering.SegmentSpawner ss = wire.GetComponent <AgXUnity.Rendering.WireRenderer>().SegmentSpawner;
                        if (ss == null)
                        {
                            continue;
                        }

                        var segments = ss.Segments;
                        foreach (var segment in segments)
                        {
                            OnSelectionProxy selectionProxy = segment.GetComponent <OnSelectionProxy>();
                            if (selectionProxy != null && selectionProxy.Target == null)
                            {
                                selectionProxy.Component = wire;
                            }
                        }
                    }
                }

                // Patching Wire to use Route as component and RouteNode as Frame.
                {
                    AgXUnity.Wire[] wires = UnityEngine.Object.FindObjectsOfType <AgXUnity.Wire>();
                    foreach (var wire in wires)
                    {
                        if (wire.GetComponent <AgXUnity.WireRoute>() != null)
                        {
                            continue;
                        }

                        var routeData = wire.GetComponent <AgXUnity.Legacy.WireRouteData>();
                        var route     = wire.gameObject.AddComponent <AgXUnity.WireRoute>();
                        if (routeData != null && routeData.Restore())
                        {
                            Debug.Log("Successfully restored " + route.NumNodes + " from local data.", wire);
                            AgXUnity.ScriptComponent.DestroyImmediate(routeData);
                        }
                        else
                        {
                            Debug.LogWarning("Wire: " + wire.name + " is not possible to load and has to be re-routed.", wire);
                        }

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                    }
                }

                // Patching Cable to use Route as component and RouteNode as Frame.
                {
                    AgXUnity.Cable[] cables = UnityEngine.Object.FindObjectsOfType <AgXUnity.Cable>();
                    foreach (var cable in cables)
                    {
                        if (cable.GetComponent <AgXUnity.CableRoute>() != null)
                        {
                            continue;
                        }

                        var routeData = cable.GetComponent <AgXUnity.Legacy.CableRouteData>();
                        var route     = cable.gameObject.AddComponent <AgXUnity.CableRoute>();
                        if (routeData != null && routeData.Restore())
                        {
                            Debug.Log("Successfully restored " + route.NumNodes + " from local data.", cable);
                            AgXUnity.ScriptComponent.DestroyImmediate(routeData);
                        }
                        else
                        {
                            Debug.LogWarning("Cable: " + cable.name + " is not possible to load and has to be re-routed.", cable);
                        }

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                    }
                }

                // Verifying shape visuals material.
                {
                    AgXUnity.Rendering.ShapeVisual[] shapeVisuals = UnityEngine.Object.FindObjectsOfType <AgXUnity.Rendering.ShapeVisual>();
                    foreach (var shapeVisual in shapeVisuals)
                    {
                        var renderers = shapeVisual.GetComponentsInChildren <MeshRenderer>();
                        foreach (var renderer in renderers)
                        {
                            if (renderer.sharedMaterial == null)
                            {
                                renderer.sharedMaterial = GetOrCreateShapeVisualDefaultMaterial();

                                Debug.Log("Shape visual with null material. Assigning default.", shapeVisual);

                                if (!EditorApplication.isPlaying)
                                {
                                    UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                                }
                            }
                        }
                    }
                }

                // Patching Collide.Mesh single source object to source object list.
                {
                    AgXUnity.Collide.Mesh[] meshes = UnityEngine.Object.FindObjectsOfType <AgXUnity.Collide.Mesh>();
                    foreach (var mesh in meshes)
                    {
                        if (!mesh.PatchSingleSourceToSourceList())
                        {
                            continue;
                        }

                        Debug.Log("Patch: Moved mesh source to source list.", mesh);

                        UnityEditor.SceneManagement.EditorSceneManager.MarkSceneDirty(scene);
                    }
                }
            }
            else if (Selection.activeGameObject != null && Selection.activeGameObject.GetComponent <AgXUnity.IO.RestoredAGXFile>() != null)
            {
                AssetPostprocessorHandler.OnPrefabAddedToScene(Selection.activeGameObject);
            }
        }