public IEnumerator ScaleLocalDistances()
        {
            // instantiate scene and button
            GameObject testButton = InstantiateSceneAndDefaultPressableButton();

            yield return(null);

            PressableButton buttonComponent = testButton.GetComponent <PressableButton>();

            Assert.IsNotNull(buttonComponent);

            testButton.transform.Translate(new Vector3(10.0f, 5.0f, 20.0f));
            PressableButton.SpaceMode distanceMode = buttonComponent.DistanceSpaceMode;
            // check default value -> default must be using world space to not introduce a breaking change to the button
            Assert.IsTrue(distanceMode == PressableButton.SpaceMode.World, "Pressable button default value is using local space distances which introduces a breaking change for existing projects");

            // make sure there's no scale on our button
            testButton.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

            // change into local space distances
            buttonComponent.DistanceSpaceMode = PressableButton.SpaceMode.Local;

            // set start distance -> default is zero
            buttonComponent.StartPushDistance = 0.00003f;

            // get the buttons default values for the push planes
            float startPushDistance = buttonComponent.StartPushDistance;
            float maxPushDistance   = buttonComponent.MaxPushDistance;
            float pressDistance     = buttonComponent.PressDistance;
            float releaseDistance   = pressDistance - buttonComponent.ReleaseDistanceDelta;

            Vector3 startPushDistanceWorld = buttonComponent.GetWorldPositionAlongPushDirection(startPushDistance);
            Vector3 maxPushDistanceWorld   = buttonComponent.GetWorldPositionAlongPushDirection(maxPushDistance);
            Vector3 pressDistanceWorld     = buttonComponent.GetWorldPositionAlongPushDirection(pressDistance);
            Vector3 releaseDistanceWorld   = buttonComponent.GetWorldPositionAlongPushDirection(releaseDistance);

            // scale the button in z direction
            // scaling the button while in local space will alter the world space distances
            Vector3 zScale = new Vector3(1.0f, 1.0f, 2.0f);

            testButton.transform.localScale = zScale;

            Vector3 startPushDistanceWorldScaled = buttonComponent.GetWorldPositionAlongPushDirection(startPushDistance);
            Vector3 maxPushDistanceWorldScaled   = buttonComponent.GetWorldPositionAlongPushDirection(maxPushDistance);
            Vector3 pressDistanceWorldScaled     = buttonComponent.GetWorldPositionAlongPushDirection(pressDistance);
            Vector3 releaseDistanceWorldScaled   = buttonComponent.GetWorldPositionAlongPushDirection(releaseDistance);

            // check if the local plane distances have scaled with the gameobject transform scale
            Vector3 initialPosition = buttonComponent.GetWorldPositionAlongPushDirection(0);

            Assert.IsTrue(((startPushDistanceWorld - initialPosition).Mul(zScale)) == ((startPushDistanceWorldScaled - initialPosition)), "Plane distance didn't scale with object while in local mode");;
            Assert.IsTrue(((maxPushDistanceWorld - initialPosition).Mul(zScale)) == ((maxPushDistanceWorldScaled - initialPosition)), "Plane distance didn't scale with object while in local mode");;
            Assert.IsTrue(((pressDistanceWorld - initialPosition).Mul(zScale)) == ((pressDistanceWorldScaled - initialPosition)), "Plane distance didn't scale with object while in local mode");;
            Assert.IsTrue(((releaseDistanceWorld - initialPosition).Mul(zScale)) == ((releaseDistanceWorldScaled - initialPosition)), "Plane distance didn't scale with object while in local mode");;

            Object.Destroy(testButton);
            // Wait for a frame to give Unity a change to actually destroy the object
            yield return(null);
        }
Exemple #2
0
        public IEnumerator ScaleWorldDistances([ValueSource(nameof(PressableButtonsTestPrefabPaths))] string prefabFilename)
        {
            // instantiate scene and button
            GameObject testButton = InstantiateDefaultPressableButton(prefabFilename);

            yield return(null);

            PressableButton button = testButton.GetComponent <PressableButton>();

            Assert.IsNotNull(button);

            // check default value -> default must be using local space in order for the button to scale and function correctly
            Assert.IsTrue(button.DistanceSpaceMode == PressableButton.SpaceMode.Local);

            // Ensure uniform scale, non-zero start push distance, and world space distance
            testButton.transform.localScale = Vector3.one;
            button.DistanceSpaceMode        = PressableButton.SpaceMode.World;
            button.StartPushDistance        = 0.00003f;

            // get the buttons default values for the push planes
            float startPushDistance = button.StartPushDistance;
            float maxPushDistance   = button.MaxPushDistance;
            float pressDistance     = button.PressDistance;
            float releaseDistance   = pressDistance - button.ReleaseDistanceDelta;

            Vector3 zeroPushDistanceWorld = button.GetWorldPositionAlongPushDirection(0.0f);

            Vector3 startPushDistanceWorld = button.GetWorldPositionAlongPushDirection(startPushDistance) - zeroPushDistanceWorld;
            Vector3 maxPushDistanceWorld   = button.GetWorldPositionAlongPushDirection(maxPushDistance) - zeroPushDistanceWorld;
            Vector3 pressDistanceWorld     = button.GetWorldPositionAlongPushDirection(pressDistance) - zeroPushDistanceWorld;
            Vector3 releaseDistanceWorld   = button.GetWorldPositionAlongPushDirection(releaseDistance) - zeroPushDistanceWorld;

            // scale the button in z direction
            // scaling the button while in world space shouldn't influence our button plane distances
            testButton.transform.localScale = new Vector3(1.0f, 1.0f, 2.0f);

            Vector3 zeroPushDistanceWorldScaled = button.GetWorldPositionAlongPushDirection(0.0f);

            Vector3 startPushDistanceWorldScaled = button.GetWorldPositionAlongPushDirection(startPushDistance) - zeroPushDistanceWorldScaled;
            Vector3 maxPushDistanceWorldScaled   = button.GetWorldPositionAlongPushDirection(maxPushDistance) - zeroPushDistanceWorldScaled;
            Vector3 pressDistanceWorldScaled     = button.GetWorldPositionAlongPushDirection(pressDistance) - zeroPushDistanceWorldScaled;
            Vector3 releaseDistanceWorldScaled   = button.GetWorldPositionAlongPushDirection(releaseDistance) - zeroPushDistanceWorldScaled;

            // compare our distances
            Assert.IsTrue(startPushDistanceWorld == startPushDistanceWorldScaled, "Start Distance was modified while scaling button GameObject");
            Assert.IsTrue(maxPushDistanceWorld == maxPushDistanceWorldScaled, "Max Push Distance was modified while scaling button GameObject");
            Assert.IsTrue(pressDistanceWorld == pressDistanceWorldScaled, "Press Distance was modified while scaling button GameObject");
            Assert.IsTrue(releaseDistanceWorld == releaseDistanceWorldScaled, "Release Distance was modified while scaling button GameObject");

            Object.Destroy(testButton);
            // Wait for a frame to give Unity a change to actually destroy the object
            yield return(null);
        }
        public IEnumerator ScaleLocalDistances([ValueSource(nameof(PressableButtonsTestPrefabFilenames))] string prefabFilename)
        {
            // instantiate scene and button
            GameObject testButton = InstantiateDefaultPressableButton(prefabFilename);

            yield return(null);

            PressableButton button = testButton.GetComponent <PressableButton>();

            Assert.IsNotNull(button);

            // check default value -> default must be using local space in order for the button to scale and function correctly
            Assert.IsTrue(button.DistanceSpaceMode == PressableButton.SpaceMode.Local);

            // make sure there's no scale on our button and non-zero to start push distance
            testButton.transform.localScale = Vector3.one;
            button.StartPushDistance        = 0.00003f;

            float zeroPushDistanceWorld = button.GetWorldPositionAlongPushDirection(0.0f).z;

            // get the buttons default values for the push planes
            float startPushDistanceWorld = button.GetWorldPositionAlongPushDirection(button.StartPushDistance).z - zeroPushDistanceWorld;
            float maxPushDistanceWorld   = button.GetWorldPositionAlongPushDirection(button.MaxPushDistance).z - zeroPushDistanceWorld;
            float pressDistanceWorld     = button.GetWorldPositionAlongPushDirection(button.PressDistance).z - zeroPushDistanceWorld;
            float releaseDistanceWorld   = button.GetWorldPositionAlongPushDirection(button.PressDistance - button.ReleaseDistanceDelta).z - zeroPushDistanceWorld;

            // scale the button in z direction
            // scaling the button while in local space should keep plane distance ratios maintained
            Vector3 zScale = new Vector3(1.0f, 1.0f, 3.6f);

            testButton.transform.localScale = zScale;
            yield return(null);

            float zeroPushDistanceWorld_Scaled = button.GetWorldPositionAlongPushDirection(0.0f).z;

            float startPushDistanceWorld_Scaled = button.GetWorldPositionAlongPushDirection(button.StartPushDistance).z - zeroPushDistanceWorld_Scaled;
            float maxPushDistanceWorld_Scaled   = button.GetWorldPositionAlongPushDirection(button.MaxPushDistance).z - zeroPushDistanceWorld_Scaled;
            float pressDistanceWorld_Scaled     = button.GetWorldPositionAlongPushDirection(button.PressDistance).z - zeroPushDistanceWorld_Scaled;
            float releaseDistanceWorld_Scaled   = button.GetWorldPositionAlongPushDirection(button.PressDistance - button.ReleaseDistanceDelta).z - zeroPushDistanceWorld_Scaled;

            float tolerance = 0.00000001f;

            Assert.IsTrue(AreApproximatelyEqual(startPushDistanceWorld * zScale.z, startPushDistanceWorld_Scaled, tolerance), "Start push distance plane did not scale correctly");
            Assert.IsTrue(AreApproximatelyEqual(maxPushDistanceWorld * zScale.z, maxPushDistanceWorld_Scaled, tolerance), "Max push distance plane did not scale correctly");
            Assert.IsTrue(AreApproximatelyEqual(pressDistanceWorld * zScale.z, pressDistanceWorld_Scaled, tolerance), "Press distance plane did not scale correctly");
            Assert.IsTrue(AreApproximatelyEqual(releaseDistanceWorld * zScale.z, releaseDistanceWorld_Scaled, tolerance), "Release distance plane did not scale correctly");

            Object.Destroy(testButton);
            // Wait for a frame to give Unity a chance to actually destroy the object
            yield return(null);
        }
        public IEnumerator ScaleWorldDistances()
        {
            // instantiate scene and button
            GameObject testButton = InstantiateSceneAndDefaultPressableButton();

            yield return(null);

            PressableButton buttonComponent = testButton.GetComponent <PressableButton>();

            Assert.IsNotNull(buttonComponent);

            testButton.transform.Translate(new Vector3(10.0f, 5.0f, 20.0f));

            PressableButton.SpaceMode distanceMode = buttonComponent.DistanceSpaceMode;
            // check default value -> default must be using world space to not introduce a breaking change to the button
            Assert.IsTrue(distanceMode == PressableButton.SpaceMode.World, "Pressable button default value is using local space distances which introduces a breaking change for existing projects");

            // make sure there's no scale on our button
            testButton.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);

            // set start distance -> default is zero
            buttonComponent.StartPushDistance = 0.00003f;

            // get the buttons default values for the push planes
            float startPushDistance = buttonComponent.StartPushDistance;
            float maxPushDistance   = buttonComponent.MaxPushDistance;
            float pressDistance     = buttonComponent.PressDistance;
            float releaseDistance   = pressDistance - buttonComponent.ReleaseDistanceDelta;

            Vector3 startPushDistanceWorld = buttonComponent.GetWorldPositionAlongPushDirection(startPushDistance);
            Vector3 maxPushDistanceWorld   = buttonComponent.GetWorldPositionAlongPushDirection(maxPushDistance);
            Vector3 pressDistanceWorld     = buttonComponent.GetWorldPositionAlongPushDirection(pressDistance);
            Vector3 releaseDistanceWorld   = buttonComponent.GetWorldPositionAlongPushDirection(releaseDistance);

            // scale the button in z direction
            // scaling the button while in world space shouldn't influence our button plane distances
            testButton.transform.localScale = new Vector3(1.0f, 1.0f, 2.0f);

            Vector3 startPushDistanceWorldScaled = buttonComponent.GetWorldPositionAlongPushDirection(startPushDistance);
            Vector3 maxPushDistanceWorldScaled   = buttonComponent.GetWorldPositionAlongPushDirection(maxPushDistance);
            Vector3 pressDistanceWorldScaled     = buttonComponent.GetWorldPositionAlongPushDirection(pressDistance);
            Vector3 releaseDistanceWorldScaled   = buttonComponent.GetWorldPositionAlongPushDirection(releaseDistance);

            // compare our distances
            Assert.IsTrue(startPushDistanceWorld == startPushDistanceWorldScaled, "Start Distance was modified while scaling button gameobject");
            Assert.IsTrue(maxPushDistanceWorld == maxPushDistanceWorldScaled, "Max Push Distance was modified while scaling button gameobject");
            Assert.IsTrue(pressDistanceWorld == pressDistanceWorldScaled, "Press Distance was modified while scaling button gameobject");
            Assert.IsTrue(releaseDistanceWorld == releaseDistanceWorldScaled, "Release Distance was modified while scaling button gameobject");

            Object.Destroy(testButton);
            // Wait for a frame to give Unity a change to actually destroy the object
            yield return(null);
        }
        private float DrawPlaneAndHandle(Vector3[] vertices, Vector2 halfExtents, float distance, ButtonInfo info, string label, bool editingEnabled)
        {
            Vector3 centerWorld = button.GetWorldPositionAlongPushDirection(distance);

            MakeQuadFromPoint(vertices, centerWorld, halfExtents, info);

            if (VisiblePlanes)
            {
                Handles.DrawSolidRectangleWithOutline(vertices, Color.Lerp(Handles.color, Color.clear, 0.65f), Handles.color);
            }

            // Label
            {
                Vector3 mousePosition = SceneView.currentDrawingSceneView.camera.ScreenToViewportPoint(Event.current.mousePosition);
                mousePosition.y = 1f - mousePosition.y;
                mousePosition.z = 0;
                Vector3 handleVisiblePos = SceneView.currentDrawingSceneView.camera.WorldToViewportPoint(vertices[1]);
                handleVisiblePos.z = 0;

                if (Vector3.Distance(mousePosition, handleVisiblePos) < labelMouseOverDistance)
                {
                    DrawLabel(vertices[1], transform.up - transform.right, label, labelStyle);
                    HandleUtility.Repaint();
                }
            }

            // Draw forward / backward arrows so people know they can drag
            if (editingEnabled)
            {
                float handleSize = HandleUtility.GetHandleSize(vertices[1]) * 0.15f;

                Vector3 dir         = (touchable != null) ? touchable.LocalPressDirection : Vector3.forward;
                Vector3 planeNormal = button.transform.TransformDirection(dir);
                Handles.ArrowHandleCap(0, vertices[1], Quaternion.LookRotation(planeNormal), handleSize * 2, EventType.Repaint);
                Handles.ArrowHandleCap(0, vertices[1], Quaternion.LookRotation(-planeNormal), handleSize * 2, EventType.Repaint);

                Vector3 newPosition = Handles.FreeMoveHandle(vertices[1], Quaternion.identity, handleSize, Vector3.zero, Handles.SphereHandleCap);
                if (!newPosition.Equals(vertices[1]))
                {
                    distance = button.GetDistanceAlongPushDirection(newPosition);
                }
            }

            return(distance);
        }
Exemple #6
0
        public IEnumerator SwitchWorldToLocalDistanceMode([ValueSource(nameof(PressableButtonsTestPrefabPaths))] string prefabFilename)
        {
            // instantiate scene and button
            GameObject testButton = InstantiateDefaultPressableButton(prefabFilename);

            yield return(null);

            PressableButton button = testButton.GetComponent <PressableButton>();

            Assert.IsNotNull(button);

            // check default value -> default must be using local space in order for the button to scale and function correctly
            Assert.IsTrue(button.DistanceSpaceMode == PressableButton.SpaceMode.Local);

            // add scale to our button so we can compare world to local distances
            testButton.transform.localScale = new Vector3(1.0f, 1.0f, 2.0f);
            button.StartPushDistance        = 0.00003f;

            // get the buttons default values for the push planes
            float startPushDistanceLocal = button.StartPushDistance;
            float maxPushDistanceLocal   = button.MaxPushDistance;
            float pressDistanceLocal     = button.PressDistance;
            float releaseDistanceLocal   = pressDistanceLocal - button.ReleaseDistanceDelta;

            // get world space positions for local distances
            Vector3 startPushDistanceWorldLocal = button.GetWorldPositionAlongPushDirection(startPushDistanceLocal);
            Vector3 maxPushDistanceWorldLocal   = button.GetWorldPositionAlongPushDirection(maxPushDistanceLocal);
            Vector3 pressDistanceWorldLocal     = button.GetWorldPositionAlongPushDirection(pressDistanceLocal);
            Vector3 releaseDistanceWorldLocal   = button.GetWorldPositionAlongPushDirection(releaseDistanceLocal);

            // switch to world space
            button.DistanceSpaceMode = PressableButton.SpaceMode.World;

            float startPushDistance = button.StartPushDistance;
            float maxPushDistance   = button.MaxPushDistance;
            float pressDistance     = button.PressDistance;
            float releaseDistance   = pressDistance - button.ReleaseDistanceDelta;

            // check if distances have changed
            Assert.IsFalse(startPushDistance == startPushDistanceLocal, "Switching from world to local space distances didn't adjust the plane coords");
            Assert.IsFalse(maxPushDistance == maxPushDistanceLocal, "Switching from world to local space distances didn't adjust the plane coords");
            Assert.IsFalse(pressDistance == pressDistanceLocal, "Switching from world to local space distances didn't adjust the plane coords");
            Assert.IsFalse(releaseDistance == releaseDistanceLocal, "Switching from world to local space distances didn't adjust the plane coords");

            // get world space positions for local distances
            Vector3 startPushDistanceWorld = button.GetWorldPositionAlongPushDirection(startPushDistance);
            Vector3 maxPushDistanceWorld   = button.GetWorldPositionAlongPushDirection(maxPushDistance);
            Vector3 pressDistanceWorld     = button.GetWorldPositionAlongPushDirection(pressDistance);
            Vector3 releaseDistanceWorld   = button.GetWorldPositionAlongPushDirection(releaseDistance);

            // compare world space distances -> local and world space mode should return us the same world space positions
            Assert.IsTrue(startPushDistanceWorld == startPushDistanceWorldLocal, "World and Local World positions don't match after switching pressable button distance mode");
            Assert.IsTrue(maxPushDistanceWorld == maxPushDistanceWorldLocal, "World and Local World positions don't match after switching pressable button distance mode");
            Assert.IsTrue(pressDistanceWorld == pressDistanceWorldLocal, "World and Local World positions don't match after switching pressable button distance mode");
            Assert.IsTrue(releaseDistanceWorld == releaseDistanceWorldLocal, "World and Local World positions don't match after switching pressable button distance mode");

            // switch back to local space
            button.DistanceSpaceMode = PressableButton.SpaceMode.Local;

            // distances must match up with original values
            Assert.IsTrue(startPushDistanceLocal == button.StartPushDistance, "Conversion from local to world distances didn't return the correct world distances");
            Assert.IsTrue(maxPushDistanceLocal == button.MaxPushDistance, "Conversion from local to world distances didn't return the correct world distances");
            Assert.IsTrue(pressDistanceLocal == button.PressDistance, "Conversion from local to world distances didn't return the correct world distances");
            float newReleaseDistance = button.PressDistance - button.ReleaseDistanceDelta;

            Assert.IsTrue(releaseDistanceLocal == newReleaseDistance, "Conversion from local to world distances didn't return the correct world distances");

            Object.Destroy(testButton);
            // Wait for a frame to give Unity a change to actually destroy the object
            yield return(null);
        }