按下时更改切换按钮背景的功能 [英] Function to change background of a toggle button when pressed

查看:139
本文介绍了按下时更改切换按钮背景的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个功能,可以在按下切换按钮时更改切换按钮的背景图像,但对我来说效果不佳.

I am creating a function to change the background image of a toggle button when it is pressed but it is not working out for me.

我目前具有此功能:

public Image Background1;
public Image Background2;
public Toggle theToggle;

public void ChangeBackground()
        {
        if (theToggle.isOn) {
            theToggle.image () = Background1;
        }
        else
        {
            theToggle.image() = Background2;
            }
        }

我的问题是,在场景视图中,我没有收到像在其他脚本上一样将图像添加到Background1和Background2的slot选项.此外,尽管图像参数器在脚本参考中,并且编辑器无法识别图像参数器,但我使用以下方法添加了UI库:

My problem is that in the scene view I don't receive the slot option to add an image to Background1 and Background2 like it happens on other scripts. Also the image parameterer is not recognized by the editor despite the fact it is on the scripting reference and that I have added the UI lybrary using:

using UnityEngine.UI;

此外,我试图将ChangeBackground()函数放入这样的Update()函数中,并且收到解析错误:

Also, I tried to put the ChangeBackground() function inside the Update() function like this and I receive a parse error:

public Image Background1;
public Image Background2;
public Toggle theToggle;

void Update () 
    {
        public void ChangeBackground()
        {
        if (theToggle.isOn) {
            theToggle.image () = Background1;
        }
        else
        {
            theToggle.image() = Background2;
            }
        }
    }

此解析错误非常奇怪,因为这是我在其他脚本中所做的方式.你能帮我吗?

This parse error is very strange because this is the way i did in other scripts. Can you help me?

推荐答案

好吧,您有一个编译器错误,因为您试图同时调用函数"image"并为其分配一个值.我的猜测是您想将image属性设置为某些Sprite类型的变量"Background1"

Well for one, you have a compiler error because you're attempting to call the function "image" and assign it a value at the same time. My guess is you wanted to set the image attribute to some Sprite type variable "Background1"

因此,您将像这样进行更改:

So you'd change that like this:

public void ChangeBackground()
{
        if (theToggle.isOn) {
            theToggle.image = Background1;
        }
        else
        {
            theToggle.image = Background2;
            }
        }        
}

现在,当出现编译器错误时,检查器将不会使用脚本中的新字段进行更新.因此,如果您在脚本中的某个位置(通常在类声明之后)声明了变量,如下所示:

Now, when there is a compiler error, the inspector will not update with new fields from a script. So if you have declared variables somewhere in your script (usually after the class declaration) like so:

public Image background1;
public Image background2;

只要您没有编译器错误且它们是公开的,它们就会出现在检查器中,等待设置

They will appear in the inspector, waiting to be set, as long as you have no compiler errors and they are public

此外,您也不能使用C#和大多数编程语言编写函数声明.

Also, you can't put a function declaration, nor would you want to, in C# and most programming languages.

必须ChangeBackground功能拖到您的切换上.拖到这里...

You MUST drag the ChangeBackground function ON TO YOUR TOGGLE. Drag it to here...

注意!以下是高度高级的信息,与此类增长问题没有直接关系.

Note! the following is highly advanced information, not immediately relevant to Grow's question as such.

每当切换一次切换时,应用ChangeBackground函数的简单方法,只需包含以下两个函数,即可在脚本中轻松完成此操作:

An easy way to apply the ChangeBackground function whenever the toggle is toggled, you could simply do it in script by including these two functions:

void OnEnable()
{
    theToggle.onValueChanged += ChangeBackground
}

void OnDisable()
{
    theToggle.onValueChanged -= ChangeBackground
}

这篇关于按下时更改切换按钮背景的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆