Unity3D C# Button 精灵交换 - 在运行时附加图像
Unity3D C# Button sprite swap - attach images at runtime
我正在创建按钮,而我的"Hero Selection Menu"正在创建。这些按钮将 获得相关的 images/Sprites,具体取决于它们将代表的 "Hero"。
我有以下方法,但我不明白我必须将精灵应用到哪个变量。
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.image = _normalSprite; //.image is not correct
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.image = _normalSprite; //.image is not correct
//...
}
这些状态确实有效,我已经通过 Debug.Log(...);
确认了
同样的问题是:如果不是 .image,必须更改哪个值?
提前致谢,
Csharpest
您正在尝试将 sprite 附加到按钮组件。精灵位于图像组件中。看看这个!
GameObject buttonGameObject;
Sprite newSprite;
void Start() {
buttonGameObject.GetComponent<Image>().sprite = newSprite;
}
但要修复您的代码,您可能会做类似的事情:
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
}
如果你想在脚本中更改按钮 spriteswap 精灵,你必须使用 spriteState
,你可以这样做;
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
void ChangeSprites(){
// _thisButton.transition = Selectable.Transition.SpriteSwap;
var ss = _thisButton.spriteState;
_thisButton.image.sprite = _normalSprite;
//ss.disabledSprite = _disabledSprite;
ss.highlightedSprite = _highlightSprite;
//ss.pressedSprite = _pressedSprie;
_thisButton.spriteState = ss;
}
如果您使用普通按钮并选择 SpriteSwap,Unity 会自动在按钮中进行交换,如果您需要更改过渡选项,请取消注释该函数的第一行。
可以通过在运行时改变图片组件的sprite属性来实现。
button1.GetComponent<Image>().sprite = sprite;
完整演示代码:
using UnityEngine;
using UnityEngine.UI;
public class SpriteChangeDemo : MonoBehaviour
{
public Button button1;
public Sprite sprite1, sprite2;
void Start()
{
ChangeSprite(sprite1);
ChangeSprite(sprite2);
}
public void ChangeSprite(Sprite sprite) {
button1.GetComponent<Image>().sprite = sprite;
}
}
我正在创建按钮,而我的"Hero Selection Menu"正在创建。这些按钮将 获得相关的 images/Sprites,具体取决于它们将代表的 "Hero"。
我有以下方法,但我不明白我必须将精灵应用到哪个变量。
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.image = _normalSprite; //.image is not correct
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.image = _normalSprite; //.image is not correct
//...
}
这些状态确实有效,我已经通过 Debug.Log(...);
确认了同样的问题是:如果不是 .image,必须更改哪个值?
提前致谢, Csharpest
您正在尝试将 sprite 附加到按钮组件。精灵位于图像组件中。看看这个!
GameObject buttonGameObject;
Sprite newSprite;
void Start() {
buttonGameObject.GetComponent<Image>().sprite = newSprite;
}
但要修复您的代码,您可能会做类似的事情:
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
protected override void DoStateTransition (SelectionState state, bool instant){
switch (state) {
case Selectable.SelectionState.Normal:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
Debug.Log("statenormalasd");
break;
case Selectable.SelectionState.Highlighted:
_thisButton.GetComponent<Image>().sprite = _normalSprite;
}
如果你想在脚本中更改按钮 spriteswap 精灵,你必须使用 spriteState
,你可以这样做;
Button _thisButton;
Sprite _normalSprite;
Sprite _highlightSprite;
void ChangeSprites(){
// _thisButton.transition = Selectable.Transition.SpriteSwap;
var ss = _thisButton.spriteState;
_thisButton.image.sprite = _normalSprite;
//ss.disabledSprite = _disabledSprite;
ss.highlightedSprite = _highlightSprite;
//ss.pressedSprite = _pressedSprie;
_thisButton.spriteState = ss;
}
如果您使用普通按钮并选择 SpriteSwap,Unity 会自动在按钮中进行交换,如果您需要更改过渡选项,请取消注释该函数的第一行。
可以通过在运行时改变图片组件的sprite属性来实现。
button1.GetComponent<Image>().sprite = sprite;
完整演示代码:
using UnityEngine;
using UnityEngine.UI;
public class SpriteChangeDemo : MonoBehaviour
{
public Button button1;
public Sprite sprite1, sprite2;
void Start()
{
ChangeSprite(sprite1);
ChangeSprite(sprite2);
}
public void ChangeSprite(Sprite sprite) {
button1.GetComponent<Image>().sprite = sprite;
}
}