Author Topic: changing sprite2D image at run time  (Read 7650 times)

gaurav.maniar

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 9
    • View Profile
changing sprite2D image at run time
« on: August 06, 2014, 02:14:35 AM »
i want to change image of button when clicked, so doing it with following code.

it works fine with unity simulator, but when loaded it on iPhone, iPad, image is not getting changed.

1st click image disappear, 2nd click default image gets visible ... and goes on, but it is not changing the image.

  1. public void sound_toggle()
  2.         {
  3.                 Debug.Log ("Sound");
  4.  
  5.                 if (sound_status)
  6.                 {
  7.                         gameObject.GetComponent<UI2DSprite> ().sprite2D = Resources.LoadAssetAtPath<Sprite> ("Assets/Mainscreen/Images/sound_off.png");
  8.                         sound_status = false;
  9.                 }
  10.                 else
  11.                 {
  12.                         gameObject.GetComponent<UI2DSprite> ().sprite2D = Resources.LoadAssetAtPath<Sprite> ("Assets/Mainscreen/Images/sound_on.png");
  13.                         sound_status = true;
  14.                 }
  15.         }

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: changing sprite2D image at run time
« Reply #1 on: August 06, 2014, 08:35:59 AM »
http://docs.unity3d.com/ScriptReference/Resources.LoadAssetAtPath.html

It says it's Editor Only.

Why isn't the sprite in part of an atlas, and you just... change the sprite?


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: changing sprite2D image at run time
« Reply #2 on: August 07, 2014, 01:20:48 AM »
Yeah you need to use Resources.Load, not Resources.LoadAssetAtPath. Check Unity's documentation.