Unity:让操纵杆填满整个屏幕?

时间:2018-06-12 23:45:05

标签: c# user-interface unity3d joystick

好的,所以我有一个从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值关闭。我怎样才能做到这一点?

我想保留这个脚本,只需调整它,这样背景&#34;图像&#34; /界限就是屏幕,无论大小如何。

0 个答案:

没有答案