示例#1
0
        private static void AddAudioSource()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("AudioSource", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            UndoRedo.RecordSO(so, false, "Added a AudioSource component");
            so.AddComponent <AudioSource>();
            EditorApplication.SetSceneDirty();
        }
示例#2
0
        private static void AddSliderJoint()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("SliderJoint", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            UndoRedo.RecordSO(so, false, "Added a SliderJoint component");
            so.AddComponent <SliderJoint>();
            EditorApplication.SetSceneDirty();
        }
示例#3
0
        private static void AddCharacterController()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("CharacterController", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            UndoRedo.RecordSO(so, false, "Added a CharacterController component");
            so.AddComponent <CharacterController>();
            EditorApplication.SetSceneDirty();
        }
示例#4
0
        private void AddBackedLight(Vector3 position, Color color, float range = 65f)
        {
            var sceneObject = new SceneObject("Light_" + color.ToString());

            sceneObject.Transform.Position = position;

            var light = sceneObject.AddComponent <Light>();

            light.FallOf    = 10f;
            light.Intensity = 1.5f;
            light.Range     = range;
            light.Color     = color;
            light.Backing   = LightRenderMode.Backed;

            Add(sceneObject);
        }
示例#5
0
        private static void AddCamera()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                return;
            }

            UndoRedo.RecordSO(so, false, "Added a Camera component");
            Camera cam = so.AddComponent <Camera>();

            cam.Main = true;

            EditorApplication.SetSceneDirty();
        }
示例#6
0
        private static void AddDirectionalLight()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                return;
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a Light component");
            Light light = so.AddComponent <Light>();

            light.Type = LightType.Directional;

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
示例#7
0
        public override void Initialize(ContentManager content)
        {
            base.Initialize(content);

            _viewport.X = graphicsDevice.Viewport.Width;
            _viewport.Y = graphicsDevice.Viewport.Height;
            CreateRenderTargets(graphicsDevice);

            _depthNormalFX = content.Load <Effect>("FX/PreLighting/PL_DepthNormal");
            _lightingFX    = content.Load <Effect>("FX/PreLighting/PL_LightMap");

            var so = new SceneObject("LightMesh");

            _lightMesh          = so.AddComponent <MeshRenderer>();
            _lightMesh.Geometry = new SphereGeometry();
            _lightMesh.Geometry.Build();
        }
示例#8
0
        /// <summary>
        /// Creates a new scene axes GUI.
        /// </summary>
        /// <param name="window">Window in which the GUI is located in.</param>
        /// <param name="panel">Panel onto which to place the GUI element.</param>
        /// <param name="width">Width of the GUI element.</param>
        /// <param name="height">Height of the GUI element.</param>
        /// <param name="projType">Projection type to display on the GUI.</param>
        public SceneAxesGUI(SceneWindow window, GUIPanel panel, int width, int height, ProjectionType projType)
        {
            renderTexture          = new RenderTexture(PixelFormat.RGBA8, width, height);
            renderTexture.Priority = 1;

            SceneObject cameraSO = new SceneObject("SceneAxesCamera", true);

            camera = cameraSO.AddComponent <Camera>();
            camera.Viewport.Target = renderTexture;
            camera.Viewport.Area   = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);

            cameraSO.Position = new Vector3(0, 0, 5);
            cameraSO.LookAt(new Vector3(0, 0, 0));

            camera.Priority            = 2;
            camera.NearClipPlane       = 0.05f;
            camera.FarClipPlane        = 1000.0f;
            camera.Viewport.ClearColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
            camera.ProjectionType      = ProjectionType.Orthographic;
            camera.Layers      = SceneAxesHandle.LAYER;
            camera.AspectRatio = 1.0f;
            camera.OrthoHeight = 2.0f;
            camera.RenderSettings.EnableHDR    = false;
            camera.RenderSettings.EnableSkybox = false;

            renderTextureGUI = new GUIRenderTexture(renderTexture, true);

            GUILayoutY layout        = panel.AddLayoutY();
            GUILayoutX textureLayout = layout.AddLayoutX();

            textureLayout.AddElement(renderTextureGUI);
            textureLayout.AddFlexibleSpace();

            Rect2I bounds = new Rect2I(0, 0, width, height);

            sceneHandles            = new SceneHandles(window, camera);
            renderTextureGUI.Bounds = bounds;

            labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
            layout.AddElement(labelGUI);
            layout.AddFlexibleSpace();

            this.panel  = panel;
            this.bounds = bounds;
        }
示例#9
0
        private void InitScene()
        {
            myScene = new Scene();

            myOnyxInstance = Onyx3DEngine.Instance;
            myOnyxInstance.Init();

            SceneObject  teapot     = new SceneObject("Teapot");
            MeshRenderer teapotMesh = teapot.AddComponent <MeshRenderer>();

            teapotMesh.Mesh = myOnyxInstance.Resources.GetMesh(BuiltInMesh.Teapot);
            teapot.Transform.LocalPosition = new Vector3(0, 0.5f, 0);
            teapotMesh.Material            = myOnyxInstance.Resources.GetMaterial(BuiltInMaterial.Default);
            teapot.Parent = myScene.Root;



            SceneObject  teapot2     = new SceneObject("Teapot2");
            MeshRenderer teapot2Mesh = teapot2.AddComponent <MeshRenderer>();

            teapot2Mesh.Mesh                = myOnyxInstance.Resources.GetMesh(BuiltInMesh.Teapot);
            teapot2Mesh.Material            = myOnyxInstance.Resources.GetMaterial(BuiltInMaterial.Default);
            teapot2.Transform.LocalScale    = new Vector3(0.5f, 0.5f, 0.5f);
            teapot2.Transform.LocalPosition = new Vector3(2, 0, 2);
            teapot2.Transform.LocalRotation = Quaternion.FromEulerAngles(new Vector3(0, 90, 0));
            teapot2.Parent = myScene.Root;

            myTeapot = teapot2;

            // Editor objects --------------------------------------

            SceneObject grid = new SceneObject("Grid");

            myGridRenderer = grid.AddComponent <GridRenderer>();
            myGridRenderer.GenerateGridMesh(100, 100, 0.25f, 0.25f);
            myGridRenderer.Material = myOnyxInstance.Resources.GetMaterial(BuiltInMaterial.Unlit);
            myGridRenderer.Material.Properties["color"].Data = new Vector4(1, 1, 1, 0.1f);

            //myBox = teapot.AddComponent<BoxRenderer>();
            //myBox.Material = myOnyxInstance.Resources.GetMaterial(BuiltInMaterial.UnlitVertexColor);

            mNavigation.CreateCamera();

            UpdateTreeView();
        }
示例#10
0
        private static void AddAudioListener()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = UndoRedo.CreateSO("AudioListener", "New scene object");

                Selection.SceneObject = so;
                FocusOnHierarchyOrScene();
            }

            GameObjectUndo.RecordSceneObject(so, false, "Added a AudioListener component");
            so.AddComponent <AudioListener>();

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
示例#11
0
        public static void Main(string[] _)
        {
            //new Input().Initialize(Input.ReadConsole).OnKey += (c) => { System.Console.WriteLine(c); };
            //camera.AddComponent(new Camera(60));
            camera = new Camera(60, new ConsoleRenderer(), new Dimensions2(30, 20)).DefaultObject();
            Scene scene = new Scene(camera.GetComponent <Camera>());

            camera.GetComponent <Camera>().renderer.renderMode = RenderMode.Shaded;
            EngineLoop e = new EngineLoop(scene, 10);

            e.RenderEvent += OnUpdate;                                 // Add the OnUpdate event for every time the camera renders

            cube1.AddComponent(new Mesh(BaseGeometry.CubeGeometry())); // Add a cube geometry to cube1
            cube1.GetComponent <Mesh>().color = new Color(1, 0, 1);    // Set the color of cube1's mesh
            cube1.worldPosition += new Point3(0, 0, 3);                // Change the worldPosition of cube1
            scene.objects.Add(cube1);                                  // Add cube1 to the scene

            camera.worldPosition = new Point3(0, -2, 0);
            camera.worldRotation = Quaternion.Euler(-30, 0, 0, Quaternion.AngleUnit.Degrees);
            //scene.light.color = Color.FromArgb(0, 255, 0);

            //cubeChild.parent = cube1; // Set the parent of cubeChild to be cube1
            cubeChild.localScale = new Direction3(0.5, 0.5, 1);
            cubeChild.AddComponent(new Mesh(BaseGeometry.CubeGeometry()));
            cubeChild.localPosition = new Point3(0, 0, 3);
            cubeChild.localRotation = Quaternion.Euler(145, 0, 0, Quaternion.AngleUnit.Degrees);
            cubeChild.GetComponent <Mesh>().color = new Color(0, 1, 1);

            SceneObject occ = new SceneObject();

            //occ.parent = cubeChild;
            occ.AddComponent(new Mesh(BaseGeometry.CubeGeometry()));
            occ.localPosition = new Point3(0, 0, 3);
            occ.GetComponent <Mesh>().color = new Color(0, 1, 0);
            occ.worldScale = new Direction3(1, 1, 1);
            //Tweener.TweenTo(ref cube1._localworldPosition,new Point3(0,0,0),5000);
            //scene.objects.Add(occ);

            //c.parent = o;*/
            //c.localworldPosition = new Point3(0, 0, 5);
            while (true)
            {
            }
        }
示例#12
0
        protected override void OnInit()
        {
            base.OnInit();

            _material = new Material(MaterialType.FlatVertexColor);

            var frag = @"
void getFragment(inout fragment_info fragment) {
    fragment.normal = frag_in.normal;

	if(dot(normalize(fragment.normal), normalize(-frag_in.view_pos)) > -0.25)
		fragment.emission = frag_in.color.xyz;
	else
		fragment.emission = vec3(0.75, 0.75, 0.75);
    fragment.albedo = vec4(0.0, 0.0, 0.0, 1.0);
}
";

            _renderer          = SceneObject.AddComponent <MeshRenderable>();
            _renderer.Material = new Material(null, frag, RenderQueue.Opaque);

            var circle = Ellipse.Create(Quaternion.Identity, Vector2.One * Radius, CircleSegmentCount);

            var meshBuilder = new MeshBuilder();

            meshBuilder.AddMesh(circle);
            meshBuilder.AddMesh(circle, Quaternion.CreateFromYawPitchRoll(0, MathF.PIOver2, 0));
            meshBuilder.AddMesh(circle, Quaternion.CreateFromYawPitchRoll(MathF.PIOver2, 0, 0));

            var mesh = meshBuilder.GetMesh();

            mesh.DrawStyle = PolygonMode.Line;
            mesh.Type      = PrimitiveType.Lines;
            mesh.Colors    = new Vector3Buffer(CircleSegmentCount * 3);
            _renderer.Mesh = mesh;

            RestoreColors();

            _sphere = new Sphere(Vector3.Zero, Radius);
            _xPlane = new Plane(new Vector3(1, 0, 0), 0);
            _yPlane = new Plane(new Vector3(0, 1, 0), 0);
            _zPlane = new Plane(new Vector3(0, 0, 1), 0);
        }
示例#13
0
        protected override void OnStart()
        {
            base.OnStart();

            SceneObject.Transform.WorldRotation = new Quaternion(Vector3.UnitX, MathF.PIOver2);

            _renderer           = SceneObject.AddComponent <MeshRenderable>();
            _renderer.IsEnabled = true;

            var mesh = Plane.Create(2, 2);

            _renderer.Mesh  = mesh;
            _material       = _renderer.Material = new Material(null, DiscardFrag, RenderQueue.Opaque);
            _material.Color = Vector4.One;
            _material.SetTexture("uniform_DiscardTexture", new Texture2("Resources/Textures/1.png", false));
            _material.SetTexture("uniform_NormalTexture", new Texture2("Resources/Textures/1.png", true));
            _material.MainTexture = new Texture2("Resources/Textures/2.png", false);
            _material.SetValue("uniform_DiscardFactor", 0f);
        }
示例#14
0
        // --------------------------------------------------------------------

        private void InitializeEditor()
        {
            Onyx3DEngine.InitMain(renderCanvas.Context, renderCanvas.WindowInfo);
            mOnyxInstance = Onyx3DEngine.Instance;

            mNavigation.CreateCamera();

            mObjectHandler = new ObjectHandler(mOnyxInstance, renderCanvas, mNavigation.Camera);
            mObjectHandler.OnTransformModified += OnTransformModifiedFromObjectHandler;

            selectedObjectInspector.InspectorChanged += OnInspectorChanged;

            SceneObject grid = new SceneObject("Grid");

            mGridRenderer = grid.AddComponent <GridRenderer>();
            mGridRenderer.GenerateGridMesh(100, 100, 0.25f, 0.25f, Vector3.One);
            mGridRenderer.Material = mOnyxInstance.Resources.GetMaterial(BuiltInMaterial.Unlit);
            mGridRenderer.Material.Properties["color"].Data = new Vector4(1, 1, 1, 0.1f);
        }
示例#15
0
        // --------------------------------------------------------------------

        public override void InitializeBasicScene()
        {
            base.InitializeBasicScene();

            mProxy        = new EntityProxy("proxy");
            mProxy.Parent = Scene.Root;

            mProxy.Transform.Rotate(new Vector3(0, MathHelper.DegreesToRadians(45f), 0));

            mFloor = new SceneObject("floor");
            MeshRenderer mr = mFloor.AddComponent <MeshRenderer>();

            mr.Mesh     = OnyxInstance.Resources.GetMesh(BuiltInMesh.Quad);
            mr.Material = OnyxInstance.Resources.GetMaterial(BuiltInMaterial.Default);
            mFloor.Transform.LocalPosition = new Vector3(0, -0.5f, 0);
            mFloor.Transform.LocalScale    = new Vector3(2, 2, 2);
            mFloor.Parent = Scene.Root;

            Camera.Transform.Translate(-Camera.Transform.Forward * 0.25f);
        }
示例#16
0
        /// <summary>
        /// Creates a new scene axes GUI.
        /// </summary>
        /// <param name="window">Window in which the GUI is located in.</param>
        /// <param name="panel">Panel onto which to place the GUI element.</param>
        /// <param name="width">Width of the GUI element.</param>
        /// <param name="height">Height of the GUI element.</param>
        /// <param name="projType">Projection type to display on the GUI.</param>
        public SceneAxesGUI(SceneWindow window, GUIPanel panel, int width, int height, ProjectionType projType)
        {
            renderTexture = new RenderTexture2D(PixelFormat.R8G8B8A8, width, height);
            renderTexture.Priority = 1;

            SceneObject cameraSO = new SceneObject("SceneAxesCamera", true);
            camera = cameraSO.AddComponent<Camera>();
            camera.Target = renderTexture;
            camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);

            cameraSO.Position = new Vector3(0, 0, 5);
            cameraSO.LookAt(new Vector3(0, 0, 0));

            camera.Priority = 2;
            camera.NearClipPlane = 0.05f;
            camera.FarClipPlane = 1000.0f;
            camera.ClearColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
            camera.ProjectionType = ProjectionType.Orthographic;
            camera.Layers = SceneAxesHandle.LAYER;
            camera.AspectRatio = 1.0f;
            camera.OrthoHeight = 2.0f;
            camera.HDR = false;

            renderTextureGUI = new GUIRenderTexture(renderTexture, true);

            GUILayoutY layout = panel.AddLayoutY();
            GUILayoutX textureLayout = layout.AddLayoutX();
            textureLayout.AddElement(renderTextureGUI);
            textureLayout.AddFlexibleSpace();

            Rect2I bounds = new Rect2I(0, 0, width, height);
            sceneHandles = new SceneHandles(window, camera);
            renderTextureGUI.Bounds = bounds;

            labelGUI = new GUILabel(projType.ToString(), EditorStyles.LabelCentered);
            layout.AddElement(labelGUI);
            layout.AddFlexibleSpace();

            this.panel = panel;
            this.bounds = bounds;
        }
示例#17
0
        private static void AddCharacterController()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("CharacterController");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <CharacterController>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a CharacterController component");
                so.AddComponent <CharacterController>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
示例#18
0
        // --------------------------------------------------------------------

        public override void InitializeBasicScene()
        {
            base.InitializeBasicScene();

            SceneObject sphere = new SceneObject("sphere");

            mRenderer          = sphere.AddComponent <MeshRenderer>();
            mRenderer.Material = OnyxInstance.Resources.GetMaterial(BuiltInMaterial.Default);
            SetMesh(BuiltInMesh.Sphere);
            sphere.Parent = Scene.Root;

            mFloor = new SceneObject("floor");
            MeshRenderer mr = mFloor.AddComponent <MeshRenderer>();

            mr.Mesh     = OnyxInstance.Resources.GetMesh(BuiltInMesh.Quad);
            mr.Material = OnyxInstance.Resources.GetMaterial(BuiltInMaterial.Default);
            mFloor.Transform.LocalPosition = new Vector3(0, -0.5f, 0);
            mFloor.Transform.LocalScale    = new Vector3(2, 2, 2);
            mFloor.Parent = Scene.Root;

            Camera.Transform.Translate(-Camera.Transform.Forward * 0.25f);
        }
示例#19
0
        private static void AddSphericalJoint()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("SphericalJoint");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <SphericalJoint>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a SphericalJoint component");
                so.AddComponent <SphericalJoint>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
示例#20
0
        private static void AddAudioSource()
        {
            SceneObject so = Selection.SceneObject;

            if (so == null)
            {
                so = new SceneObject("AudioSource");

                GameObjectUndo.RecordNewSceneObject(so);
                so.AddComponent <AudioSource>();

                FocusOnHierarchyOrScene();
            }
            else
            {
                GameObjectUndo.RecordSceneObject(so, false, "Added a AudioSource component");
                so.AddComponent <AudioSource>();
            }

            GameObjectUndo.ResolveDiffs();
            EditorApplication.SetSceneDirty();
        }
示例#21
0
        // --------------------------------------------------------------------

        public virtual void InitializeBasicScene()
        {
            Scene = new Scene(OnyxInstance);

            Camera = new PerspectiveCamera("MainCamera", 0.5f, (float)mFrameBuffer.Width / (float)mFrameBuffer.Height);
            Camera.Transform.LocalPosition = new Vector3(0, 1.45f, 3f);
            Camera.Transform.LocalRotation = OpenTK.Quaternion.FromAxisAngle(new Vector3(1, 0, 0), -0.45f);

            Scene.ActiveCamera = Camera;

            mCamPivot        = new SceneObject("camPivot");
            mCamPivot.Parent = Scene.Root;
            Camera.Parent    = mCamPivot;

            SceneObject grid = new SceneObject("Grid");

            mGridRenderer = grid.AddComponent <GridRenderer>();
            mGridRenderer.GenerateGridMesh(10, 10, 0.25f, 0.25f, new Vector3(0.8f, 0.8f, 0.8f));
            mGridRenderer.Material = OnyxInstance.Resources.GetMaterial(BuiltInMaterial.UnlitVertexColor);

            SceneObject light  = new SceneObject("Light");
            Light       lightC = light.AddComponent <Light>();

            light.Parent = Scene.Root;
            light.Transform.LocalPosition = new Vector3(1, 2, 1);
            lightC.Intensity = 5;

            SceneObject test = new SceneObject("ReflectionProbe");

            test.Parent = Scene.Root;
            test.Transform.LocalPosition = new Vector3(0, 0, 0);
            mReflectionProbe             = test.AddComponent <ReflectionProbe>();
            mReflectionProbe.Init(64);

            mReflectionProbe.Bake(OnyxInstance.Renderer);
        }
示例#22
0
        protected override void OnStart()
        {
            _control = Scene.Control;

            _control.MouseDown += Control_MouseDown;

            var material = new Material(MaterialType.FlatColor);

            material.Color      = new Vector4(1, 1, 0, 1);
            _renderer           = SceneObject.AddComponent <MeshRenderable>();
            _renderer.Material  = material;
            _renderer.IsEnabled = false;

            var mesh    = new Mesh();
            var buffer3 = new Vector3Buffer(2);

            buffer3.Usage = BufferUsage.DynamicDraw;
            mesh.Vertices = buffer3;
            mesh.Type     = PrimitiveType.Lines;

            _renderer.Mesh = mesh;

            var hitPointSo = new Node(Scene);
            var sphere     = Icosahedron.Create(0.01f, 1);

            _hitPointRenderer                   = hitPointSo.AddComponent <MeshRenderable>();
            _hitPointRenderer.IsEnabled         = false;
            _hitPointRenderer.Mesh              = sphere;
            _hitPointRenderer.SceneObject.Layer = 2;
            _hitPointRenderer.Material          = material;

            _hitPointTransform = hitPointSo.Transform;

            _selectedMaterial       = new Material(MaterialType.DiffuseColor);
            _selectedMaterial.Color = new Vector4(1, 0, 0, 1);
        }
示例#23
0
        /// <summary>
        /// Activates or deactivates the profiler overlay according to current editor settings.
        /// </summary>
        private void UpdateProfilerOverlay()
        {
            if (EditorSettings.GetBool(ProfilerOverlayActiveKey))
            {
                if (activeProfilerOverlay == null)
                {
                    SceneObject profilerSO = new SceneObject("EditorProfilerOverlay");
                    profilerCamera = profilerSO.AddComponent<Camera>();
                    profilerCamera.Target = renderTexture;
                    profilerCamera.ClearFlags = ClearFlags.None;
                    profilerCamera.Priority = 1;
                    profilerCamera.Layers = 0;
                    profilerCamera.HDR = false;

                    activeProfilerOverlay = profilerSO.AddComponent<ProfilerOverlay>();
                }
            }
            else
            {
                if (activeProfilerOverlay != null)
                {
                    activeProfilerOverlay.SceneObject.Destroy();
                    activeProfilerOverlay = null;
                    profilerCamera = null;
                }
            }
        }
示例#24
0
        private void OnEditorUpdate()
        {
            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
                        DuplicateSelection();
                    else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
                        DeleteSelection();
                    else if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
                        EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));
                    else if(VirtualInput.IsButtonDown(viewToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;
                    else if(VirtualInput.IsButtonDown(moveToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;
                    else if(VirtualInput.IsButtonDown(rotateToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
                    else if(VirtualInput.IsButtonDown(scaleToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                UpdateProfilerOverlay();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGizmos.Draw();
            sceneGrid.Draw();

            bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();

            Vector2I scenePos;
            bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);
            bool dragResult = false;
            if (Input.IsPointerButtonUp(PointerButton.Left))
            {
                dragResult = EndDragSelection();
                if (sceneHandles.IsActive())
                    sceneHandles.ClearSelection();

                if (sceneAxesGUI.IsActive())
                    sceneAxesGUI.ClearSelection();
            }
            else if (Input.IsPointerButtonDown(PointerButton.Left))
            {
                mouseDownPosition = scenePos;
            }

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;
                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
                            if (meta != null)
                            {
                                if (meta.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent<Renderable>();
                                        renderable.Mesh = mesh;
                                        if (mesh != null)
                                            draggedSOOffset = mesh.Bounds.Box.Center;
                                        else
                                            draggedSOOffset = Vector3.Zero;
                                    }

                                    break;
                                }
                                else if (meta.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);

                                        if (draggedSO != null)
                                        {
                                            AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
                                            draggedSOOffset = draggedObjBounds.Center;
                                        }
                                        else
                                            draggedSOOffset = Vector3.Zero;
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        if (Input.IsButtonHeld(ButtonCode.Space))
                        {
                            SnapData snapData;
                            sceneSelection.Snap(scenePos, out snapData, new SceneObject[] {draggedSO});

                            Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
                            draggedSO.Position = snapData.position;
                            draggedSO.Rotation = q;
                        }
                        else
                        {
                            Ray worldRay = camera.ViewportToWorldRay(scenePos);
                            draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
                        }
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if (HasContentFocus || IsPointerHovering)
            {
                cameraController.EnableInput(true);

                if (inBounds && HasContentFocus)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
                            HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                            sceneAxesGUI.TrySelect(scenePos);
                        else
                            sceneHandles.TrySelect(scenePos);
                    }
                    else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
                             draggedSO == null && scenePos != mouseDownPosition)
                    {
                        if (isDraggingSelection)
                            UpdateDragSelection(scenePos);
                        else
                            StartDragSelection(scenePos);
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive && !dragActive && !dragResult)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] {draggedSO});
                        }
                    }
                }
            }
            else
                cameraController.EnableInput(false);

            SceneHandles.BeginInput();
            sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
            sceneHandles.Draw();

            sceneAxesGUI.UpdateInput(scenePos);
            sceneAxesGUI.Draw();
            SceneHandles.EndInput();

            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
                cameraController.FrameSelected();
        }
示例#25
0
        private void OnEditorUpdate()
        {
            UpdateLoadingProgress();

            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(EditorApplication.DuplicateKey))
                    {
                        DuplicateSelection();
                    }
                    else if (VirtualInput.IsButtonDown(EditorApplication.DeleteKey))
                    {
                        DeleteSelection();
                    }
                    else if (VirtualInput.IsButtonDown(viewToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;
                    }
                    else if (VirtualInput.IsButtonDown(moveToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;
                    }
                    else if (VirtualInput.IsButtonDown(rotateToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;
                    }
                    else if (VirtualInput.IsButtonDown(scaleToolKey))
                    {
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;
                    }
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGrid.Draw();

            bool handleActive = sceneHandles.IsActive() || sceneAxesGUI.IsActive();

            Vector2I scenePos;
            bool     inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);

            bool clearSelection = false;

            if (AllowViewportInput)
            {
                if (Input.IsPointerButtonUp(PointerButton.Left))
                {
                    clearSelection = true;
                }
                else if (Input.IsPointerButtonDown(PointerButton.Left))
                {
                    mouseDownPosition = scenePos;
                }
            }
            else
            {
                clearSelection = true;
                inBounds       = false;
            }

            bool dragResult = false;

            if (clearSelection)
            {
                dragResult = EndDragSelection();
                if (sceneHandles.IsActive())
                {
                    sceneHandles.ClearSelection();
                }

                if (sceneAxesGUI.IsActive())
                {
                    sceneAxesGUI.ClearSelection();
                }
            }

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;

            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;
                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            ResourceMeta meta = ProjectLibrary.GetMeta(draggedPaths[i]);
                            if (meta != null)
                            {
                                if (meta.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load <Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent <Renderable>();
                                        renderable.Mesh = mesh;
                                        if (mesh != null)
                                        {
                                            draggedSOOffset = mesh.Bounds.Box.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                                else if (meta.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load <Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);

                                        if (draggedSO != null)
                                        {
                                            AABox draggedObjBounds = EditorUtility.CalculateBounds(draggedSO);
                                            draggedSOOffset = draggedObjBounds.Center;
                                        }
                                        else
                                        {
                                            draggedSOOffset = Vector3.Zero;
                                        }
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        if (Input.IsButtonHeld(ButtonCode.Space))
                        {
                            SnapData snapData;
                            sceneSelection.Snap(scenePos, out snapData, new SceneObject[] { draggedSO });

                            Quaternion q = Quaternion.FromToRotation(Vector3.YAxis, snapData.normal);
                            draggedSO.Position = snapData.position;
                            draggedSO.Rotation = q;
                        }
                        else
                        {
                            Ray worldRay = camera.ScreenPointToRay(scenePos);
                            draggedSO.Position = worldRay * DefaultPlacementDepth - draggedSOOffset;
                        }
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if ((HasContentFocus || IsPointerHovering) && AllowViewportInput)
            {
                cameraController.EnableInput(true);

                if (inBounds && HasContentFocus)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX,
                                                               HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                        {
                            sceneAxesGUI.TrySelect(scenePos);
                        }
                        else
                        {
                            sceneHandles.TrySelect(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonHeld(PointerButton.Left) && !handleActive && !dragActive &&
                             draggedSO == null && scenePos != mouseDownPosition)
                    {
                        if (isDraggingSelection)
                        {
                            UpdateDragSelection(scenePos);
                        }
                        else
                        {
                            StartDragSelection(scenePos);
                        }
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive && !dragActive && !dragResult)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld, new SceneObject[] { draggedSO });
                        }
                    }
                }
            }
            else
            {
                cameraController.EnableInput(false);
            }

            if (AllowViewportInput)
            {
                SceneHandles.BeginInput();
                sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
                sceneAxesGUI.UpdateInput(scenePos);
                SceneHandles.EndInput();
            }

            sceneHandles.Draw();
            sceneAxesGUI.Draw();

            // Must be done after handle input is processed, in order to reflect most recent transform
            sceneGizmos.Draw();
            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
            {
                cameraController.FrameSelected();
            }
        }
示例#26
0
        /// <summary>
        /// Creates the scene camera and updates the render texture. Should be called at least once before using the
        /// scene view. Should be called whenever the window is resized.
        /// </summary>
        /// <param name="width">Width of the scene render target, in pixels.</param>
        /// <param name="height">Height of the scene render target, in pixels.</param>
        private void UpdateRenderTexture(int width, int height)
        {
            width  = MathEx.Max(20, width);
            height = MathEx.Max(20, height);

            // Note: Depth buffer and readable flags are required because ScenePicking uses it
            Texture2D colorTex = new Texture2D(width, height, PixelFormat.R8G8B8A8, TextureUsage.Render | TextureUsage.CPUReadable);
            Texture2D depthTex = new Texture2D(width, height, PixelFormat.D24S8, TextureUsage.DepthStencil | TextureUsage.CPUReadable);

            renderTexture          = new RenderTexture2D(colorTex, depthTex);
            renderTexture.Priority = 1;

            if (camera == null)
            {
                SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
                camera              = sceneCameraSO.AddComponent <Camera>();
                camera.Target       = renderTexture;
                camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);

                sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
                sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));

                camera.Priority      = 2;
                camera.NearClipPlane = 0.05f;
                camera.FarClipPlane  = 2500.0f;
                camera.ClearColor    = ClearColor;
                camera.Layers        = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera

                cameraController = sceneCameraSO.AddComponent <SceneCamera>();

                renderTextureGUI = new GUIRenderTexture(renderTexture);
                rtPanel.AddElement(renderTextureGUI);

                sceneGrid      = new SceneGrid(camera);
                sceneSelection = new SceneSelection(camera);
                sceneGizmos    = new SceneGizmos(camera);
                sceneHandles   = new SceneHandles(this, camera);
            }
            else
            {
                camera.Target = renderTexture;
                renderTextureGUI.RenderTexture = renderTexture;
            }

            Rect2I rtBounds = new Rect2I(0, 0, width, height);

            renderTextureGUI.Bounds = rtBounds;
            focusCatcher.Bounds     = GUIUtility.CalculateBounds(rtPanel, GUI);

            sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);

            // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
            // render target destroy/create cycle for every single pixel.

            camera.AspectRatio = width / (float)height;

            if (profilerCamera != null)
            {
                profilerCamera.Target = renderTexture;
            }
        }
示例#27
0
        public override void Initialize()
        {
            base.Initialize();

            // Camera
            var camera = new CameraPrefab("camera");

            camera.AddComponent <OrbitController>();
            camera.AddComponent <RayPickingTester>();
            Add(camera);

            // Light
            var lightPrefab = new LightPrefab("lightPrefab", LightType.Point);

            Add(lightPrefab);
            lightPrefab.Transform.Position = new Vector3(0, 15, 15);
            lightPrefab.Light.Range        = 105;
            lightPrefab.Light.Intensity    = 2.0f;
            lightPrefab.Light.FallOf       = 5f;
            lightPrefab.Light.Color        = Color.Violet;
            lightPrefab.Transform.Rotation = new Vector3(-1, 1, 0);
            lightPrefab.Light.Angle        = 0.1f;
            lightPrefab.Light.ShadowGenerator.ShadowStrength = 0.6f; // FIXME need to be inverted
            lightPrefab.Light.ShadowGenerator.SetShadowMapSize(Application.GraphicsDevice, 1024);
            lightPrefab.EnableShadows = true;
            lightPrefab.AddComponent <LightSwitcher>();
            lightPrefab.AddComponent <LightMover>();
            lightPrefab.AddComponent <DemoBehaviour>();

            var lightPrefabSphere = lightPrefab.AddComponent <MeshRenderer>();

            lightPrefabSphere.Geometry = new SphereGeometry(2f, 4);
            lightPrefabSphere.Geometry.Build();
            lightPrefabSphere.CastShadow       = false;
            lightPrefabSphere.ReceiveShadow    = false;
            lightPrefabSphere.Material         = new SimpleMaterial(scene);
            lightPrefabSphere.Material.Texture = GraphicsHelper.CreateTexture(Color.Yellow, 1, 1);

            // Terrain
            var terrainMaterial = new StandardMaterial(scene);

            terrainMaterial.Texture   = GraphicsHelper.CreateBorderTexture(Color.LightGreen, Color.LightSeaGreen, 128, 128, 4);
            terrainMaterial.Shininess = 10;
            terrainMaterial.Tiling    = new Vector2(16);

            var terrain = new TerrainPrefab("terrain");

            terrain.Renderer.Geometry.Size = new Vector3(4);
            terrain.Renderer.Geometry.Build();
            terrain.Flatten();
            terrain.Renderer.Material = terrainMaterial;
            terrain.Transform.Translate(-terrain.Width >> 1, 0, -terrain.Depth / 2);
            Add(terrain);

            // Cube
            var cubeSuperMaterial = new StandardMaterial(scene);

            cubeSuperMaterial.Texture       = GraphicsHelper.CreateCheckboardTexture(Color.FloralWhite, Color.DodgerBlue);
            cubeSuperMaterial.DiffuseColor  = Color.WhiteSmoke;
            cubeSuperMaterial.SpecularColor = new Color(0.8f, 0.8f, 0.8f, 1.0f);
            cubeSuperMaterial.Shininess     = 10;
            cubeSuperMaterial.EmissiveColor = new Color(0f, 0.0f, 0.2f, 1.0f);

            var cubeScene = new SceneObject();

            cubeScene.Transform.Translate(0, 6f, 0);
            cubeScene.Transform.LocalScale = new Vector3(4.0f);
            cubeScene.Transform.Rotate((float)Math.PI / 4, 0, (float)Math.PI / 4);
            var autoRot = cubeScene.AddComponent <AutoRotation>();

            autoRot.Rotation = new Vector3(0, 0.01f, 0);
            Add(cubeScene);

            var cube = cubeScene.AddComponent <MeshRenderer>();

            cube.ReceiveShadow = false;
            cube.Geometry      = new CubeGeometry();
            cube.Geometry.Build();
            cube.Material = cubeSuperMaterial;

            cubeScene.AddComponent <BoxCollider>();

            // Skybox
            RenderSettings.Skybox.Generate(Application.GraphicsDevice, Application.Content, DemoGame.StarsSkybox, 500);

            Screen.ShowCursor = true;
        }
示例#28
0
        /// <summary>
        /// Creates the scene camera and updates the render texture. Should be called at least once before using the
        /// scene view. Should be called whenever the window is resized.
        /// </summary>
        /// <param name="width">Width of the scene render target, in pixels.</param>
        /// <param name="height">Height of the scene render target, in pixels.</param>
        private void UpdateRenderTexture(int width, int height)
        {
            width  = MathEx.Max(20, width);
            height = MathEx.Max(20, height);

            // Note: Depth buffer and readable flags are required because ScenePicking uses it
            Texture colorTex = Texture.Create2D(width, height, PixelFormat.RGBA8, TextureUsage.Render | TextureUsage.CPUReadable);
            Texture depthTex = Texture.Create2D(width, height, PixelFormat.D32_S8X24, TextureUsage.DepthStencil | TextureUsage.CPUReadable);

            renderTexture          = new RenderTexture(colorTex, depthTex);
            renderTexture.Priority = 1;

            if (camera == null)
            {
                SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
                camera = sceneCameraSO.AddComponent <Camera>();
                camera.Viewport.Target = renderTexture;
                camera.Viewport.Area   = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);

                Vector3 camPosition = new Vector3(0.0f, 1.7f, 5.0f);
                object  camPosObj   = ProjectSettings.GetObject <object>(CameraPositionKey);
                if (camPosObj is Vector3)
                {
                    camPosition = (Vector3)camPosObj;
                }

                Quaternion camRotation = Quaternion.Identity;
                object     camRotObj   = ProjectSettings.GetObject <object>(CameraRotationKey);
                if (camRotObj is Quaternion)
                {
                    camRotation = (Quaternion)camRotObj;
                }

                sceneCameraSO.Position = camPosition;
                sceneCameraSO.Rotation = camRotation;

                camera.Priority            = 2;
                camera.Viewport.ClearColor = ClearColor;
                camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera

                sceneCamera = sceneCameraSO.AddComponent <SceneCamera>();

                renderTextureGUI = new GUIRenderTexture(renderTexture);
                rtPanel.AddElement(renderTextureGUI);

                sceneGrid      = new SceneGrid(camera);
                sceneSelection = new SceneSelection(camera);
                sceneGizmos    = new SceneGizmos(camera);
                sceneHandles   = new SceneHandles(this, camera);
            }
            else
            {
                camera.Viewport.Target         = renderTexture;
                renderTextureGUI.RenderTexture = renderTexture;
            }

            Rect2I rtBounds = new Rect2I(0, 0, width, height);

            renderTextureGUI.Bounds = rtBounds;
            focusCatcher.Bounds     = GUIUtility.CalculateBounds(rtPanel, GUI);

            sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);

            // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
            // render target destroy/create cycle for every single pixel.

            camera.AspectRatio = width / (float)height;
        }
示例#29
0
        /// <summary>
        /// Tests saving, loading and updating of prefabs.
        /// </summary>
        private static void UnitTest4_Prefabs()
        {
            if (!EditorApplication.IsProjectLoaded)
            {
                Debug.LogWarning("Skipping unit test as no project is loaded.");
                return;
            }

            if (EditorApplication.IsSceneModified())
            {
                Debug.LogWarning("Cannot perform unit test as the current scene is modified.");
                return;
            }

            Action PrintSceneState = () =>
            {
                SceneObject root = Scene.Root;

                Stack <SceneObject> todo = new Stack <SceneObject>();
                todo.Push(root);

                StringBuilder output = new StringBuilder();
                while (todo.Count > 0)
                {
                    SceneObject so = todo.Pop();

                    int numChildren = so.GetNumChildren();
                    for (int i = numChildren - 1; i >= 0; i--)
                    {
                        SceneObject child = so.GetChild(i);

                        output.AppendLine(child.Name);
                        todo.Push(child);
                    }
                }

                Debug.Log(output);
            };

            // Disabled because it's a slow test, enable only when relevant (or when a build machine is set up)
            return;

            string oldScene = Scene.ActiveSceneUUID;

            Scene.Clear();

            try
            {
                // Simple scene save & load
                {
                    {
                        // unitTest4Scene_0.prefab:
                        // so0 (Comp1)
                        //  - so0_0
                        //  - so0_1 (Comp1)
                        //    - so0_1_0 (Comp1)
                        // so1 (Comp2)
                        //  - so1_0

                        SceneObject so0     = new SceneObject("so0");
                        SceneObject so1     = new SceneObject("so1");
                        SceneObject so0_0   = new SceneObject("so0_0");
                        SceneObject so0_1   = new SceneObject("so0_1");
                        SceneObject so1_0   = new SceneObject("so1_0");
                        SceneObject so0_1_0 = new SceneObject("so0_1_0");

                        so0_0.Parent   = so0;
                        so0_1.Parent   = so0;
                        so1_0.Parent   = so1;
                        so0_1_0.Parent = so0_1;

                        so0_1_0.LocalPosition = new Vector3(10.0f, 15.0f, 20.0f);
                        so0_1.LocalPosition   = new Vector3(1.0f, 2.0f, 3.0f);
                        so1_0.LocalPosition   = new Vector3(0, 123.0f, 0.0f);

                        UT1_Component1 comp0     = so0.AddComponent <UT1_Component1>();
                        UT1_Component2 comp1     = so1.AddComponent <UT1_Component2>();
                        UT1_Component1 comp1_1   = so0_1.AddComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0 = so0_1_0.AddComponent <UT1_Component1>();

                        comp0.otherSO        = so0_1_0;
                        comp0.otherComponent = comp1;

                        comp1_1.b = "originalValue2";

                        comp0_1_0.b               = "testValue";
                        comp0_1_0.otherSO         = so0;
                        comp0_1_0.otherComponent2 = comp0;

                        EditorApplication.SaveScene("unitTest4Scene_0.prefab");
                    }
                    {
                        EditorApplication.LoadScene("unitTest4Scene_0.prefab");

                        SceneObject sceneRoot = Scene.Root;
                        SceneObject so0       = sceneRoot.FindChild("so0", false);
                        SceneObject so1       = sceneRoot.FindChild("so1", false);
                        SceneObject so0_0     = so0.FindChild("so0_0", false);
                        SceneObject so0_1     = so0.FindChild("so0_1", false);
                        SceneObject so0_1_0   = so0_1.FindChild("so0_1_0", false);

                        Assert(so0_0 != null);
                        Assert(so0_1 != null);
                        Assert(so0_1_0 != null);

                        UT1_Component1 comp0     = so0.GetComponent <UT1_Component1>();
                        UT1_Component2 comp1     = so1.GetComponent <UT1_Component2>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();

                        Assert(comp0 != null);
                        Assert(comp1 != null);
                        Assert(comp0_1_0 != null);
                        Assert(comp0_1_0.b == "testValue");
                        Assert(comp0.otherSO == so0_1_0);
                        Assert(comp0.otherComponent == comp1);
                        Assert(comp0_1_0.otherSO == so0);
                        Assert(comp0_1_0.otherComponent2 == comp0);
                    }
                }

                Debug.Log("Passed stage 1");

                // Load & save a scene that contains a prefab and references its objects
                {
                    {
                        // unitTest4Scene_1.prefab:
                        // parentSO0
                        //  - [unitTest4Scene_0.prefab]
                        // parentSO1
                        //  - parentSO1_0 (Comp1)

                        Scene.Clear();

                        SceneObject parentSO0   = new SceneObject("parentSO0", false);
                        SceneObject parentSO1   = new SceneObject("parentSO1", false);
                        SceneObject parentSO1_0 = new SceneObject("parentSO1_0", false);

                        parentSO1_0.Parent      = parentSO1;
                        parentSO0.LocalPosition = new Vector3(50.0f, 50.0f, 50.0f);

                        UT1_Component1 parentComp1_0 = parentSO1_0.AddComponent <UT1_Component1>();

                        Prefab      scene0Prefab   = ProjectLibrary.Load <Prefab>("unitTest4Scene_0.prefab");
                        SceneObject prefabInstance = scene0Prefab.Instantiate();
                        prefabInstance.Parent        = parentSO0;
                        prefabInstance.LocalPosition = Vector3.Zero;

                        SceneObject so0     = prefabInstance.FindChild("so0", false);
                        SceneObject so1     = prefabInstance.FindChild("so1", false);
                        SceneObject so0_1   = so0.FindChild("so0_1", false);
                        SceneObject so1_0   = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);

                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();

                        parentComp1_0.otherSO         = so1_0;
                        parentComp1_0.otherComponent2 = comp0_1_0;

                        EditorApplication.SaveScene("unitTest4Scene_1.prefab");
                    }
                    {
                        EditorApplication.LoadScene("unitTest4Scene_1.prefab");

                        SceneObject parentSO0   = Scene.Root.FindChild("parentSO0", false);
                        SceneObject parentSO1   = Scene.Root.FindChild("parentSO1", false);
                        SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);

                        UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent <UT1_Component1>();

                        SceneObject prefabInstance = parentSO0.GetChild(0);
                        SceneObject so0            = prefabInstance.FindChild("so0", false);
                        SceneObject so1            = prefabInstance.FindChild("so1", false);
                        SceneObject so0_1          = so0.FindChild("so0_1", false);
                        SceneObject so1_0          = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0        = so0_1.FindChild("so0_1_0", false);

                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();

                        Assert(parentComp1_0.otherSO == so1_0);
                        Assert(parentComp1_0.otherComponent2 == comp0_1_0);
                    }
                }

                Debug.Log("Passed stage 2");

                // Modify prefab, reload the scene and ensure it is updated with modified prefab
                {
                    {
                        // unitTest4Scene_0.prefab:
                        // so0
                        //  - so0_1 (Comp1)
                        //    - so0_1_0 (Comp1)
                        // so1 (Comp1, Comp2)
                        //  - so1_0
                        //  - so1_1

                        Scene.Load("unitTest4Scene_0.prefab");

                        SceneObject sceneRoot = Scene.Root;
                        SceneObject so0       = sceneRoot.FindChild("so0", false);
                        SceneObject so0_0     = so0.FindChild("so0_0", false);
                        SceneObject so0_1     = so0.FindChild("so0_1", false);
                        SceneObject so1       = sceneRoot.FindChild("so1", false);
                        SceneObject so1_0     = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0   = so0_1.FindChild("so0_1_0", false);

                        SceneObject so1_1 = new SceneObject("so1_1");
                        so1_1.Parent = so1;

                        so0.RemoveComponent <UT1_Component1>();
                        UT1_Component1 comp1     = so1.AddComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();

                        so0_0.Destroy();

                        comp1.otherSO         = so1_0;
                        comp1.otherComponent2 = comp0_1_0;

                        comp0_1_0.otherSO         = so1_1;
                        comp0_1_0.otherComponent2 = comp1;
                        comp0_1_0.a = 123;
                        comp0_1_0.b = "modifiedValue";

                        so1.Name          = "so1_modified";
                        so1.LocalPosition = new Vector3(0, 999.0f, 0.0f);

                        EditorApplication.SaveScene("unitTest4Scene_0.prefab");
                    }

                    {
                        EditorApplication.LoadScene("unitTest4Scene_1.prefab");

                        SceneObject parentSO0   = Scene.Root.FindChild("parentSO0", false);
                        SceneObject parentSO1   = Scene.Root.FindChild("parentSO1", false);
                        SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);

                        UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent <UT1_Component1>();

                        SceneObject prefabInstance = parentSO0.GetChild(0);
                        SceneObject so0            = prefabInstance.FindChild("so0", false);
                        SceneObject so1            = prefabInstance.FindChild("so1_modified", false);
                        SceneObject so0_0          = so0.FindChild("so0_0", false);
                        SceneObject so0_1          = so0.FindChild("so0_1", false);
                        SceneObject so1_0          = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0        = so0_1.FindChild("so0_1_0", false);
                        SceneObject so1_1          = so1.FindChild("so1_1", false);

                        UT1_Component1 comp0     = so0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp1     = so1.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();

                        Assert(parentComp1_0.otherSO == so1_0);
                        Assert(parentComp1_0.otherComponent2 == comp0_1_0);
                        Assert(so1_1 != null);
                        Assert(so0_0 == null);
                        Assert(comp0 == null);
                        Assert(comp0_1_0.otherSO == so1_1);
                        Assert(comp0_1_0.otherComponent2 == comp1);
                        Assert(comp0_1_0.a == 123);
                        Assert(comp0_1_0.b == "modifiedValue");
                        Assert(comp1.otherSO == so1_0);
                        Assert(comp1.otherComponent2 == comp0_1_0);
                        Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
                    }
                }

                Debug.Log("Passed stage 3");

                // Make instance specific changes to the prefab, modify the prefab itself and ensure
                // both changes persist
                {
                    // Create new scene referencing the prefab and make instance modifications
                    {
                        // unitTest4Scene_2.prefab:
                        // parent2SO0
                        //  - [unitTest4Scene_0.prefab]
                        // parent2SO1
                        //  - parent2SO1_0 (Comp1)

                        // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
                        // so0 (Comp1(INSTANCE))
                        //  - so0_0 (INSTANCE)
                        //  - so0_1 (Comp1)
                        //    - so0_1_0 (Comp1)
                        // so1 (Comp2)
                        //  - so1_0

                        Scene.Clear();

                        SceneObject parent2SO0   = new SceneObject("parent2SO0");
                        SceneObject parent2SO1   = new SceneObject("parent2SO1");
                        SceneObject parent2SO1_0 = new SceneObject("parent2SO1_0");

                        parent2SO1_0.Parent = parent2SO1;

                        UT1_Component1 parentComp1_0 = parent2SO1_0.AddComponent <UT1_Component1>();

                        Prefab      scene0Prefab   = ProjectLibrary.Load <Prefab>("unitTest4Scene_0.prefab");
                        SceneObject prefabInstance = scene0Prefab.Instantiate();
                        prefabInstance.Parent = parent2SO0;

                        SceneObject so0 = prefabInstance.FindChild("so0", false);
                        SceneObject so1 = prefabInstance.FindChild("so1_modified", false);

                        SceneObject so0_1   = so0.FindChild("so0_1", false);
                        SceneObject so1_0   = so1.FindChild("so1_0", false);
                        SceneObject so1_1   = so1.FindChild("so1_1", false);
                        SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);

                        UT1_Component2 comp1     = so1.GetComponent <UT1_Component2>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1   = so0_1.GetComponent <UT1_Component1>();

                        SceneObject so0_0 = new SceneObject("so0_0");
                        so0_0.Parent = so0;
                        UT1_Component1 comp0 = so0.AddComponent <UT1_Component1>();

                        so1.RemoveComponent <UT1_Component1>();
                        so1_1.Destroy();

                        comp0.otherSO        = so0_1_0;
                        comp0.otherComponent = comp1;

                        parentComp1_0.otherSO         = so1_0;
                        parentComp1_0.otherComponent2 = comp0_1_0;

                        comp0_1_0.otherSO         = parent2SO1_0;
                        comp0_1_0.otherComponent2 = parentComp1_0;
                        comp0_1_0.b = "instanceValue";

                        comp0_1.b = "instanceValue2";

                        EditorApplication.SaveScene("unitTest4Scene_2.prefab");
                    }

                    Debug.Log("Passed stage 4.1");

                    // Reload the scene and ensure instance modifications remain
                    {
                        EditorApplication.LoadScene("unitTest4Scene_2.prefab");

                        SceneObject root         = Scene.Root;
                        SceneObject parent2SO0   = root.FindChild("parent2SO0", false);
                        SceneObject parent2SO1   = root.FindChild("parent2SO1", false);
                        SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);

                        SceneObject prefabInstance = parent2SO0.GetChild(0);

                        SceneObject so0     = prefabInstance.FindChild("so0", false);
                        SceneObject so1     = prefabInstance.FindChild("so1_modified", false);
                        SceneObject so0_0   = so0.FindChild("so0_0", false);
                        SceneObject so0_1   = so0.FindChild("so0_1", false);
                        SceneObject so1_0   = so1.FindChild("so1_0", false);
                        SceneObject so1_1   = so1.FindChild("so1_1", false);
                        SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);

                        UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0         = so0.GetComponent <UT1_Component1>();
                        UT1_Component2 comp1         = so1.GetComponent <UT1_Component2>();
                        UT1_Component1 comp11        = so1.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0     = so0_1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1       = so0_1.GetComponent <UT1_Component1>();

                        Assert(so0_0 != null);
                        Assert(comp0 != null);
                        Assert(so1_1 == null);
                        Assert(comp11 == null);

                        Assert(comp0.otherSO == so0_1_0);
                        Assert(comp0.otherComponent == comp1);

                        Assert(parentComp1_0.otherSO == so1_0);
                        Assert(parentComp1_0.otherComponent2 == comp0_1_0);

                        Debug.Log(comp0_1_0.otherSO == null);
                        if (comp0_1_0.otherSO != null)
                        {
                            Debug.Log(comp0_1_0.otherSO.InstanceId + " - " + parent2SO1_0.InstanceId);
                        }

                        Assert(comp0_1_0.otherSO == parent2SO1_0);
                        Assert(comp0_1_0.otherComponent2 == parentComp1_0);
                        Assert(comp0_1_0.b == "instanceValue");

                        Assert(comp0_1.b == "instanceValue2");
                    }

                    Debug.Log("Passed stage 4.2");

                    // Load original scene and ensure instance modifications didn't influence it
                    {
                        EditorApplication.LoadScene("unitTest4Scene_1.prefab");

                        SceneObject parentSO0   = Scene.Root.FindChild("parentSO0", false);
                        SceneObject parentSO1   = Scene.Root.FindChild("parentSO1", false);
                        SceneObject parentSO1_0 = parentSO1.FindChild("parentSO1_0", false);

                        UT1_Component1 parentComp1_0 = parentSO1_0.GetComponent <UT1_Component1>();

                        SceneObject prefabInstance = parentSO0.GetChild(0);
                        SceneObject so0            = prefabInstance.FindChild("so0", false);
                        SceneObject so1            = prefabInstance.FindChild("so1_modified", false);
                        SceneObject so0_0          = so0.FindChild("so0_0", false);
                        SceneObject so0_1          = so0.FindChild("so0_1", false);
                        SceneObject so1_0          = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0        = so0_1.FindChild("so0_1_0", false);
                        SceneObject so1_1          = so1.FindChild("so1_1", false);

                        UT1_Component1 comp0     = so0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp1     = so1.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1   = so0_1.GetComponent <UT1_Component1>();

                        Assert(parentComp1_0.otherSO == so1_0);
                        Assert(parentComp1_0.otherComponent2 == comp0_1_0);
                        Assert(so1_1 != null);
                        Assert(so0_0 == null);
                        Assert(comp0 == null);
                        Assert(comp0_1_0.otherSO == so1_1);
                        Assert(comp0_1_0.otherComponent2 == comp1);
                        Assert(comp0_1_0.a == 123);
                        Assert(comp0_1_0.b == "modifiedValue");
                        Assert(comp1.otherSO == so1_0);
                        Assert(comp1.otherComponent2 == comp0_1_0);
                        Assert(comp0_1.b == "originalValue2");
                        Assert(MathEx.ApproxEquals(so1.LocalPosition.y, 999.0f));
                    }

                    Debug.Log("Passed stage 4.3");

                    // Modify prefab and ensure both prefab and instance modifications remain
                    {
                        // unitTest4Scene_0.prefab:
                        // so0 (Comp1)
                        //  - so0_1
                        //    - so0_1_0 (Comp1)
                        // so1 (Comp1, Comp2)
                        //  - so1_1
                        //  - so1_2 (Comp1)

                        // unitTest4Scene_0.prefab (unitTest4Scene_2.prefab instance):
                        // so0 (Comp1)
                        //  - so0_0
                        //  - so0_1 (Comp1)
                        //    - so0_1_0 (Comp1)
                        // so1 (Comp2)
                        //  - so1_2 (Comp1)

                        Scene.Load("unitTest4Scene_0.prefab");

                        SceneObject sceneRoot = Scene.Root;
                        SceneObject so0       = sceneRoot.FindChild("so0", false);
                        SceneObject so0_1     = so0.FindChild("so0_1", false);
                        SceneObject so1       = sceneRoot.FindChild("so1_modified", false);
                        SceneObject so1_0     = so1.FindChild("so1_0", false);
                        SceneObject so0_1_0   = so0_1.FindChild("so0_1_0", false);

                        SceneObject so1_2 = new SceneObject("so1_2");
                        so1_2.Parent = so1;

                        so0.AddComponent <UT1_Component1>();
                        so0_1.RemoveComponent <UT1_Component1>();
                        so1_0.Destroy();

                        UT1_Component1 comp3     = so1_2.AddComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0 = so0_1_0.GetComponent <UT1_Component1>();
                        comp0_1_0.b = "modifiedValueAgain";
                        so1.Name    = "so1_modifiedAgain";

                        comp3.otherSO         = so0_1;
                        comp3.otherComponent2 = comp0_1_0;

                        EditorApplication.SaveScene("unitTest4Scene_0.prefab");
                    }

                    Debug.Log("Passed stage 4.4");

                    // Reload the scene and ensure both instance and prefab modifications remain
                    {
                        EditorApplication.LoadScene("unitTest4Scene_2.prefab");

                        SceneObject root         = Scene.Root;
                        SceneObject parent2SO0   = root.FindChild("parent2SO0", false);
                        SceneObject parent2SO1   = root.FindChild("parent2SO1", false);
                        SceneObject parent2SO1_0 = parent2SO1.FindChild("parent2SO1_0", false);

                        SceneObject prefabInstance = parent2SO0.GetChild(0);

                        SceneObject so0     = prefabInstance.FindChild("so0", false);
                        SceneObject so1     = prefabInstance.FindChild("so1_modifiedAgain", false);
                        SceneObject so0_0   = so0.FindChild("so0_0", false);
                        SceneObject so0_1   = so0.FindChild("so0_1", false);
                        SceneObject so1_0   = so1.FindChild("so1_0", false);
                        SceneObject so1_1   = so1.FindChild("so1_1", false);
                        SceneObject so1_2   = so1.FindChild("so1_2", false);
                        SceneObject so0_1_0 = so0_1.FindChild("so0_1_0", false);

                        UT1_Component1 parentComp1_0 = parent2SO1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0         = so0.GetComponent <UT1_Component1>();
                        UT1_Component2 comp1         = so1.GetComponent <UT1_Component2>();
                        UT1_Component1 comp11        = so1.GetComponent <UT1_Component1>();
                        UT1_Component1 comp0_1_0     = so0_1_0.GetComponent <UT1_Component1>();
                        UT1_Component1 comp3         = so1_2.AddComponent <UT1_Component1>();

                        // Check instance modifications (they should override any prefab modifications)
                        Assert(so0_0 != null);
                        Assert(comp0 != null);
                        Assert(so1_1 == null);
                        Assert(comp11 == null);

                        Assert(comp0.otherSO == so0_1_0);
                        Assert(comp0.otherComponent == comp1);

                        Assert(parentComp1_0.otherSO == so1_0);
                        Assert(parentComp1_0.otherComponent2 == comp0_1_0);

                        Assert(comp0_1_0.otherSO == parent2SO1_0);
                        Assert(comp0_1_0.otherComponent2 == parentComp1_0);
                        Assert(comp0_1_0.b == "instanceValue");

                        // Check prefab modifications
                        Assert(so1_0 == null);
                        Assert(so1.Name == "so1_modifiedAgain");
                        Assert(comp3.otherSO == so0_1);
                        Assert(comp3.otherComponent2 == comp0_1_0);
                    }

                    Debug.Log("Passed stage 4.5");
                }
            }
            catch
            {
                PrintSceneState();

                throw;
            }
            finally
            {
                if (!string.IsNullOrEmpty(oldScene))
                {
                    Scene.Load(ProjectLibrary.GetPath(oldScene));
                }
                else
                {
                    Scene.Clear();
                }

                ProjectLibrary.Delete("unitTest4Scene_0.prefab");
                ProjectLibrary.Delete("unitTest4Scene_1.prefab");
                ProjectLibrary.Delete("unitTest4Scene_2.prefab");
            }
        }
示例#30
0
        private void OnEditorUpdate()
        {
            if (HasFocus)
            {
                if (!Input.IsPointerButtonHeld(PointerButton.Right))
                {
                    if (VirtualInput.IsButtonDown(toggleProfilerOverlayKey))
                        EditorSettings.SetBool(ProfilerOverlayActiveKey, !EditorSettings.GetBool(ProfilerOverlayActiveKey));

                    if (VirtualInput.IsButtonDown(viewToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.View;

                    if (VirtualInput.IsButtonDown(moveToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Move;

                    if (VirtualInput.IsButtonDown(rotateToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Rotate;

                    if (VirtualInput.IsButtonDown(scaleToolKey))
                        EditorApplication.ActiveSceneTool = SceneViewTool.Scale;

                    if (VirtualInput.IsButtonDown(duplicateKey))
                    {
                        SceneObject[] selectedObjects = Selection.SceneObjects;
                        CleanDuplicates(ref selectedObjects);

                        if (selectedObjects.Length > 0)
                        {
                            String message;
                            if (selectedObjects.Length == 1)
                                message = "Duplicated " + selectedObjects[0].Name;
                            else
                                message = "Duplicated " + selectedObjects.Length + " elements";

                            UndoRedo.CloneSO(selectedObjects, message);
                            EditorApplication.SetSceneDirty();
                        }
                    }

                    if (VirtualInput.IsButtonDown(deleteKey))
                    {
                        SceneObject[] selectedObjects = Selection.SceneObjects;
                        CleanDuplicates(ref selectedObjects);

                        if (selectedObjects.Length > 0)
                        {
                            foreach (var so in selectedObjects)
                            {
                                string message = "Deleted " + so.Name;
                                UndoRedo.DeleteSO(so, message);
                            }

                            EditorApplication.SetSceneDirty();
                        }
                    }
                }
            }

            // Refresh GUI buttons if needed (in case someones changes the values from script)
            if (editorSettingsHash != EditorSettings.Hash)
            {
                UpdateButtonStates();
                UpdateProfilerOverlay();
                editorSettingsHash = EditorSettings.Hash;
            }

            // Update scene view handles and selection
            sceneGizmos.Draw();
            sceneGrid.Draw();

            bool handleActive = false;
            if (Input.IsPointerButtonUp(PointerButton.Left))
            {
                if (sceneHandles.IsActive())
                {
                    sceneHandles.ClearSelection();
                    handleActive = true;
                }

                if (sceneAxesGUI.IsActive())
                {
                    sceneAxesGUI.ClearSelection();
                    handleActive = true;
                }
            }

            Vector2I scenePos;
            bool inBounds = ScreenToScenePos(Input.PointerPosition, out scenePos);

            bool draggedOver = DragDrop.DragInProgress || DragDrop.DropInProgress;
            draggedOver &= IsPointerHovering && inBounds && DragDrop.Type == DragDropType.Resource;

            if (draggedOver)
            {
                if (DragDrop.DropInProgress)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        Selection.SceneObject = draggedSO;
                        EditorApplication.SetSceneDirty();
                    }

                    draggedSO = null;
                }
                else
                {
                    if (!dragActive)
                    {
                        dragActive = true;

                        ResourceDragDropData dragData = (ResourceDragDropData)DragDrop.Data;

                        string[] draggedPaths = dragData.Paths;

                        for (int i = 0; i < draggedPaths.Length; i++)
                        {
                            LibraryEntry entry = ProjectLibrary.GetEntry(draggedPaths[i]);
                            if (entry != null && entry.Type == LibraryEntryType.File)
                            {
                                FileEntry fileEntry = (FileEntry) entry;
                                if (fileEntry.ResType == ResourceType.Mesh)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        string meshName = Path.GetFileNameWithoutExtension(draggedPaths[i]);
                                        draggedSO = UndoRedo.CreateSO(meshName, "Created a new Renderable \"" + meshName + "\"");
                                        Mesh mesh = ProjectLibrary.Load<Mesh>(draggedPaths[i]);

                                        Renderable renderable = draggedSO.AddComponent<Renderable>();
                                        renderable.Mesh = mesh;
                                    }

                                    break;
                                }
                                else if (fileEntry.ResType == ResourceType.Prefab)
                                {
                                    if (!string.IsNullOrEmpty(draggedPaths[i]))
                                    {
                                        Prefab prefab = ProjectLibrary.Load<Prefab>(draggedPaths[i]);
                                        draggedSO = UndoRedo.Instantiate(prefab, "Instantiating " + prefab.Name);
                                    }

                                    break;
                                }
                            }
                        }
                    }

                    if (draggedSO != null)
                    {
                        Ray worldRay = camera.ScreenToWorldRay(scenePos);
                        draggedSO.Position = worldRay*DefaultPlacementDepth;
                    }
                }

                return;
            }
            else
            {
                if (dragActive)
                {
                    dragActive = false;

                    if (draggedSO != null)
                    {
                        draggedSO.Destroy();
                        draggedSO = null;
                    }
                }
            }

            if (HasFocus)
            {
                cameraController.EnableInput(true);

                if (inBounds)
                {
                    if (Input.IsPointerButtonDown(PointerButton.Left))
                    {
                        Rect2I sceneAxesGUIBounds = new Rect2I(Width - HandleAxesGUISize - HandleAxesGUIPaddingX, 
                            HandleAxesGUIPaddingY, HandleAxesGUISize, HandleAxesGUISize);

                        if (sceneAxesGUIBounds.Contains(scenePos))
                            sceneAxesGUI.TrySelect(scenePos);
                        else
                            sceneHandles.TrySelect(scenePos);
                    }
                    else if (Input.IsPointerButtonUp(PointerButton.Left))
                    {
                        if (!handleActive)
                        {
                            bool ctrlHeld = Input.IsButtonHeld(ButtonCode.LeftControl) ||
                                            Input.IsButtonHeld(ButtonCode.RightControl);

                            sceneSelection.PickObject(scenePos, ctrlHeld);
                        }
                    }
                }
            }
            else
                cameraController.EnableInput(false);

            SceneHandles.BeginInput();
            sceneHandles.UpdateInput(scenePos, Input.PointerDelta);
            sceneHandles.Draw();

            sceneAxesGUI.UpdateInput(scenePos);
            sceneAxesGUI.Draw();
            SceneHandles.EndInput();

            sceneSelection.Draw();

            UpdateGridMode();

            if (VirtualInput.IsButtonDown(frameKey))
                cameraController.FrameSelected();
        }
示例#31
0
        /// <summary>
        /// Tests managed object serialization and deserialization.
        /// </summary>
        static void UnitTest1_ManagedSerialization()
        {
            SceneObject    otherSO       = new SceneObject("OtherSO");
            UT1_Component2 dbgComponent2 = otherSO.AddComponent <UT1_Component2>();

            dbgComponent2.a2 = 33;

            SceneObject    so           = new SceneObject("TestSO");
            UT1_Component1 dbgComponent = so.AddComponent <UT1_Component1>();

            dbgComponent.a = 5;
            dbgComponent.b = "SomeTestVal";
            dbgComponent.complex.someValue      = 19;
            dbgComponent.complex.anotherValue   = "AnotherValue";
            dbgComponent.complex2.someValue2    = 21;
            dbgComponent.complex2.anotherValue2 = "AnotherValue2";

            dbgComponent.arrA       = new int[5];
            dbgComponent.arrA[4]    = 5;
            dbgComponent.arrB       = new string[5];
            dbgComponent.arrB[4]    = "ArrAnotherValue";
            dbgComponent.arrComplex = new UT1_SerzObj[5];
            dbgComponent.arrComplex[4].someValue    = 99;
            dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
            dbgComponent.arrComplex2                  = new UT1_SerzCls[5];
            dbgComponent.arrComplex2[4]               = new UT1_SerzCls();
            dbgComponent.arrComplex2[4].someValue2    = 101;
            dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";

            dbgComponent.listA = new List <int>();
            dbgComponent.listA.Add(5);
            dbgComponent.listB = new List <string>();
            dbgComponent.listB.Add("ListAnotherValue");
            dbgComponent.listB.Add(null);
            dbgComponent.listComplex = new List <UT1_SerzObj>();
            dbgComponent.listComplex.Add(new UT1_SerzObj());
            dbgComponent.listComplex.Add(new UT1_SerzObj(99, "ListComplexAnotherValue"));
            dbgComponent.listComplex2 = new List <UT1_SerzCls>();
            dbgComponent.listComplex2.Add(new UT1_SerzCls());
            dbgComponent.listComplex2[0].someValue2    = 101;
            dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
            dbgComponent.listComplex2.Add(null);

            dbgComponent.dictA     = new Dictionary <int, string>();
            dbgComponent.dictA[5]  = "value";
            dbgComponent.dictA[10] = "anotherValue";

            dbgComponent.dictB         = new Dictionary <string, UT1_SerzObj>();
            dbgComponent.dictB["key1"] = new UT1_SerzObj(99, "DictComplexValue");

            dbgComponent.otherComponent = dbgComponent2;
            dbgComponent.otherSO        = otherSO;

            Internal_UT1_GameObjectClone(so);

            System.Diagnostics.Debug.Assert(so.GetNumChildren() == 1);

            for (int i = 0; i < so.GetNumChildren(); i++)
            {
                SceneObject childSO = so.GetChild(i);

                UT1_Component1 otherComponent = childSO.GetComponent <UT1_Component1>();
                DebugUnit.Assert(otherComponent.a == 5);
                DebugUnit.Assert(otherComponent.b == "SomeTestVal");
                DebugUnit.Assert(otherComponent.complex.someValue == 19);
                DebugUnit.Assert(otherComponent.complex2.anotherValue2 == "AnotherValue2");

                DebugUnit.Assert(otherComponent.arrA[4] == 5);
                DebugUnit.Assert(otherComponent.arrB[4] == "ArrAnotherValue");
                DebugUnit.Assert(otherComponent.arrComplex[4].someValue == 99);
                DebugUnit.Assert(otherComponent.arrComplex2[4].anotherValue2 == "ArrComplex2AnotherValue");

                DebugUnit.Assert(otherComponent.listA[0] == 5);
                DebugUnit.Assert(otherComponent.listB[0] == "ListAnotherValue");
                DebugUnit.Assert(otherComponent.listComplex[1].someValue == 99);
                DebugUnit.Assert(otherComponent.listComplex2[0].anotherValue2 == "ListComplexAnotherValue");
            }

            so.Destroy();
            otherSO.Destroy();
        }
示例#32
0
        private void AddLines()
        {
            _lineRenderer          = SceneObject.AddComponent <MeshRenderable>();
            _lineRenderer.Material = _material;

            var mesh = new Mesh();

            mesh.Type     = PrimitiveType.Lines;
            mesh.Vertices = new Vector3[] {
                new Vector3(0, 0, 0),
                new Vector3(1, 0, 0), //X

                new Vector3(0, 0, 0),
                new Vector3(0, 1, 0), //Y

                new Vector3(0, 0, 0),
                new Vector3(0, 0, 1),  //Z

                new Vector3(CrossMid, CrossMid - CrossSize, 0),
                new Vector3(CrossMid, CrossMid, 0),
                new Vector3(CrossMid - CrossSize, CrossMid, 0),
                new Vector3(CrossMid, CrossMid, 0), //XY

                new Vector3(CrossMid, 0, CrossMid - CrossSize),
                new Vector3(CrossMid, 0, CrossMid),
                new Vector3(CrossMid - CrossSize, 0, CrossMid),
                new Vector3(CrossMid, 0, CrossMid), //XZ

                new Vector3(0, CrossMid, CrossMid - CrossSize),
                new Vector3(0, CrossMid, CrossMid),
                new Vector3(0, CrossMid - CrossSize, CrossMid),
                new Vector3(0, CrossMid, CrossMid), //YZ
            };
            mesh.Colors = new Vector3[] {
                new Vector3(1, 0, 0),
                new Vector3(1, 0, 0), //X

                new Vector3(0, 1, 0),
                new Vector3(0, 1, 0), //Y

                new Vector3(0, 0, 1),
                new Vector3(0, 0, 1), //Z

                new Vector3(1, 0, 0),
                new Vector3(1, 0, 0),
                new Vector3(0, 1, 0),
                new Vector3(0, 1, 0), //XY

                new Vector3(1, 0, 0),
                new Vector3(1, 0, 0),
                new Vector3(0, 0, 1),
                new Vector3(0, 0, 1), //XZ

                new Vector3(0, 1, 0),
                new Vector3(0, 1, 0),
                new Vector3(0, 0, 1),
                new Vector3(0, 0, 1), //YZ
            };

            mesh.CalculateBounds();
            _lineRenderer.Mesh = mesh;
        }
示例#33
0
        /// <summary>
        /// Creates the scene camera and updates the render texture. Should be called at least once before using the
        /// scene view. Should be called whenever the window is resized.
        /// </summary>
        /// <param name="width">Width of the scene render target, in pixels.</param>
        /// <param name="height">Height of the scene render target, in pixels.</param>
        private void UpdateRenderTexture(int width, int height)
        {
            width = MathEx.Max(20, width);
            height = MathEx.Max(20, height);

            // Note: Depth buffer and readable flags are required because ScenePicking uses it
            Texture2D colorTex = new Texture2D(width, height, PixelFormat.R8G8B8A8, TextureUsage.Render | TextureUsage.CPUReadable);
            Texture2D depthTex = new Texture2D(width, height, PixelFormat.D32_S8X24, TextureUsage.DepthStencil | TextureUsage.CPUReadable);

            renderTexture = new RenderTexture2D(colorTex, depthTex);
            renderTexture.Priority = 1;

            if (camera == null)
            {
                SceneObject sceneCameraSO = new SceneObject("SceneCamera", true);
                camera = sceneCameraSO.AddComponent<Camera>();
                camera.Target = renderTexture;
                camera.ViewportRect = new Rect2(0.0f, 0.0f, 1.0f, 1.0f);

                sceneCameraSO.Position = new Vector3(0, 0.5f, 1);
                sceneCameraSO.LookAt(new Vector3(0, 0.5f, 0));

                camera.Priority = 2;
                camera.NearClipPlane = 0.05f;
                camera.FarClipPlane = 2500.0f;
                camera.ClearColor = ClearColor;
                camera.Layers = UInt64.MaxValue & ~SceneAxesHandle.LAYER; // Don't draw scene axes in this camera

                cameraController = sceneCameraSO.AddComponent<SceneCamera>();

                renderTextureGUI = new GUIRenderTexture(renderTexture);
                rtPanel.AddElement(renderTextureGUI);

                sceneGrid = new SceneGrid(camera);
                sceneSelection = new SceneSelection(camera);
                sceneGizmos = new SceneGizmos(camera);
                sceneHandles = new SceneHandles(this, camera);
            }
            else
            {
                camera.Target = renderTexture;
                renderTextureGUI.RenderTexture = renderTexture;
            }

            Rect2I rtBounds = new Rect2I(0, 0, width, height);
            renderTextureGUI.Bounds = rtBounds;
            focusCatcher.Bounds = GUIUtility.CalculateBounds(rtPanel, GUI);

            sceneAxesGUI.SetPosition(width - HandleAxesGUISize - HandleAxesGUIPaddingX, HandleAxesGUIPaddingY);

            // TODO - Consider only doing the resize once user stops resizing the widget in order to reduce constant
            // render target destroy/create cycle for every single pixel.

            camera.AspectRatio = width / (float)height;

            if (profilerCamera != null)
                profilerCamera.Target = renderTexture;
        }
示例#34
0
        private void OnEditorUpdate()
        {
            if (currentType == InspectorType.SceneObject)
            {
                Component[] allComponents   = activeSO.GetComponents();
                bool        requiresRebuild = allComponents.Length != inspectorComponents.Count;

                if (!requiresRebuild)
                {
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        if (inspectorComponents[i].instanceId != allComponents[i].InstanceId)
                        {
                            requiresRebuild = true;
                            break;
                        }
                    }
                }

                if (requiresRebuild)
                {
                    SceneObject so = activeSO;
                    Clear();
                    SetObjectToInspect(so);
                }
                else
                {
                    RefreshSceneObjectFields(false);

                    InspectableState componentModifyState = InspectableState.NotModified;
                    for (int i = 0; i < inspectorComponents.Count; i++)
                    {
                        componentModifyState |= inspectorComponents[i].inspector.Refresh();
                    }

                    if (componentModifyState.HasFlag(InspectableState.ModifyInProgress))
                    {
                        EditorApplication.SetSceneDirty();
                    }

                    modifyState |= componentModifyState;
                }
            }
            else if (currentType == InspectorType.Resource)
            {
                inspectorResource.inspector.Refresh();
            }

            // Detect drag and drop
            bool isValidDrag = false;

            if (activeSO != null)
            {
                if ((DragDrop.DragInProgress || DragDrop.DropInProgress) && DragDrop.Type == DragDropType.Resource)
                {
                    Vector2I windowPos     = ScreenToWindowPos(Input.PointerPosition);
                    Vector2I scrollPos     = windowPos;
                    Rect2I   contentBounds = inspectorLayout.Bounds;
                    scrollPos.x -= contentBounds.x;
                    scrollPos.y -= contentBounds.y;

                    bool   isInBounds = false;
                    Rect2I dropArea   = new Rect2I();
                    foreach (var bounds in dropAreas)
                    {
                        if (bounds.Contains(scrollPos))
                        {
                            isInBounds = true;
                            dropArea   = bounds;
                            break;
                        }
                    }

                    Type draggedComponentType = null;
                    if (isInBounds)
                    {
                        ResourceDragDropData dragData = DragDrop.Data as ResourceDragDropData;
                        if (dragData != null)
                        {
                            foreach (var resPath in dragData.Paths)
                            {
                                ResourceMeta meta = ProjectLibrary.GetMeta(resPath);
                                if (meta != null)
                                {
                                    if (meta.ResType == ResourceType.ScriptCode)
                                    {
                                        ScriptCode scriptFile = ProjectLibrary.Load <ScriptCode>(resPath);

                                        if (scriptFile != null)
                                        {
                                            Type[] scriptTypes = scriptFile.Types;
                                            foreach (var type in scriptTypes)
                                            {
                                                if (type.IsSubclassOf(typeof(Component)))
                                                {
                                                    draggedComponentType = type;
                                                    isValidDrag          = true;
                                                    break;
                                                }
                                            }

                                            if (draggedComponentType != null)
                                            {
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    if (isValidDrag)
                    {
                        scrollAreaHighlight.Bounds = dropArea;

                        if (DragDrop.DropInProgress)
                        {
                            activeSO.AddComponent(draggedComponentType);

                            modifyState = InspectableState.Modified;
                            EditorApplication.SetSceneDirty();
                        }
                    }
                }
            }

            if (scrollAreaHighlight != null)
            {
                scrollAreaHighlight.Active = isValidDrag;
            }
        }
示例#35
0
        /// <summary>
        /// Tests managed object serialization and deserialization.
        /// </summary>
        static void UnitTest1_ManagedSerialization()
        {
            SceneObject otherSO = new SceneObject("OtherSO");
            UT1_Component2 dbgComponent2 = otherSO.AddComponent<UT1_Component2>();
            dbgComponent2.a2 = 33;

            SceneObject so = new SceneObject("TestSO");
            UT1_Component1 dbgComponent = so.AddComponent<UT1_Component1>();

            dbgComponent.a = 5;
            dbgComponent.b = "SomeTestVal";
            dbgComponent.complex.someValue = 19;
            dbgComponent.complex.anotherValue = "AnotherValue";
            dbgComponent.complex2.someValue2 = 21;
            dbgComponent.complex2.anotherValue2 = "AnotherValue2";

            dbgComponent.arrA = new int[5];
            dbgComponent.arrA[4] = 5;
            dbgComponent.arrB = new string[5];
            dbgComponent.arrB[4] = "ArrAnotherValue";
            dbgComponent.arrComplex = new UT1_SerzObj[5];
            dbgComponent.arrComplex[4].someValue = 99;
            dbgComponent.arrComplex[4].anotherValue = "ArrComplexAnotherValue";
            dbgComponent.arrComplex2 = new UT1_SerzCls[5];
            dbgComponent.arrComplex2[4] = new UT1_SerzCls();
            dbgComponent.arrComplex2[4].someValue2 = 101;
            dbgComponent.arrComplex2[4].anotherValue2 = "ArrComplex2AnotherValue";

            dbgComponent.listA = new List<int>();
            dbgComponent.listA.Add(5);
            dbgComponent.listB = new List<string>();
            dbgComponent.listB.Add("ListAnotherValue");
            dbgComponent.listB.Add(null);
            dbgComponent.listComplex = new List<UT1_SerzObj>();
            dbgComponent.listComplex.Add(new UT1_SerzObj());
            dbgComponent.listComplex.Add(new UT1_SerzObj(99, "ListComplexAnotherValue"));
            dbgComponent.listComplex2 = new List<UT1_SerzCls>();
            dbgComponent.listComplex2.Add(new UT1_SerzCls());
            dbgComponent.listComplex2[0].someValue2 = 101;
            dbgComponent.listComplex2[0].anotherValue2 = "ListComplexAnotherValue";
            dbgComponent.listComplex2.Add(null);

            dbgComponent.dictA = new Dictionary<int, string>();
            dbgComponent.dictA[5] = "value";
            dbgComponent.dictA[10] = "anotherValue";

            dbgComponent.dictB = new Dictionary<string, UT1_SerzObj>();
            dbgComponent.dictB["key1"] = new UT1_SerzObj(99, "DictComplexValue");

            dbgComponent.otherComponent = dbgComponent2;
            dbgComponent.otherSO = otherSO;

            Internal_UT1_GameObjectClone(so);

            System.Diagnostics.Debug.Assert(so.GetNumChildren() == 1);

            for (int i = 0; i < so.GetNumChildren(); i++)
            {
                SceneObject childSO = so.GetChild(i);

                UT1_Component1 otherComponent = childSO.GetComponent<UT1_Component1>();
                DebugUnit.Assert(otherComponent.a == 5);
                DebugUnit.Assert(otherComponent.b == "SomeTestVal");
                DebugUnit.Assert(otherComponent.complex.someValue == 19);
                DebugUnit.Assert(otherComponent.complex2.anotherValue2 == "AnotherValue2");

                DebugUnit.Assert(otherComponent.arrA[4] == 5);
                DebugUnit.Assert(otherComponent.arrB[4] == "ArrAnotherValue");
                DebugUnit.Assert(otherComponent.arrComplex[4].someValue == 99);
                DebugUnit.Assert(otherComponent.arrComplex2[4].anotherValue2 == "ArrComplex2AnotherValue");

                DebugUnit.Assert(otherComponent.listA[0] == 5);
                DebugUnit.Assert(otherComponent.listB[0] == "ListAnotherValue");
                DebugUnit.Assert(otherComponent.listComplex[1].someValue == 99);
                DebugUnit.Assert(otherComponent.listComplex2[0].anotherValue2 == "ListComplexAnotherValue");
            }

            so.Destroy();
            otherSO.Destroy();
        }