简体   繁体   English

(Unity - C#) 按钮触发其他事件的问题

[英](Unity - C#) Problems with buttons triggering other events

I'm developing a 2D mobile game where the player should tap on the screen to go up.我正在开发一款 2D 手机游戏,玩家应该在屏幕上点击 go 向上。 I just added a pause button recently and I've noticed that when I click the button it counts as a tap on the screen, so the player goes up.我最近刚刚添加了一个暂停按钮,我注意到当我单击该按钮时,它会被视为在屏幕上的点击,因此播放器会上升。 Basically, in the same exact moment that I press the pause button, the player jumps and then the game freezes while he is in mid-air.基本上,在我按下暂停按钮的同一时刻,玩家跳跃,然后游戏在他在半空中时冻结。 Is there a way I could make so the button wouldn't count as a tap?有没有办法让按钮不被视为点击?

Here's the part of the player script that makes him move:下面是让他移动的玩家脚本部分:

            if (Input.GetMouseButtonDown (0) && isDead == false && isKnifeDead == false && UiStartScript.uiInstance.firstClickUi == true) {
            rb.velocity = Vector2.up * velocity;
        }

Heres's the part of the UI script for the pause button:这是暂停按钮的 UI 脚本的一部分:

public void PauseGame()
{
    Time.timeScale = 0;
}

My EventSystem: EventSystem我的事件系统:事件系统

My Button: Button我的按钮:按钮

Hey man hope this helps.嘿,伙计,希望这会有所帮助。

In U. I script add public GameObject PlayerConrtollerScript;在 U. 我脚本添加公共 GameObject PlayerConrtollerScript; In your UI Script, under the class Pausgame() Add this code.在您的 UI 脚本中,在 class Pausgame() 下添加此代码。 GameObject.PlayerControllerScript.SetActive = false; GameObject.PlayerControllerScript.SetActive = false;

When unpaused you need to add to your script that has the Time.未暂停时,您需要添加到具有时间的脚本中。 timeScale =1;时间刻度=1; Add this code.添加此代码。 GameObject.PlayerControllerScript.SetActive = true; GameObject.PlayerControllerScript.SetActive = true;

After you have added the code mentioned above.添加上述代码后。 Don't forget to add the player game object from your hierarchy into the Inspector with the U. I script, the space will be available to add a game object when you scroll down to your U. I script.不要忘记使用 U.I 脚本将玩家游戏 object 从您的层次结构添加到 Inspector 中,当您向下滚动到 U.I 脚本时,该空间将可用于添加游戏 object。

Hope it works for you and if you struggle with it let me know.希望它对您有用,如果您遇到困难,请告诉我。 I will try help you as much as I can.我会尽力帮助你。

That will stop your player from moving around during pause.这将阻止您的播放器在暂停期间四处移动。

An easy with no code required is to remove the code about detecting tap on screen and instead place a button that covers the whole screen (invisible button).一个不需要代码的简单方法是删除关于检测屏幕上的点击的代码,而是放置一个覆盖整个屏幕的按钮(隐形按钮)。

That button event would trigger the screen movement.该按钮事件将触发屏幕移动。

You can add extra buttons on top and Unity will automatically discard the tap from any other button below.您可以在顶部添加额外的按钮,Unity 将自动放弃来自下方任何其他按钮的点击。

This way you can start adding UI all over without checking in code which should be considered.通过这种方式,您可以开始添加 UI,而无需签入应考虑的代码。

This is the approach that I been done.这是我已经完成的方法。 I create a cople of tricks using Event System and Canvas for take advantage of the Mouse Input, you don't need the use of Updates functions for do the basic gameplay.我使用事件系统和 Canvas 创建了一系列技巧来利用鼠标输入,您不需要使用更新功能来进行基本游戏。

Here's my example project: TapAndJump on Github这是我的示例项目: Github 上的 TapAndJump

Check the GameObjects called "Click Panel" and "Pause Button" and respective linked methods.检查名为“单击面板”和“暂停按钮”的游戏对象以及相应的链接方法。

JumpWithTap.cs JumpWithTap.cs

using UnityEngine;

public class JumpWithTap : MonoBehaviour
{
    [SerializeField] private UIHandler uiHandler;
    [SerializeField] private float jumpPower = 5f;
    
    private Rigidbody2D rb2D;
    private int counter;

    #region Class Logic
    public void Jump()
    {
        rb2D.velocity = Vector2.up * jumpPower;
        uiHandler.SetNumberToCounter(++counter);
    }
    #endregion

    #region MonoBehaviour API
    private void Awake()
    {
        rb2D = GetComponent<Rigidbody2D>();
    }
    #endregion
}

UIHandler.cs UIHandler.cs

using UnityEngine;
using UnityEngine.UI;

public class UIHandler : MonoBehaviour
{
    [SerializeField] private Text counterText;
    [SerializeField] private Text pauseText;
    [SerializeField] private Image clickPanel;

    private bool isPaused = false;
    public bool IsPaused
    {
        get { return isPaused; }
        set 
        {
            pauseText.gameObject.SetActive(value);
            isPaused = value; 
        }
    }

    #region Class Logic
    public void SetNumberToCounter(int number) => counterText.text = number.ToString();

    public void DisableClickPanel() => clickPanel.raycastTarget = !clickPanel.raycastTarget;


    public void PauseGame()
    {
        IsPaused = !IsPaused;
        DisableClickPanel();
        // Time.timeScale = IsPaused == true ? 0 : 1; // If you want keep the Physics (ball fall) don't stop the time.
    }
    #endregion
}

I guided by your code example and so on;我以您的代码示例等为指导; be free that modify and request any issue.可以自由修改和请求任何问题。

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

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