Download and open png image? - c#

In Unity, this script loads on the start of the program. I want to download an image, and after that show it on the main screen. What should I do? The following code did not work.
My code:
using UnityEngine;
public class PushNotifications : MonoBehaviour {
IEnumerator Start () {
Texture2D textWebPic = null;
WWW image = new WWW("http://www.test.com/image.png");
yield return image;
image.LoadImageIntoTexture(textWebPic);
}
void Update () {
}
}

You can't pass null to LoadImageIntoTexture, since then Unity has no idea where to put the output (it is not ref). The texture must be initialized first.
It does not really matter, however, with what size or format you initialize it, unity will resize it anyway. So you can initialize some dummy, like this to load the image:
IEnumerator Start () {
Texture2D textWebPic = new Texture2D(2,2);
WWW image = new WWW("http://www.test.com/image.png");
yield return image;
image.LoadImageIntoTexture(textWebPic);
}
Another, and probably better option is to use WWW.texture instead of LoadImageIntoTexture, like this:
IEnumerator Start () {
WWW image = new WWW("http://www.test.com/image.png");
yield return image;
Texture2D textWebPic = image.texture;
}
See WWW class reference for more examples:
http://docs.unity3d.com/ScriptReference/WWW.html
And then to show it on the screen you have multiple option - creating a material with this texture, creating sprite from texture (best for 2d games) or simply use Graphics.DrawTexture.

Related

List of Texture2D

I am creating a simple game that need to capture a photo from the webcam every time the user click on some object. To avoid lag issues because of the task of convert the Texture2D to PNG and write the image on the disk, I am trying to store one List of Texture2D from the captures, and after the game ends, write all on the disk.
The problem is, when I capture one Texture2D texture from the webcam and try to do a List.Add(texture), all the elements of the list are updated because it store the reference of texture, not the Texture itself. Can anyone please, suggest me one alternative to store all the textures?
Edit: inserting code.
public class GetPhoto : MonoBehaviour
{
WebCamTexture webcam;
Texture2D photo;
List<Texture2D> photos;
IEnumerator TakePhoto()
{
//run when user click on object.
yield return new WaitForEndOfFrame();
photo.SetPixels(webcam.GetPixels());
photo.Apply();
photos.Add(photo);
}
}
As said rather use a new Texture2D instance for every press like e.g.
IEnumerator TakePhoto()
{
//run when user click on object.
yield return new WaitForEndOfFrame();
var photo = new Texture2D(yourTextureWidth, yourTextureHeight);
photo.SetPixels(webcam.GetPixels());
photo.Apply();
photos.Add(photo);
}
otherwise every entry in your list refers to the exact same Texture2D instance => everytime you overwrite the content of that instance.

Unity : From large image to map / terrain / texture

In Unity, I'd like to generate 2D map from a large image file (965012573).
I already have a tile/image pieces generator but it generates 1900 images 256256, and it seems hard to create a map by hand like this (and I have more then one large image to process...).
These image pieces are sorted like this :
x/y.png
Where x starts from left to right and y from top to bottom.
As MelvMay suggests me here, I should use textures to achieve that, but how?
Do you have any idea of how to achieve that programmatically? Or may be with an existing bundle?
Thanks a lot!
[edit] As a workaround, I tried this :
public class MapGenerator : MonoBehaviour {
private Renderer m_Renderer;
private Texture2D m_MainTexture;
// Use this for initialization
void Start () {
//Fetch the Renderer from the GameObject
m_Renderer = GetComponent<Renderer> ();
m_MainTexture = LoadPNG("Assets/Tiles/Ressources/Map/0/3.png");
m_Renderer.material.mainTexture = m_MainTexture;
}
private Texture2D LoadPNG(string filePath) {
Texture2D tex = null;
byte[] fileData;
if (File.Exists(filePath)) {
UnityEngine.Debug.Log(filePath);
fileData = File.ReadAllBytes(filePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
}
return tex;
}
}
But even if I apply it on an empty object, I only see my dynamic texture in the inspector, not in the scene when I press run...

Unity - Set material from Downloaded image instead of resources

I can successfully load a material saved in my Unity project using the code below:
RenderSettings.mat1 = (Material)Resources.Load ("images/img1.jpg", typeof(Material));
However, I am now trying to load an external image by downloading it.
Texture2D imgDownloaded;
string url = "http://www.intrawallpaper.com/static/images/1968081.jpg";
void Start()
{
StartCoroutine(getImg());
fucntionx ();
}
public void functionx()
{
RenderSettings.mat1 = (Material)imgDownloaded;
}
IEnumerator getImg()
{
yield return 0;
WWW dl = new WWW(url);
yield return dl;
imgDownloaded = dl.texture;
}
However, I get the message that I cannot convert from Texture2D to Material .
Is there any way to fix this?
Try:
yourMaterial.mainTexture = yourTexture;
A material consists of many textures, so naturally you can't convert between them.

Capture and Show Video from IP camera source (Unity3d + c#)

Need your help.
I am making simple application (and I'm a new to Unity3d also), which takes video from IP-camera and displays it onto Texture2D.
Video format is MJPG.
For simple jpg-images the code below works fine, but when I try to display MJPG I just get gray screen.
Did I make a mistake in code?
public class testVid : MonoBehaviour {
//public string uri = "http://24.172.4.142/mjpg/video.mjpg"; //url for example
public Texture2D cam;
public void Start() {
cam = new Texture2D(1, 1, TextureFormat.RGB24, false);
StartCoroutine(Fetch());
}
public IEnumerator Fetch() {
while(true) {
Debug.Log("loading... " + Time.realtimeSinceStartup);
WWWForm form = new WWWForm();
WWW www = new WWW("http://24.172.4.142/mjpg/video.mjpg");
yield return www;
if(!string.IsNullOrEmpty(www.error))
throw new UnityException(www.error);
www.LoadImageIntoTexture(cam);
}
}
public void OnGUI() {
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), cam);
}
}
I used this plugin https://www.assetstore.unity3d.com/en/#!/content/15580
Add the script to a game object
Set the URL of the video in the script
Create a new material of Unlit 2D
Add this material to the movie script in the inspector
Then assign the same material to the game object you
want to display the video (e.g. a quad)
Hope it helps

Texture not Assigning to GameObject on Button Click

I'm hung up on something that I've not been able to work out and I could really use a fresh set of eyes on it. I'm loading up an array of images, assigning one of them to a texture in LoadGallery.cs. Then in LoadImage I have a button that when clicked runs LoadImg but when I click the button nothing happens. The texture doesn't get assigned to the GameObject. I'm not getting any errors either. The script works fine when it's all within the IEnumerator function, but when I pull it out into a different script I'm getting nothing. What am I getting wrong?
LoadGallery.cs
public Texture2D tex;
// Use this for initialization
void Start () {
StartCoroutine("LoadAllImages");
}
IEnumerator LoadAllImages()
{
string[] filePaths = Directory.GetFiles(#"/Users/kenmarold/Screenshots/TATUS/", "*.png");
WWW www = new WWW("file://" + filePaths[0]);
yield return www;
Texture2D tex = new Texture2D(512,512);
www.LoadImageIntoTexture(tex);
}
LoadImage.cs
void LoadImg()
{
GameObject galleryLoader = GameObject.FindGameObjectWithTag("GalleryLoader");
LoadGallery loadGallery = galleryLoader.GetComponent<LoadGallery>();
Rect rct = new Rect(0, 0, loadGallery.tex.width, loadGallery.tex.height);
Vector2 pvt = new Vector2(0.5f, 0.5f);
GameObject screenShotImg = GameObject.FindGameObjectWithTag("screenshot");
Image img = screenShotImg.GetComponent<Image>();
img.sprite = Sprite.Create(loadGallery.tex, rct, pvt);
}
I could not see what's wrong with what you are doing. These are common mistakes from my experience:
1. You might have forgotten to assign the function LoadImg to the button.
2. The size of UI object "screenshot" is too small. Try assigning some dummy image to see if it's actually showing something.
3. There could have been some exception when loading gallery. Try to check for www.error. Debug it carefully.
Good luck with your work.

Categories

Resources