nirasan's tech blog

趣味や仕事の覚え書きです。Linux, Perl, PHP, Ruby, Javascript, Android, Cocos2d-x, Unity などに興味があります。

Unity の NGUI で画像を実行時に変更する

画像を変更する

  • UISprite のアタッチされたオブジェクトの画像を変更する
  • UISprite.spriteName に Atlas の Sprite 名を文字列で指定すると画像を変更することができます。
obj.GetComponent<UISprite> ().spriteName = "new_sprite_name";

ボタン画像を変更する

  • UIButton と UISprite がアタッチされたオブジェクトで UISprite の画像を変更する
  • UIButton はクリックなどのイベント発生時に UISprite の画像を変更するため、ボタン画像の変更は UIButton 経由で行う必要がある
    • UISprite.spriteName を直接変更しても画像が切り替わるように見えるが、UIButton が内部的に持っている画像の設定は変更されないため、クリックなどのイベント発生時に元の画像に戻されてしまう
obj.GetComponent<UIButton> ().normalSprite = "new_sprite_name";