コード例 #1
0
        public IEnumerator LoadAsyncActivation(string moduleName, string id)
        {
            IApplication application = CreateApplication(moduleName);

            application.Initialize();

            var          module = application.GetModule <ISceneModule>();
            Task <Scene> task   = module.LoadAsync(id);

            while (!task.IsCompleted)
            {
                yield return(null);
            }

            Scene          scene     = task.Result;
            AsyncOperation operation = scene.GetOperation();

            Assert.True(scene.IsValid(), "Load: scene.IsValid()");
            Assert.False(scene.isLoaded, "Load: scene.isLoaded");
            Assert.NotNull(operation);
            Assert.GreaterOrEqual(operation.progress, 0.9F);
            Assert.AreEqual(module.Scenes.Get(id).Address, "Assets/UGF.Module.Scenes.Runtime.Tests/Resources/SceneTest.unity");

            scene.Activate();

            Assert.False(scene.TryGetOperation(out _));

            yield return(operation);

            Assert.True(scene.IsValid(), "Load: scene.IsValid()");
            Assert.True(scene.isLoaded, "Load: scene.isLoaded");
        }
コード例 #2
0
ファイル: CoroutinesTest.cs プロジェクト: raycrasher/duality
        [Test] public void Cancelling()
        {
            Scene scene = new Scene();

            scene.Activate();

            CoroutineObject obj       = new CoroutineObject();
            Coroutine       coroutine = scene.StartCoroutine(this.BasicRoutine(obj));

            // All code until first yield is already executed
            Assert.AreEqual(10, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();

            // All code until second yield is executed
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            coroutine.Cancel();
            scene.Update();

            // Coroutine disposed
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Cancelled);

            scene.Update();

            // No further changes
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Cancelled);
        }
コード例 #3
0
ファイル: CoroutinesTest.cs プロジェクト: raycrasher/duality
        [Test] public void WaitTwoSeconds()
        {
            Scene scene = new Scene();

            scene.Activate();

            int             secondsToWait = 2;
            CoroutineObject obj           = new CoroutineObject();
            Coroutine       coroutine     = scene.StartCoroutine(this.WaitSeconds(obj, secondsToWait));

            // All code until first yield is already executed
            Assert.AreEqual(10, obj.Value);
            TimeSpan startTime = Time.GameTimer;

            // Waiting...
            while (obj.Value == 10)
            {
                scene.Update();
                Time.FrameTick(true, true);
            }

            double secondsElapsed = (Time.GameTimer - startTime).TotalSeconds;

            Assert.AreEqual(20, obj.Value);

            // Not being overly precise about the 2 seconds wait..
            Assert.GreaterOrEqual(secondsElapsed, secondsToWait);
            Assert.LessOrEqual(secondsElapsed, secondsToWait + Time.SecondsPerFrame * 10);

            Assert.True(coroutine.Status == CoroutineStatus.Complete);
        }
コード例 #4
0
        public static async Task ActivateAsync(this Scene scene)
        {
            scene.Activate();

            while (!scene.isLoaded)
            {
                await Task.Yield();
            }
        }
コード例 #5
0
 public void LoadScene(Scene newScene)
 {
     if (activeScene != null)
     {
         activeScene.Deactivate();
     }
     activeScene = newScene;
     if (activeScene != null)
     {
         activeScene.Activate();
     }
 }
コード例 #6
0
        /// <summary>
        /// This test will determine whether a <see cref="Scene"/> can be activated, updated and deactivated
        /// in isolation from the global <see cref="Scene.Current"/> setting.
        /// </summary>
        [Test] public void IsolatedSimulation()
        {
            // Create an isolated new test scene with a ball and a platform
            Scene scene = new Scene();

            GameObject ball          = new GameObject("Ball");
            Transform  ballTransform = ball.AddComponent <Transform>();
            RigidBody  ballBody      = ball.AddComponent <RigidBody>();

            ballBody.AddShape(new CircleShapeInfo(32.0f, new Vector2(0.0f, 0.0f), 1.0f));
            ball.AddComponent <RigidBodyRenderer>();
            scene.AddObject(ball);

            GameObject platform          = new GameObject("Platform");
            Transform  platformTransform = platform.AddComponent <Transform>();

            platformTransform.Pos = new Vector3(0.0f, 128.0f, 0.0f);
            RigidBody platformBody = platform.AddComponent <RigidBody>();

            platformBody.AddShape(new ChainShapeInfo(new[] { new Vector2(-128.0f, 0.0f), new Vector2(128.0f, 0.0f) }));
            platformBody.BodyType = BodyType.Static;
            platform.AddComponent <RigidBodyRenderer>();
            scene.AddObject(platform);

            // Do a single, fixed-step Duality update in order to set up
            // Time.DeltaTime in a predictable way.
            DualityApp.Update(true);

            // Assert that the isolated scene remains unaffected
            Assert.AreEqual(new Vector3(0.0f, 0.0f, 0.0f), ballTransform.Pos);
            Assert.AreEqual(new Vector3(0.0f, 128.0f, 0.0f), platformTransform.Pos);

            // Activate the scene to prepare for simulation
            scene.Activate();

            // Run the simulation for a few fixed-step frames
            for (int i = 0; i < 100; i++)
            {
                scene.Update();
            }

            // Deactivate the scene again
            scene.Deactivate();

            // Assert that the balls position is within expected values
            Assert.IsTrue(ballTransform.Pos.Y > 96.0f);
            Assert.IsTrue(ballTransform.Pos.Y < 128.0f);
            Assert.IsTrue(Math.Abs(ballTransform.Pos.X) < 1.00f);
            Assert.IsTrue(Math.Abs(ballTransform.Pos.Z) < 1.00f);
        }
コード例 #7
0
    public void SetScene(SceneType sceneType)
    {
        if((Scene)_sceneList[sceneType])
        {
            if(_currentScene)
                _currentScene.Deactivate();

            _currentScene = (Scene)_sceneList[sceneType];
            _currentSceneType = sceneType;
            _currentScene.Activate();
        }
        else
        {
            Debug.LogError("No scene of type " + sceneType + " exists!");
        }
    }
コード例 #8
0
        public void IsolatedRendering()
        {
            // Create an isolated new test scene with a ball and a platform
            Scene scene = new Scene();

            GameObject     ball          = new GameObject("Ball");
            Transform      ballTransform = ball.AddComponent <Transform>();
            SpriteRenderer ballRenderer  = ball.AddComponent <SpriteRenderer>();

            scene.AddObject(ball);

            GameObject cameraObj       = new GameObject("Camera");
            Transform  cameraTransform = cameraObj.AddComponent <Transform>();

            cameraTransform.Pos = new Vector3(0.0f, 0.0f, -500.0f);
            Camera camera = cameraObj.AddComponent <Camera>();

            camera.ClearColor = new ColorRgba(128, 192, 255);
            scene.AddObject(cameraObj);

            // Activate the scene to prepare for simulation
            scene.Activate();

            // Render the scene to an image target
            PixelData renderedImage = null;

            using (Texture texture = new Texture(800, 600, TextureSizeMode.NonPowerOfTwo, TextureMagFilter.Nearest, TextureMinFilter.Nearest))
                using (RenderTarget renderTarget = new RenderTarget(AAQuality.Off, true, texture))
                {
                    scene.Render(renderTarget, new Rect(renderTarget.Size), renderTarget.Size);
                    renderedImage = texture.GetPixelData();
                }

            // Determine where in the resulting image we should see the ball object.
            // Note that we need to do this after rendering, but before deactivating the scene again,
            // so the correct target resolution is assumed.
            Vector2 ballPosInOutputImage = camera.GetScreenPos(ballTransform.Pos);

            // Deactivate the scene again
            scene.Deactivate();

            // Assert that the image target has the correct background color
            // and deviates from it at the balls position
            Assert.AreEqual(camera.ClearColor, renderedImage[0, 0]);
            Assert.AreNotEqual(camera.ClearColor, renderedImage[(int)ballPosInOutputImage.X, (int)ballPosInOutputImage.Y]);
        }
コード例 #9
0
    public void SetScene(SceneType sceneType)
    {
        if ((Scene)_sceneList[sceneType])
        {
            if (_currentScene)
            {
                _currentScene.Deactivate();
            }

            _currentScene     = (Scene)_sceneList[sceneType];
            _currentSceneType = sceneType;
            _currentScene.Activate();
        }
        else
        {
            Debug.LogError("No scene of type " + sceneType + " exists!");
        }
    }
コード例 #10
0
ファイル: CoroutinesTest.cs プロジェクト: raycrasher/duality
        [Test] public void Exception()
        {
            Scene scene = new Scene();

            scene.Activate();

            Coroutine coroutine = scene.StartCoroutine(this.ExceptionRoutine());

            // All code until first yield is already executed
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();
            // All code until second yield is executed
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            // Exception thrown, a log should appear
            scene.Update();
            Assert.True(coroutine.Status == CoroutineStatus.Error);
        }
コード例 #11
0
ファイル: CoroutinesTest.cs プロジェクト: raycrasher/duality
        [Test] public void Basics()
        {
            Scene scene = new Scene();

            scene.Activate();

            CoroutineObject obj       = new CoroutineObject();
            Coroutine       coroutine = scene.StartCoroutine(this.BasicRoutine(obj));

            // All code until first yield is already executed
            Assert.AreEqual(10, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();

            // All code until second yield is executed
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();

            // Yelded null, value didn't change, need to wait one more update
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();
            // Yielded condition is waiting for two frames now..
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            scene.Update();
            // All remaining code has been executed
            Assert.AreEqual(30, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Complete);

            scene.Update();

            // No further changes
            Assert.AreEqual(30, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Complete);
        }
コード例 #12
0
ファイル: CoroutinesTest.cs プロジェクト: raycrasher/duality
        [Test] public void Resuming()
        {
            Scene scene = new Scene();

            scene.Activate();

            CoroutineObject obj       = new CoroutineObject();
            Coroutine       coroutine = scene.StartCoroutine(this.BasicRoutine(obj));

            // All code until first yield is already executed
            Assert.AreEqual(10, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);

            coroutine.Pause();
            scene.Update();

            // No changes, since the coroutine is now paused
            Assert.AreEqual(10, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Paused);

            int rnd = MathF.Rnd.Next(ushort.MaxValue);

            for (int i = 0; i < rnd; i++)
            {
                scene.Update();
            }

            // No matter how many updates
            Assert.AreEqual(10, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Paused);

            coroutine.Resume();
            scene.Update();

            // All code until second yield is executed
            Assert.AreEqual(20, obj.Value);
            Assert.True(coroutine.Status == CoroutineStatus.Running);
        }
コード例 #13
0
        [Test] public void AddChildrenOnInit()
        {
            // Set up an object that will recursively create child objects on activation
            Scene      scene   = new Scene();
            GameObject testObj = new GameObject("TestObject");
            InitCreateChildComponent createComponent = testObj.AddComponent <InitCreateChildComponent>();

            createComponent.CreateCount = 9;
            scene.AddObject(testObj);

            // Activate the scene, prompting child creation
            scene.Activate();

            // Assert that all child objects were created successfully
            Assert.AreEqual(10, scene.AllObjects.Count());
            Assert.AreEqual(1, scene.RootObjects.Count());
            Assert.AreSame(testObj, scene.RootObjects.FirstOrDefault());
            Assert.IsTrue(scene.AllObjects.All(obj => obj.Children.Count <= 1));

            // Clean up
            scene.Deactivate();
            scene.Dispose();
        }
コード例 #14
0
ファイル: Scene.cs プロジェクト: TheJare/UnityUtils
 void AddSceneToTop(Scene scene)
 {
     mStack.Add(scene);
     scene.manager = this;
     scene.Init();
     scene.Activate();
 }
コード例 #15
0
        /// <summary>
        /// This test will determine whether a <see cref="Scene"/> that was activated in isolation / without
        /// being <see cref="Scene.Current"/> will ensure the initialization and shutdown of objects added
        /// or removed during its lifetime.
        /// </summary>
        [Test] public void IsolatedInitAndShutdown()
        {
            // Create an isolated new test scene
            Scene scene = new Scene();

            GameObject testObj = new GameObject("TestObject");
            InitializableEventReceiver testReceiver = testObj.AddComponent <InitializableEventReceiver>();

            scene.AddObject(testObj);

            // Ensure events are delivered in scene activation
            testReceiver.Reset();
            scene.Activate();
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Activate), "Activate by Scene activation");

            // Ensure events are delivered in scene deactivation
            testReceiver.Reset();
            scene.Deactivate();
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Deactivate), "Deactivate by Scene deactivation");

            // Ensure no events are delivered for removal while deactivated
            testReceiver.Reset();
            scene.RemoveObject(testObj);
            scene.Activate();
            Assert.AreEqual(InitializableEventReceiver.EventFlag.None, testReceiver.ReceivedEvents, "Removal from Scene, activation of Scene");

            // Ensure events are delivered post-activation
            testReceiver.Reset();
            scene.AddObject(testObj);
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Activate), "Activate by addition to active Scene");

            testReceiver.Reset();
            testObj.RemoveComponent(testReceiver);
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Deactivate), "Removal from object in active Scene");
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.RemovingFromGameObject), "Removal from object in active Scene");

            testReceiver.Reset();
            testObj.AddComponent(testReceiver);
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Activate), "Addition to object in active Scene");
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.AddToGameObject), "Addition to object in active Scene");

            testReceiver.Reset();
            testObj.Active = false;
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Deactivate), "Deactivate by GameObject.Active property in active Scene");

            testReceiver.Reset();
            testObj.Active = true;
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Activate), "Activate by GameObject.Active property in active Scene");

            testReceiver.Reset();
            testReceiver.Active = false;
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Deactivate), "Deactivate by Component.Active property in active Scene");

            testReceiver.Reset();
            testReceiver.Active = true;
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Activate), "Activate by Component.Active property in active Scene");

            testReceiver.Reset();
            scene.RemoveObject(testObj);
            Assert.IsTrue(testReceiver.HasReceived(InitializableEventReceiver.EventFlag.Deactivate), "Deactivate by removal from active Scene");

            // Ensure no de/activation events are delivered in an inactive scene
            scene.Deactivate();
            testReceiver.Reset();
            scene.AddObject(testObj);
            testObj.Active      = false;
            testObj.Active      = true;
            testReceiver.Active = false;
            testReceiver.Active = true;
            scene.RemoveObject(testObj);
            Assert.AreEqual(InitializableEventReceiver.EventFlag.None, testReceiver.ReceivedEvents, "No de/activation events in inactive Scene");
        }
コード例 #16
0
ファイル: Director.cs プロジェクト: Neio/MonoGame2D
        /// <summary>
        /// Switches the domain with specified domain switch effect.
        /// </summary>
        /// <param name="domain">The new domain.</param>
        /// <param name="switchEffect">The switch effect.</param>
        /// <param name="timeToSwitch">The time to switch.</param>
        public void SetScene(Scene scene, ISceneSwitchEffect switchEffect, float timeToSwitch)
        {
            if (null == scene) throw new ArgumentNullException("scene");

            waitFrame += () =>
            {
                if (null != switchEffect)
                {
                    StartEffect(switchEffect, timeToSwitch);
                }

                if (_sceneStack.Count > 0)
                {
                    var pop = _sceneStack.Pop();
                    pop.Deactivate();
                    pop.DeactivateResource();
                }

                //change domain
                _sceneStack.Push(scene);

                //invoke activate for new domain...
                if (null != scene)
                {
                    scene.BindService = this.Services;
                    scene.ActivateResource();
                    scene.Activate();
                }
            };
        }