簡體   English   中英

當我按下游戲場景上的按鈕時,它開始游戲

[英]When i press button on the game scene its starts the game

我的問題是我在游戲場景中有一個商店按鈕,所以當我按下它時不會帶我到商店場景它開始游戲,我想要當我按下按鈕時帶我到商店場景! ,我會發布我的代碼我希望你們能幫助我,因為我花了一天時間閱讀,但我沒有得到任何結果請幫助!

移動輸入腳本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MobileInput : MonoBehaviour {

private const float DEADZONE = 100.0f;

public static MobileInput Instance { set; get;  }

private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
private Vector2 swipeDelta, startTouch;

public bool Tap { get { return tap; } }
public Vector2 SwipeDelta { get { return swipeDelta; } }
public bool SwipeLeft { get { return swipeLeft; } }
public bool SwipeRight { get { return swipeRight; } }
public bool SwipeUp { get { return swipeUp; } }
public bool SwipeDown { get { return swipeDown; } }

private void Awake()
{
    Instance = this;
}

private void Update()
{ 
    //Reseting all the booleans
    tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

    //Let's check for inputs
    #region Stanedalone Inputs
    if (Input.GetMouseButtonDown(0))
    {
        tap = true;
        startTouch = Input.mousePosition;
    }
    else if (Input.GetMouseButtonUp(0))
    {
        startTouch = swipeDelta = Vector2.zero;
    }
    #endregion



    #region Mobile Inputs
    if (Input.touches.Length != 0)
    {
        if (Input.touches[0].phase == TouchPhase.Began)
        {
            tap = true;
            startTouch = Input.mousePosition;
        }

        else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
        {
            startTouch = swipeDelta = Vector2.zero;
        }

    }
    #endregion

    //Caluclate distance
    swipeDelta = Vector2.zero;
    if (startTouch != Vector2.zero)
    { 
        //Let's check with mobile 
        if (Input.touches.Length != 0)

        {
            swipeDelta = Input.touches[0].position - startTouch;
        }
        //Lets check with standalone
        else if (Input.GetMouseButton(0))
        {
            swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }
    }

    //Let's check if we're beyond the deadzone 
    if (swipeDelta.magnitude > DEADZONE)
    { 
        // this is a confirmed swipe
        float x = swipeDelta.x;
        float y = swipeDelta.y;

        if (Mathf.Abs(x) > Mathf.Abs(y))
        {
            // Left or right
            if (x < 0)
                swipeLeft = true;
            else
                swipeRight = true;
        }

        else
        { 
            // Up or Down
            if (y < 0)
                swipeDown = true;
            else
                swipeUp = true;
        }

        startTouch = swipeDelta = Vector2.zero;
    }
}

}

游戲管理器腳本

    private void Update()
{
    if (MobileInput.Instance.Tap && !isGameStarted)
    {
        isGameStarted = true;
        motor.StartRunning();
        FindObjectOfType<GlacierSpawner>().IsScrolling = true;
        FindObjectOfType<CameraMotor>().IsMoving = true;
        gameCanvas.SetTrigger("Show");
        MenuAnim.SetTrigger("Hide");
    }
    if (isGameStarted && !isDead)
    {
        //Bump up the score 
        score += (Time.deltaTime * modifierScore);
        if (lastScore != (int)score)
        {
            lastScore = (int)score;
            scoreText.text = score.ToString("0");
        }
    }
}

您可以創建一個不可見的 UIButton 來覆蓋除 ShopButton 區域之外的屏幕。 在您的 GameManager 腳本中定義兩個公共方法,一個用於開始游戲,一個用於展示商店,然后通過在 UIButton 的按鈕組件中添加一個 OnClick 處理程序將這些方法鏈接到 UIButton 的方法。

點擊后不要忘記禁用播放按鈕;)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM