Author Topic: Load new texture into UISprite from code  (Read 11498 times)

sunkas

  • Guest
Load new texture into UISprite from code
« on: October 15, 2012, 04:36:33 AM »
Hi

I am trying to download a picture from the web and then assigning it to a existing UISprite. I have gotten the texture but have no clue on how to load it into an existing UISprite. It uses an atlas from the start but I want to replace it with a texture.

  1.         public void AddProfilePictureFromItsUse()
  2.         {
  3.                 UITexture ut = NGUITools.AddWidget<UITexture> (pictureParent);
  4.                 ut.material = new Material (Shader.Find ("Unlit/Transparent Colored"));
  5.                 ut.material.mainTexture = PictureTexture;
  6.                 ut.MakePixelPerfect ();
  7.         ReferenceToMySprite.loadPicture = ut; //Something like this????
  8.         }

What to write on last row?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Load new texture into UISprite from code
« Reply #1 on: October 15, 2012, 06:04:43 AM »
You can't assign a texture to a sprite.

Use UITexture to draw your texture directly.

sunkas

  • Guest
Re: Load new texture into UISprite from code
« Reply #2 on: October 15, 2012, 09:56:00 AM »
Hmm. This seems to be a bit of a tricky problem. I now have two problems, first Android complains when fetching a image file containing not "http://" but if "http://" is left in the url, it generates an empty error. In iOS I at least get a picture, even if it is a "?" picture. This without "http://".

  1.         public void AddProfilePictureFromItsUserForParent()
  2.         {
  3.                 UITexture ut = Picture.AddComponent<UITexture>();
  4.                 ut.material = new Material (Shader.Find ("Unlit/Transparent Colored"));
  5.                 ut.material.mainTexture = chatFriendObject.PictureTexture;
  6.                 ut.MakePixelPerfect ();
  7.         }
   
  1.         private IEnumerator GetImageFromWWW(ChatFriend chatFriend, GetImageFromWWWDelegate callback, GetImageFromWWWErrorDelegate errorCallback)
  2.         {                      
  3.                 String url = chatFriend.PictureUrl;
  4.                 Debug.Log ("GetImageFromWWW:" + url);
  5.                 WWW connection = new WWW(url);
  6.                
  7.                 yield return connection;
  8.                        
  9.                 Debug.Log ("GetImageFromWWW2");
  10.                 if (connection.error != null)
  11.                 {
  12.                         try
  13.                         {
  14.                                 Texture2D texture = new Texture2D(chatFriend.PictureWidth, chatFriend.PictureHeight);
  15.                                 connection.LoadImageIntoTexture(texture);
  16.                                 callback(texture);
  17.                         }
  18.                         catch (Exception e)
  19.                         {
  20.                                 errorCallback(e.ToString());
  21.                         }
  22.                 }
  23.                 else
  24.                 {
  25.                         Debug.Log ("GetImageFromWWW2Error");
  26.                         errorCallback(connection.error);
  27.                 }
  28.                
  29.         }
On Android I get the following error:
You are trying to load data from a www stream which had the following error when downloading.
java.net.MalformedURLException: Protocol not found: profile.ak.fbcdn.net/hprofile-ak-ash3/c...013_97745536_a.jpg

If I use do not remove https from the url I get an empty error (connection.error == "").

Any ideas?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Load new texture into UISprite from code
« Reply #3 on: October 15, 2012, 01:37:56 PM »
This is a Unity question, not an NGUI question. Have you looked at DownloadTexture example class?

JSorrentino

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Load new texture into UISprite from code
« Reply #4 on: April 08, 2014, 04:49:58 PM »
I'm having an issue with loading a texture (facebook 64x64 profile pic) onto objects that scroll in a grid under a scroll view.

When a texture is used as one of the elements in the view, rapid scrolling crashes the device.

Aren, do you have a proper example of loading a texture and properly unloading any resources (the www.texture) properly?

I'm thinking my issue is the fact that it's a texture, as opposed to a sprite.  If I activate the placeholder sprite, scrolling seems fine - no crashes or ANRs.

I *think* the issue is the added draw calls from using textures, combined with rapid scrolling. I know there is a memory leak issue without properly handling unloading the texture (example that's 100% accurate would help there, but this issue is different).

Thanks!

EDIT:
Here's an ANR Crash report.  Being that you are better at this than me, maybe it will point to something obvious.
This report was generated with static elements in the grid.  One of the panel items is a UITexture. Nothing loaded dynamically.
All I did to cause it is scroll the list view on the device and let it keep bouncing back.
« Last Edit: April 08, 2014, 06:06:09 PM by JSorrentino »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Load new texture into UISprite from code
« Reply #5 on: April 09, 2014, 03:24:14 AM »
The ANR isn't overly useful. It just points to some issue that seems to be in adreno-based rendering. Make sure that multi-threaded rendering is turned off. The DownloadTexture takes care of destroying the texture resource and disposing of the WWW correctly.

JSorrentino

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Load new texture into UISprite from code
« Reply #6 on: April 09, 2014, 09:05:13 AM »
OK, so it was the same as I deemed, not enough useful info to point to something obvious.

The DownloadTexture?   (edit, looked it up)  But this is exactly what I am doing in the root of the download, for loading and destruction.

I even thought that it could be a Android OS, so built as 2.3 -> 4.3 and tested all, apparently with the same error.

The one thing I am doing is destroying the objects that the texture is on,  but I've since removed all textures and tested with sprites only from the atlas - to the same dismay.

My object destruction is only on 2 UI panels that display facebook 64^2 textures.  I destroy, then clean up.  I was more worried that the destruction was leaving remnants (from www.texture -> mTex in your example), I also destroy the texture, specifically in the OnDestroy method.  But, who knows, I'm recoding that to recycle the objects just to see if it has anything to do with fragmentation on the OS.

JSorrentino

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 30
    • View Profile
Re: Load new texture into UISprite from code
« Reply #7 on: April 09, 2014, 07:54:27 PM »
OK, so after hours of looking at code, I finally figured this out.

It was an issue due to a Texture being used as a backdrop, it should have been a sprite.  Somehow it was converted, I probably deleted it from the atlas. Who knows.  On top of that, I use transparency, so VBO issues make sense now.  I made the texture a sprite, as it should have been, and removed another later of transparency to be safe.  All is good in life again.

On a side note, trying to add a 1920x1080 sprite to the atlas crashes the Atlas Builder :-)

Thank for the time, again, really appreciated.