Unity UI按钮状态通过代码转换

时间:2017-04-10 05:43:26

标签: c# unity3d unity5

我愿意通过C#中的代码更改按钮状态(按下,取消激活,突出显示)。就像某些动作一样,我想将按钮状态切换为deactive。我怎么能这样做?

我尝试设置按钮状态,但我没有找到任何属性接受spriteState。 我试图通过这个

设置禁用状态
        public Sprite activeBtnState;
        public Sprite deactiveBtnState;//button disable sprite that will be applied
        public Button BtnToChange;//button whoes state require to change

        public void NavigationTask() {
         if (isBtnActive)
                    {
                        BtnToChange.interactable = false;
                        BtnToChange.spriteState.disabledSprite = deactiveBtnState;
                    }
                    else
                    {
                        BtnToChange.interactable = true;
                    }
              }
  

错误11无法修改返回值   'UnityEngine.UI.Selectable.spriteState'因为它不是变量

此外是否有任何内置功能(功能)可自动更改我的按钮状态,而且我不编写代码来管理精灵?

2 个答案:

答案 0 :(得分:0)

答案在documentation

//Creates an instance of a sprite state (This includes the highlighted, pressed and disabled sprite.
// Assign the sprites in edit mode
public SpriteState sprState = new SpriteState(); // <- IMPORTANT DECLARATION
public Button BtnToChange;

// You can even change the transition type
public Selectable.Transition transition ;

 public void NavigationTask()
 {
     if (isBtnActive)
     {
         BtnToChange.interactable = false;
         BtnToChange.spriteState = sprState ; // <- IMPORTANT LINE
     }
     else
     {
         BtnToChange.interactable = true;
     }
 }

答案 1 :(得分:-1)

您可以使用Button.interactable更改Button州。

禁用:

BtnToChange.interactable = false;

启用:

BtnToChange.interactable = true;