How to use download picture in WebGL? - unity3d

I want to download a jpg, and use it in my game.
This is my code, and this code works very well in unity Editor and unity web player. But when I build a WebGL, it just show a picture with a "?". Is there something wrong?
This the "?" picture
IEnumerator TestDownPNG() {
string url = "https://xxxx/test_bg.jpg";
WWW www = new WWW(url);
yield return www;
Texture2D texture2d = www.textureNonReadable;
Sprite sprite = Sprite.Create (texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
objectA.GetComponent<SpriteRenderer>().sprite = sprite;
}

Related

How to set Texture2D to skybox Panoramic material?

I'm developing a VR App by using skybox Panoramic material. And I want to bind the material with a Texture2D created in C# script, then rendering the Texture2D in native plugin, the codes is below, but the skybox doesn't show anything, why?
private int texWidth = 2304;
private int texHeight = 2304;
private Texture2D tex;
// Start is called before the first frame update
void Start()
{
// Create a texture
Texture2D tex = new Texture2D(2304, 2304, TextureFormat.ARGB32, true);
// Set point filtering just so we can see the pixels clearly
tex.filterMode = FilterMode.Trilinear;
// Call Apply() so it‘s actually uploaded to the GPU
tex.Apply();
init(tex.GetNativeTexturePtr());
// Set texture onto our material
//RenderSettings.skybox.SetTexture("_MainTex", tex);
RenderSettings.skybox.mainTexture = tex;
}

How do i take a screenshot with camera clearly in Unity

[Original picture]
[The picture taken in unity]
I wanna take a screenshot with camera in unity. i can get a screenshot with camera but it looks weird like second picture. how can i solve this problem?
public void ScreenShot(int imgName)
{
RenderTexture activeRenderTexture = RenderTexture.active; RenderTexture.active = targetCam.targetTexture;
targetCam.Render();
Texture2D image = new Texture2D(targetCam.targetTexture.width, targetCam.targetTexture.height);
_texture = image;
image.ReadPixels(new Rect(0, 0, targetCam.targetTexture.width, targetCam.targetTexture.height), 0, 0);
image.Apply();
RenderTexture.active = activeRenderTexture;
byte[] bytes = image.EncodeToPNG();
}
This is the code used in unity

Imaged loaded via new WWW doesn't display in a build, but it does in the player

I'm loading a .jpeg file in at runtime from disk, it works absolutely fine in the player but when I build the project the texture being loaded in via WWW doesn't display - no errors. It suggests a format issue, but like I said it renders as expected in the player:
Unit doc: https://docs.unity3d.com/ScriptReference/WWW.LoadImageIntoTexture.html
private IEnumerator SetMaterialImage(string path)
{
Texture2D tex;
tex = new Texture2D(4, 4, TextureFormat.RGB24, false, true);
WWW www = new WWW(path);
if (!string.IsNullOrEmpty(www.error))
{
Debug.Log("ERROR REDNERING IMAGE --- " + www.error);
}
while (!www.isDone) yield return null;
if (www.isDone)
{
Debug.Log("WWW,ISDONE = TRUE");
Shader shader = Shader.Find("Standard");
www.LoadImageIntoTexture(tex);
GetComponent<Renderer>().material.mainTexture = tex;
GetComponent<Renderer>().material.shader = shader;
}
}
Edit: please don't suggest the Resources folder - this particular application must load from disk
I was unable to locate any Unity documentation that explains why this scenario differed between Player and Build, however I was able to confirm that:
GetComponent<Renderer>().material.mainTexture = tex;
Renderer>().material.shader = shader;
Should have been declared in the opposite order:
Renderer>().material.shader = shader;
GetComponent<Renderer>().material.mainTexture = tex;
Edit: Credit #Programmer 'The reason that code work in the Editor is because the Editor remembers which Shader or Texture is assigned to a material. So, when you change the shader, it uses the old texture'

Unity texture from disk has low resolution

I am trying to load a texture(and create a sprite from it eventually) from disk but sprite renders as low resolution image.
What I am doing:
-> Download the image from url. Once the image is downloaded, I save the texture as png to disk so that next time it doesn't requires a download.
WWW www = new WWW(url);
yield return www;
if (www.isDone)
{
if (string.IsNullOrEmpty(www.error))
{
Sprite img = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
reward.RewardSprite = img;
byte[] bytes = www.texture.EncodeToPNG();
FileManager.SaveRewardImage(reward.rewardId, bytes);
}
else
{
Debug.Log(www.error);
}
}
-> Load from disk
string path = string.Format("Cache\\Venue\\{0}", nameWithoutExtension);
return Resources.Load<Texture2D>(path);
The first time when the texture loads from url, its resolution seems fine(because its the original one). When it loads from cache, it attenuates to a lower one.
Can someone tell me what am I missing, or even if there is way around it?
Thanks in advance.
You can overload your texture in your sprite creation in the same way that you do with the rectangle and specify in TextureFormat the format you need:
Sprite img = Sprite.Create(
new Texture2D (www.texture.width, www.texture.height, TextureFormat format, bool mipmap),
new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));

Read image and set texture

I add a cube to unity scene. I want to set this cub's texture by using an image.
I use below code to load image and set texture :
Texture2D text2D = new Texture2D(Screen.width, Screen.height,TextureFormat.RGB24 , false);
text2D.SetPixels(((Texture2D)Resources.Load("image")).GetPixels());
MeshRenderer renderer = cube.GetComponent<MeshRenderer>();
renderer.material.mainTexture = text2D;
I see only a gray cube not the image on the scene.
You can shorten this quite a bit with only:
renderer.material.mainTexture = Resources.Load<Texture2D>("image");
Note that if the image is not found then you get null.
To see changes on the Texture2D, use text2d.Apply();
This is even more easy to do.
Try
public GameObject _cube;
void Start()
{
Renderer rend = _cube.GetComponent<Renderer> ();
rend.material.mainTexture = Resources.Load ("image") as Texture;
}
LoadImage method can also be used to do the job. But here, you have to pass in the image as .bytes format.
Example:
public TextAsset image;
void Start()
{
var texture = new Texture2D(100, 100, TextureFormat.ARGB32, false);
texture.LoadImage(image.bytes);
GetComponent<Renderer>().material.mainTexture = texture;
texture.Apply();
}