按下切换按钮时更改背景 [英] Change background of toggle button when it is pressed
问题描述
我看到在此问题上已经存在一些问题,但是他们没有使用unity3d或使用旧的UI unity方法.
I see there is already some questions on this matter, but they are not using unity3d or are using the old UI method of unity.
我有一个具有特定背景的切换按钮,并且我希望在关闭切换时更改背景.我尝试使用检查器执行此操作,并设置了一个功能,以在关闭按钮时将不同的纹理应用于按钮,但没有用.
I have a toggle button with a specific background and I want that background to change when the toggle is off. I tried to do this using the inspector and setting a function to apply a different texture to the button when it was off, but did not work.
您对如何实现此目标有任何想法吗?我附上我尝试过的东西.
Do you have any idea on how to accomplish this? I am attaching what I tried.
推荐答案
切换的背景只是Image
就这么简单,在脚本中有
so it's this simple, in a script have
public Image theBackground;
然后您可以按自己希望的任何方式更改它!
you can then change that in any way you wish!
如有需要,请提供功能
public Image theBackground;
public Toggle theToggle;
public void ChangeBackground()
{
if (theToggle.isOn)
... set theBackground as you wish ..
else
... set theBackground as you wish ..
}
可对其进行适当设置.查看切换开关,然后将该函数拖动到OnValueChanged.
which sets it appropriately. Look in the toggle and drag that function to the OnValueChanged.
享受!
这篇关于按下切换按钮时更改背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!