简体   繁体   English

当我按下游戏场景上的按钮时,它开始游戏

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

my problem is I have a Shop button in the game scene so when I press it doesn't take me to the shop scene it starts the game, I want when I press the button take me to the shop scene!我的问题是我在游戏场景中有一个商店按钮,所以当我按下它时不会带我到商店场景它开始游戏,我想要当我按下按钮时带我到商店场景! , I will post my code I hope you can guys help me because I spend one day reading about that and I didn't get any result PLEASE HELP! ,我会发布我的代码我希望你们能帮助我,因为我花了一天时间阅读,但我没有得到任何结果请帮助!

Mobile Input script移动输入脚本

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;
    }
}

} }

GameManager script游戏管理器脚本

    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");
        }
    }
}

You can create an invisible UIButton to cover the screen except the ShopButton area.您可以创建一个不可见的 UIButton 来覆盖除 ShopButton 区域之外的屏幕。 Define two public methods in your GameManager script, one for start game, on for show shop, and link these to methods to the UIButtons by adding an OnClick Handler in the Button Component of your UIButtons.在您的 GameManager 脚本中定义两个公共方法,一个用于开始游戏,一个用于展示商店,然后通过在 UIButton 的按钮组件中添加一个 OnClick 处理程序将这些方法链接到 UIButton 的方法。

Don't forget to disable the PlayButton after clicking it ;)点击后不要忘记禁用播放按钮;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM