Unity: заставить джойстик заполнить весь экран? - PullRequest
0 голосов
/ 13 июня 2018

Хорошо, у меня есть стандартный джойстик, загруженный из хранилища активов Unity, скрипт здесь (не так долго):

// 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.Как я могу это сделать?

Я хочу сохранить этот скрипт, просто настройте его так, чтобы фоновое «изображение» / bounds было экраном, независимо от его размера.

...