如何通过单击屏幕上的任意位置来更改按钮图像?

How to make button image change with on click anywhere on screen?

我是 Unity 的新手,我无法理解 UI 按钮等的点击可能性。在我的项目中,我有一个按钮,每当我点击它时它的图像和方式都会改变我做的是:我创建了这个脚本:

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


[RequireComponent(typeof(Button))]

public class OnClickScript : MonoBehaviour
{
    public static int spritenumber=1;
    public Button mybutton;
    public Sprite square;
    public Sprite circle;
    public Sprite triangle;
    public int counter = 1;

    void Start()
    {
        mybutton = GetComponent<Button>();


    }

    public void changeButton()
    {
        if (PAUSESCRIPTE.isPaused == false)
        {
            counter++;
            if (counter == 1)
            {
                mybutton.image.overrideSprite = square;
                spritenumber = 1;
                soundmanagerscript.PlaySound("buttonpress");



            }
            if (counter == 2)
            {
                mybutton.image.overrideSprite = circle;
                spritenumber = 2;
                soundmanagerscript.PlaySound("buttonpress");

            }
            if (counter == 3)
            {
                mybutton.image.overrideSprite = triangle;
                spritenumber = 3;
                counter = counter - 3;
                soundmanagerscript.PlaySound("buttonpress");
            }
        }

    }
}

我将这个脚本附加到我的按钮上,选择了我想要使用的精灵,并将 On Click() 函数添加到按钮上。

之后我决定不再通过单击按钮本身来更改按钮的精灵,而是通过单击屏幕上的任意位置来更改按钮的精灵。我尝试使用 Input.GetMouseButtonDown(0) 但没有得到任何结果,因为我使用不正确。非常欢迎任何关于如何实现我想要的帮助。提前致谢。

这是我的做法:

using UnityEngine.UI;
using UnityEngine;

public class ButtonScript : MonoBehaviour
{
    // Public objects
    public Sprite[] sprites;

    //Private objects/variables
    private Image buttonImage;
    private int spriteIndex;

    void Start()
    {
        buttonImage = GetComponent<Image>(); // Get the image component of your button
        spriteIndex = 0; // Set the index to 0
        buttonImage.sprite = sprites[spriteIndex]; // Set the image to the first image in your sprite array
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            spriteIndex++; // Increment the index
            if (spriteIndex > sprites.Length - 1) // Ensure that you stay within the array size
            {
                spriteIndex = 0;
            }
            buttonImage.sprite = sprites[spriteIndex]; // Set the image
        }
    }
}

但是,您需要将图像设置更改为:

图片类型:简单

使用 Sprite Mesh:正确

(可能)保留方面:true

作为对您上述评论的答复,我建议您在后台设置一个按钮,在上方设置一个暂停按钮。然后,像以前一样用事件 OnClick() 替换 Update() 方法。

您的暂停按钮会捕获左键单击,而您的背景按钮不会。 请务必在 Button 组件中勾选 Interactable

然后,如果不需要,您可以安全地删除背景按钮的 Image 组件。


这意味着您可以为暂停按钮创建(或修改)class

using UnityEngine.EventSystems;

public class PauseButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    public bool IsMouseOver = false;

    public void OnPointerEnter(PointerEventData eventData)
    {
        IsMouseOver = true;
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        IsMouseOver = false;
    }

    // --
    // Your OnClick method
    // --
}

注:使用eventSystems.

现在您可以在 amengelbrecht 发送给您的代码中使用此布尔值。

我将从他的回答中复制并粘贴他的代码,并根据上面的 class 添加 bool 值。

public class BackgroundButtonScript : MonoBehaviour
{
    // Public objects
    public Sprite[] sprites;
    public PauseButton pauseButton;

    //Private variables
    private Image buttonImage;
    private int spriteIndex;

    private void Start()
    {
        buttonImage = GetComponent<Image>(); // Get the image component of your button
        spriteIndex = 0; // Set the index to 0
        buttonImage.sprite = sprites[spriteIndex]; // Set the image to the first image in your sprite array
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0) && !pauseButton.IsMouseOver)
        {
            spriteIndex++; // Increment the index
            if (spriteIndex > sprites.Length - 1) // Ensure that you stay within the array size
            {
                spriteIndex = 0;
            }
            buttonImage.sprite = sprites[spriteIndex]; // Set the image
        }
    }
}

这将允许您单击屏幕上的任意位置,除非鼠标悬停在暂停按钮上。在这种情况下,将调用暂停按钮的 OnClick。

小心

如果您的游戏处于暂停状态并且用户在除暂停按钮以外的任何地方单击鼠标左键,这取决于暂停的工作方式,将调用 BackgroundButtonScript 的 Update() 方法并可能导致 if (Input.GetMouseButtonDown(0) && !pauseButton.IsMouseOver)