代码之家  ›  专栏  ›  技术社区  ›  blue

统一:让操纵杆充满整个屏幕?

  •  0
  • blue  · 技术社区  · 6 年前

    好的,我有一个从Unity asset store下载的标准操纵杆,脚本在这里(不是很长):

    // Implement IPointerDownHandler, IPointerUpHandler, IDragHandler to subscribe to pointer events
    public class Joystick : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IDragHandler {
        public delegate void JoystickAction(Vector2 joystickAxes);
        public static event JoystickAction JoystickMoved;
    
        public Vector2 joystickAxes;
    
        public bool usingJoystick;
        private Image bgImg;
        private Image stickImg;
        private RectTransform bgTransform;
        private RectTransform stickTransform;
    
        // The first tap is treated similar to any following detection of a drag
        public virtual void OnPointerDown(PointerEventData ped) {
            usingJoystick = true;
            OnDrag (ped);
        }
    
        public virtual void OnPointerUp(PointerEventData ped) {
            usingJoystick = false;
            joystickAxes = stickImg.GetComponent<RectTransform> ().anchoredPosition = Vector2.zero;
            //joystickAxes = new Vector2(Screen.height / 2, Screen.width/2);
    
            if (JoystickMoved != null) JoystickMoved (Vector2.zero);
        }
    
        public virtual void OnDrag(PointerEventData ped) {
            Vector2 rectPos;
    
            if (RectTransformUtility.ScreenPointToLocalPointInRectangle(
                    bgImg.rectTransform,
                    ped.position,
                    ped.enterEventCamera,
                    out rectPos)) { // Check if the pointer is positioned on the joystick
                // Clamp the position to be inside the image bounds, prevents dragging beyond the image to get higher values.
                Vector2 clampedPos = GetClampedPosition (rectPos); 
    
                // Normalize the joystick axes to be between -1 and 1.
                joystickAxes = new Vector2 (
                    clampedPos.x / (bgTransform.rect.width / 2f),
                    clampedPos.y / (bgTransform.rect.height / 2f));
    
                // Set the position of the inner joystick.
                if (joystickAxes.magnitude > 1f) { // Normalizing the clampedPos constrains the joystick positions to a circle
                    stickImg.GetComponent<RectTransform> ().anchoredPosition = clampedPos.normalized * stickTransform.rect.width;
                } else {
                    stickImg.GetComponent<RectTransform> ().anchoredPosition = clampedPos;
                }
            }
        }
    
        private Vector2 GetClampedPosition(Vector2 pos) {
            Vector2 bgMin = bgTransform.rect.min;
            Vector2 bgMax = bgTransform.rect.max;
    
            return new Vector2 (
                Mathf.Clamp (pos.x, bgMin.x, bgMax.x),
                Mathf.Clamp (pos.y, bgMin.y, bgMax.y));
        }
    
        // Use this for initialization
        void Start () {
            joystickAxes = new Vector2 ();
    
            bgImg = GetComponent<Image> ();
            stickImg = transform.GetChild (0).GetComponent<Image> ();
    
            bgTransform = gameObject.GetComponent<RectTransform> ();
            stickTransform = transform.GetChild (0).GetComponent<RectTransform> ();
        }
    
        void Update() {
            if (usingJoystick && JoystickMoved != null) {
                JoystickMoved (joystickAxes);
            }
        }
    }
    

    这依赖于画布上的图像对象和旋钮的另一个图像。如图所示:

    enter image description here

    这一切都很好,但是我希望操纵杆是屏幕,这意味着旋钮的外部边界不是背景图像,而是屏幕本身的边界。

    我试过使用rh canvas等将背景图像缩放到屏幕上,但这会使inputAxes的值大为不同。我该怎么做?

    0 回复  |  直到 6 年前