Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当我按下游戏场景上的按钮时,它开始游戏_C#_Unity3d_Unity5_Unityscript - Fatal编程技术网

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

C# 当我按下游戏场景上的按钮时,它开始游戏,c#,unity3d,unity5,unityscript,C#,Unity3d,Unity5,Unityscript,我的问题是,我在游戏场景中有一个商店按钮,所以当我按下它时,它不会带我去商店场景,而是会启动游戏,我想当我按下按钮时,它会带我去商店场景,我将发布我的代码,我希望你们能帮助我,因为我花了一天的时间阅读,但我没有得到任何结果,请帮助 移动输入脚本 using System.Collections; using System.Collections.Generic; using UnityEngine; public class MobileInput : MonoBehaviour { pri

我的问题是,我在游戏场景中有一个商店按钮,所以当我按下它时,它不会带我去商店场景,而是会启动游戏,我想当我按下按钮时,它会带我去商店场景,我将发布我的代码,我希望你们能帮助我,因为我花了一天的时间阅读,但我没有得到任何结果,请帮助

移动输入脚本

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;
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类移动输入:单行为{
专用常数浮动死区=100.0f;
公共静态MobileInput实例{set;get;}
私用水龙头、左水龙头、右水龙头、上水龙头、下水龙头;
私人Vector2 swipeDelta,startTouch;
公共布尔点击{获取{返回点击;}}
公共向量2 SwipeDelta{get{return SwipeDelta;}}
公共布尔SwipeLeft{get{return SwipeLeft;}}
公共布尔SwiperRight{get{return SwiperRight;}}
公共bool SwipeUp{get{return SwipeUp;}}
公共bool SwipeDown{get{return SwipeDown;}
私人空间
{
实例=此;
}
私有void更新()
{ 
//重置所有布尔人
点击=开关左=开关右=开关向上=开关向下=假;
//让我们检查输入
#区域标准输入
if(Input.GetMouseButtonDown(0))
{
tap=真;
startTouch=Input.mousePosition;
}
else if(Input.GetMouseButtonUp(0))
{
startTouch=swipeDelta=矢量2.0;
}
#端区
#区域移动输入
if(Input.touchs.Length!=0)
{
if(Input.touchs[0]。phase==TouchPhase.begined)
{
tap=真;
startTouch=Input.mousePosition;
}
else if(Input.touchs[0]。phase==TouchPhase.end | | Input.touchs[0]。phase==TouchPhase.Canceled)
{
startTouch=swipeDelta=矢量2.0;
}
}
#端区
//钙板距离
swipeDelta=矢量2.0;
if(startTouch!=Vector2.zero)
{ 
//让我们看看手机
if(Input.touchs.Length!=0)
{
swipeDelta=Input.Touchs[0]。位置-startTouch;
}
//让我们检查一下单机版
else if(Input.GetMouseButton(0))
{
swipeDelta=(Vector2)Input.mousePosition-startTouch;
}
}
//让我们检查一下我们是否超出了死区
如果(swipeDelta.magnitude>死区)
{ 
//这是一次确认的刷卡
浮动x=swipeDelta.x;
浮动y=swipeDelta.y;
如果(数学绝对值(x)>数学绝对值(y))
{
//左还是右
if(x<0)
swipeLeft=true;
其他的
swipeRight=真;
}
其他的
{ 
//上下
if(y<0)
swipeDown=true;
其他的
swipeUp=true;
}
startTouch=swipeDelta=矢量2.0;
}
}
}

游戏管理器脚本

    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");
        }
    }
}
private void Update()
{
if(MobileInput.Instance.Tap&&!isGameStarted)
{
isGameStarted=true;
电机。起动耳轴();
FindObjectOfType().IsScrolling=true;
FindObjectOfType().IsMoving=true;
gameCanvas.SetTrigger(“显示”);
MenuAnim.SetTrigger(“隐藏”);
}
如果(isGameStarted&!isDead)
{
//提高分数
分数+=(Time.deltaTime*modifierScore);
如果(最后分数!=(整数)分数)
{
lastScore=(int)分数;
scoreText.text=score.ToString(“0”);
}
}
}

您可以创建一个不可见的UIButton来覆盖屏幕,ShopButton区域除外。在GameManager脚本中定义两个公共方法,一个用于开始游戏,另一个用于show shop,并通过在UIButtons的按钮组件中添加OnClick处理程序将这些方法链接到UIButtons


不要忘记在单击播放按钮后禁用它;)

我看不出您在哪里指示
装载级别
。我经常在场景之间切换。确保你将场景添加到构建设置中。我想你没有得到我想要的,首先我必须为商店场景添加一个场景,为游戏场景添加第二个场景,所以我在游戏场景中遇到了一个问题,当我按下shop按钮时,它会自动启动游戏,然后当我禁用mobile inputs脚本时,它会将我带到shop场景,所以我认为我的mobile input脚本有问题,但我在这两个代码中都看不到任何与交换场景相关的内容?你能告诉我或告诉我哪一行的脚本会这样做吗?它会启动游戏,因为你让你的游戏在你的GameManager脚本中启动。你没有对照任何东西检查输入,你只是盲目地接受任何和所有的触按作为开始游戏的提示。@Draco18好的,我怎么能解决这个问题?