Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: gaurav.maniar on August 06, 2014, 02:14:35 AM

Title: changing sprite2D image at run time
Post by: gaurav.maniar 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.         }
Title: Re: changing sprite2D image at run time
Post by: Yukichu on August 06, 2014, 08:35:59 AM
http://docs.unity3d.com/ScriptReference/Resources.LoadAssetAtPath.html (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?

Title: Re: changing sprite2D image at run time
Post by: ArenMook on August 07, 2014, 01:20:48 AM
Yeah you need to use Resources.Load, not Resources.LoadAssetAtPath. Check Unity's documentation.