Author Topic: How to add downloaded texture to Altas by code on the fly?  (Read 11821 times)

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
How to add downloaded texture to Altas by code on the fly?
« on: April 13, 2012, 05:17:47 AM »
My app will download user photo from the web and use as their avatar image, in this way, I can do preset in unity, as must done by code? How can I do it? Any example? How should I start with?


Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #1 on: April 13, 2012, 06:38:16 AM »
Look into UITexture. There you can set any Material as the source. Making a new material from a texture you've downloaded is trivial; see unity answers for how to get it from the web.

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #2 on: April 15, 2012, 04:00:48 AM »
yes, i am doing that, i can succesfully download a photo to a gameobject plane mesh by:

renderer.material.mainTexture = www.texture;
i don"t even need to set create a instant of the material, or setting the shader etc....

but in UItexture, although there is a material public variable, I cannot show a photo but just a question mark...i have tried to create a new instance of material with different texture format such as RGB DXB, none of them work....

myUItextureobject.material.mainTexture = www.texture;


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #3 on: April 15, 2012, 04:04:03 AM »
Whenever you access 'renderer.material', Unity creates a copy of 'renderer.sharedMaterial' for you, silently. So you actually are creating a new material there, just without realizing it. :)

UITexture doesn't do this when you access its material property, as it points to an existing material. You have to have a material already present in there in order for it to work, or create a new one:
  1. myUItextureobject.material = new Material("Unlit/Transparent Colored");
  2. myUItextureobject.material.mainTexture = www.texture;
  3. myUItextureobject.MakePixelPerfect();

cheuksh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 28
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #4 on: April 15, 2012, 06:37:11 AM »
Fixed work very well now!!! Thx a lot...one small thing..
The UItexture is ignoring the depth setting. other ui widget works fine.
you can see from the photo background is depth 3, white color text is depth 1, yellow color text is depth 5

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #5 on: April 15, 2012, 10:51:44 AM »
Because it's not in the same atlas as the rest, the depth will not work directly. You have to set the z-index on the transform to something lower than the rest. (for instance -1). Then it will be closer to the camera, and rendered on top of the rest.

timt

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: How to add downloaded texture to Altas by code on the fly?
« Reply #6 on: May 30, 2012, 01:12:41 PM »
I think the material constructor is supposed to be :

new Material(Shader.Find("Unlit/Transparent Colored"));

otherwise it will actually try to compile that string into a shader.