/// <summary>
        /// Creates all the necessary runtime editor subsystems.
        /// </summary>
        private static void CreateRuntimeEditorApplicationSubsystems()
        {
            // First, make sure all existing subsystems are destroyed
            DestroyExistingSubsystems();

            // Now, create each subsystem
            RuntimeEditorApplication runtimeEditorApplication = CreateSubsystemObject <RuntimeEditorApplication>(null);
            Transform runtimeEditorApplicationTransform       = runtimeEditorApplication.transform;

            EditorGizmoSystem editorGizmoSystem = CreateSubsystemObject <EditorGizmoSystem>(runtimeEditorApplicationTransform);

            CreateSubsystemObject <EditorObjectSelection>(runtimeEditorApplicationTransform);
            EditorCamera editorCamera = CreateSubsystemObject <EditorCamera>(runtimeEditorApplicationTransform);

            editorCamera.gameObject.AddComponent <Camera>();

            CreateSubsystemObject <EditorUndoRedoSystem>(runtimeEditorApplicationTransform);
            CreateSubsystemObject <EditorMeshDatabase>(runtimeEditorApplicationTransform);
            CreateSubsystemObject <MessageListenerDatabase>(runtimeEditorApplicationTransform);
            CreateSubsystemObject <InputDevice>(runtimeEditorApplicationTransform);
            CreateSubsystemObject <SceneGizmo>(runtimeEditorApplicationTransform);

            // Create all transform gizmos and attach them to the gizmo system
            GameObject gizmoObject = new GameObject();

            gizmoObject.name             = "Translation Gizmo";
            gizmoObject.transform.parent = runtimeEditorApplicationTransform;
            TranslationGizmo translationGizmo = gizmoObject.AddComponent <TranslationGizmo>();

            editorGizmoSystem.TranslationGizmo = translationGizmo;

            gizmoObject                  = new GameObject();
            gizmoObject.name             = "Rotation Gizmo";
            gizmoObject.transform.parent = runtimeEditorApplicationTransform;
            RotationGizmo rotationGizmo = gizmoObject.AddComponent <RotationGizmo>();

            rotationGizmo.GizmoBaseScale    = 1.3f;
            editorGizmoSystem.RotationGizmo = rotationGizmo;

            gizmoObject                  = new GameObject();
            gizmoObject.name             = "Scale Gizmo";
            gizmoObject.transform.parent = runtimeEditorApplicationTransform;
            ScaleGizmo scaleGizmo = gizmoObject.AddComponent <ScaleGizmo>();

            editorGizmoSystem.ScaleGizmo = scaleGizmo;

            gizmoObject                  = new GameObject();
            gizmoObject.name             = "Volume Scale Gizmo";
            gizmoObject.transform.parent = runtimeEditorApplicationTransform;
            VolumeScaleGizmo volumeScaleGizmo = gizmoObject.AddComponent <VolumeScaleGizmo>();

            editorGizmoSystem.VolumeScaleGizmo = volumeScaleGizmo;
        }
        public override void OnSelect()
        {
            base.OnSelect();

            //Draw the Gizmos Root and and set his position.
            initialzeGizmo(ref gizmosRoot, "Gizmos", null);
            gizmosRoot.transform.position = transform.position;

            initialzeGizmo(ref topAnchor, "topAnchor", gizmosRoot.transform);
            topAnchor.transform.localPosition = new Vector3(0, transform.localScale.y, 0);


            initialzeGizmo(ref bottomAnchor, "bottomAnchor", gizmosRoot.transform);
            bottomAnchor.transform.localScale    = new Vector3(transform.localScale.x + 0.2f, bottomAnchor.transform.localScale.y + 0.2f, bottomAnchor.transform.localScale.z + 0.2f);
            bottomAnchor.transform.localPosition = Vector3.zero;

            initialzeGizmo(ref leftAnchor, "leftAnchor", gizmosRoot.transform);
            initialzeGizmo(ref rightAnchor, "rightAnchor", gizmosRoot.transform);


            //initialize the Scale Gizmo.
            ScaleGizmo scaleGizmo = new ScaleGizmo();

            scaleGizmo.Draw(topAnchor.transform, GizmoType.Default, scaleGizmo);

            //initialize the Translation Gizmo.
            //TranslateGizmo translateGizmo = new TranslateGizmo();
            //translateGizmo.Draw(topAnchor.transform, GizmoType.Default, translateGizmo);

            gizmosRoot.transform.localScale = transform.parent.localScale;

            if (transform.gameObject.GetComponent <Collider>())
            {
                transform.gameObject.GetComponent <Collider>().isTrigger = false;
            }

            if (!transform.gameObject.GetComponent <Rigidbody>())
            {
                transform.gameObject.AddComponent <Rigidbody>();
                transform.gameObject.GetComponent <Rigidbody>().isKinematic = true;
            }

            Spaces.UI.UIHandler.instance.ShowManipulationTool();
        }
示例#3
0
 public ScaleTool()
 {
     gizmo = new ScaleGizmo();
 }
        /// <summary>
        /// Called when the inspector needs to be rendered.
        /// </summary>
        public override void OnInspectorGUI()
        {
            // Let the user specify the translation gizmo
            EditorGUILayout.BeginVertical("Box");
            TranslationGizmo translationGizmo = EditorGUILayout.ObjectField("Translation Gizmo", _gizmoSystem.TranslationGizmo, typeof(TranslationGizmo), true) as TranslationGizmo;

            if (translationGizmo != _gizmoSystem.TranslationGizmo)
            {
                UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                _gizmoSystem.TranslationGizmo = translationGizmo;
            }

            // Let the user specify the rotation gizmo
            RotationGizmo rotationGizmo = EditorGUILayout.ObjectField("Rotation Gizmo", _gizmoSystem.RotationGizmo, typeof(RotationGizmo), true) as RotationGizmo;

            if (rotationGizmo != _gizmoSystem.RotationGizmo)
            {
                UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                _gizmoSystem.RotationGizmo = rotationGizmo;
            }

            // Let the user specify the scale gizmo
            ScaleGizmo scaleGizmo = EditorGUILayout.ObjectField("Scale Gizmo", _gizmoSystem.ScaleGizmo, typeof(ScaleGizmo), true) as ScaleGizmo;

            if (scaleGizmo != _gizmoSystem.ScaleGizmo)
            {
                UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                _gizmoSystem.ScaleGizmo = scaleGizmo;
            }

            VolumeScaleGizmo volumeScaleGizmo = EditorGUILayout.ObjectField("Volume Scale Gizmo", _gizmoSystem.VolumeScaleGizmo, typeof(VolumeScaleGizmo), true) as VolumeScaleGizmo;

            if (scaleGizmo != _gizmoSystem.VolumeScaleGizmo)
            {
                UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                _gizmoSystem.VolumeScaleGizmo = volumeScaleGizmo;
            }

            // Let the user specify the active gimo type
            if (_gizmoSystem.IsAnyGizmoTypeAvailable())
            {
                EditorGUILayout.Separator();
                GizmoType newActiveGizmoType = (GizmoType)EditorGUILayout.EnumPopup("Active Gizmo Type", _gizmoSystem.ActiveGizmoType);
                if (newActiveGizmoType != _gizmoSystem.ActiveGizmoType)
                {
                    UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                    _gizmoSystem.ActiveGizmoType = newActiveGizmoType;
                }
            }

            EditorGUILayout.Separator();
            List <GizmoType> allGizmoTypes = Enum.GetValues(typeof(GizmoType)).Cast <GizmoType>().ToList();

            foreach (var gizmoType in allGizmoTypes)
            {
                bool isGizmoTypeAvailable = _gizmoSystem.IsGizmoTypeAvailable(gizmoType);
                bool newBool = EditorGUILayout.ToggleLeft(gizmoType.ToString(), isGizmoTypeAvailable);
                if (isGizmoTypeAvailable != newBool)
                {
                    UnityEditorUndoHelper.RecordObjectForInspectorPropertyChange(_gizmoSystem);
                    if (newBool)
                    {
                        _gizmoSystem.SetGizmoTypeAvailable(gizmoType, true);
                    }
                    else
                    {
                        _gizmoSystem.SetGizmoTypeAvailable(gizmoType, false);
                    }
                }
            }
            EditorGUILayout.EndVertical();

            _keyMappingsAreVisible = EditorGUILayout.Foldout(_keyMappingsAreVisible, "Key mappings");
            if (_keyMappingsAreVisible)
            {
                _gizmoSystem.ActivateTranslationGizmoShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.ActivateRotationGizmoShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.ActivateScaleGizmoShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.ActivateVolumeScaleGizmoShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.ActivateGlobalTransformShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.ActivateLocalTransformShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.TurnOffGizmosShortcut.RenderView(_gizmoSystem);
                _gizmoSystem.TogglePivotShortcut.RenderView(_gizmoSystem);
            }
        }
        protected override Task <bool> Initialize(EditorServiceGame editorGame)
        {
            game        = (EntityHierarchyEditorGame)editorGame;
            editorScene = game.EditorScene;

            var transformMainGizmoRenderStage        = new RenderStage("TransformGizmoOpaque", "Main");
            var transformTransparentGizmoRenderStage = new RenderStage("TransformGizmoTransparent", "Main")
            {
                SortMode = new BackToFrontSortMode()
            };

            game.EditorSceneSystem.GraphicsCompositor.RenderStages.Add(transformMainGizmoRenderStage);
            game.EditorSceneSystem.GraphicsCompositor.RenderStages.Add(transformTransparentGizmoRenderStage);

            var meshRenderFeature = game.EditorSceneSystem.GraphicsCompositor.RenderFeatures.OfType <MeshRenderFeature>().First();

            // Reset all stages for TransformationGrizmoGroup
            meshRenderFeature.RenderStageSelectors.Add(new SimpleGroupToRenderStageSelector
            {
                RenderGroup = TransformationGizmo.TransformationGizmoGroupMask,
            });
            meshRenderFeature.RenderStageSelectors.Add(new MeshTransparentRenderStageSelector
            {
                EffectName             = EditorGraphicsCompositorHelper.EditorForwardShadingEffect,
                RenderGroup            = TransformationGizmo.TransformationGizmoGroupMask,
                OpaqueRenderStage      = transformMainGizmoRenderStage,
                TransparentRenderStage = transformTransparentGizmoRenderStage,
            });
            meshRenderFeature.PipelineProcessors.Add(new MeshPipelineProcessor {
                TransparentRenderStage = transformTransparentGizmoRenderStage
            });

            var editorCompositor = (EditorTopLevelCompositor)game.EditorSceneSystem.GraphicsCompositor.Game;

            editorCompositor.PostGizmoCompositors.Add(new ClearRenderer {
                ClearFlags = ClearRendererFlags.DepthOnly
            });
            editorCompositor.PostGizmoCompositors.Add(new SingleStageRenderer {
                RenderStage = transformMainGizmoRenderStage, Name = "Transform Opaque Gizmos"
            });
            editorCompositor.PostGizmoCompositors.Add(new SingleStageRenderer {
                RenderStage = transformTransparentGizmoRenderStage, Name = "Transform Transparent Gizmos"
            });

            TranslationGizmo = new TranslationGizmo();
            RotationGizmo    = new RotationGizmo();
            ScaleGizmo       = new ScaleGizmo();
            TranslationGizmo.TransformationEnded += OnGizmoTransformationFinished;
            ScaleGizmo.TransformationEnded       += OnGizmoTransformationFinished;
            RotationGizmo.TransformationEnded    += OnGizmoTransformationFinished;

            transformationGizmos.Add(TranslationGizmo);
            transformationGizmos.Add(RotationGizmo);
            transformationGizmos.Add(ScaleGizmo);

            Services.Get <IEditorGameEntitySelectionService>().SelectionUpdated += UpdateModifiedEntitiesList;

            // Initialize and add the Gizmo entities to the gizmo scene
            MicrothreadLocalDatabases.MountCommonDatabase();

            // initialize the gizmo
            foreach (var gizmo in transformationGizmos)
            {
                gizmo.Initialize(game.Services, editorScene);
            }

            // Deactivate all transformation gizmo by default
            foreach (var gizmo in transformationGizmos)
            {
                gizmo.IsEnabled = false;
            }

            // set the default active transformation gizmo
            ActiveTransformationGizmo = TranslationGizmo;

            // Start update script (with priority 1 so that it happens after UpdateModifiedEntitiesList is called -- which usually happens from a EditorGameComtroller.PostAction() which has a default priority 0)
            game.Script.AddTask(Update, 1);
            return(Task.FromResult(true));
        }
 /// <summary>
 /// Called when the gizmo is selected in the scene view.
 /// </summary>
 protected override void OnEnable()
 {
     base.OnEnable();
     _scaleGizmo = target as ScaleGizmo;
 }