Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Change a Button From Script

Discussion in 'UGUI & TextMesh Pro' started by comixplay, Sep 9, 2014.

  1. comixplay

    comixplay

    Joined:
    Sep 27, 2013
    Posts:
    104
    Hi,
    Sorry if it's in the docs or forum, I couldn't find anything ...
    For Button with SpriteSwap - Is there any way to change the Highlighted, Pressed and Disabled sprites attributes from script?
    Should I use SpriteState?
     
  2. djweinbaum

    djweinbaum

    Joined:
    Nov 3, 2013
    Posts:
    533
    It looks like its:
    Code (CSharp):
    1. someButton.spriteState.disabledSprite = someSprite;
    Its kind of secret but in the editor if you go to Help > Scripting Reference it will open the offline docs that ship with your version of unity in your browser, and in 4.6 those include the API for the UI. The normal online ones won't include that stuff until uGUI is done with beta I think.
     
    comixplay likes this.
  3. comixplay

    comixplay

    Joined:
    Sep 27, 2013
    Posts:
    104
    Thanks djweinbaum,
    The docs doesn't say much about SpriteState... it works fine.

    Sprite newSprite = Resources.Load<Sprite>("sprites/somesprite");
    SpriteState st = new SpriteState();
    st.disabledSprite = newSprite;
    st.highlightedSprite = newSprite;
    st.pressedSprite = newSprite;
    _button.spriteState = st;
     
    Umai likes this.