Add remote image as texture during loop - unity3d

I am very new to Unity3d.
I have a JSON array that contains the parameters of the prefabs I want to create at runtime.
I want to display images that are stored on my server in the scene.
I have a prefab "iAsset" that has a plane (mesh filter) and I want to load the image files as the texture of the plane.
I am able to Instatiate the prefabs however the prefab is showing up as a white square. This is my code:
for(var i = 0; i < bookData.Assets.Count; i++){
GameObject newAsset = null;
newAsset = (GameObject)Instantiate(iasset, new Vector3(2*i, 0, 0), Quaternion.identity);
if(!imageAssetRequested )
{
remoteImageAsset = new WWW(((BookAssets)bookData.Assets[i]).AssetContent);
imageAssetRequested = true;
}
if(remoteImageAsset.progress >=1)
{
if(remoteImageAsset.error == null)
{
loadingRemoteAsset = false;
newAsset.renderer.material.mainTexture = remoteImageAsset.texture;
}
}
}
the urls to the images on my server is retrieved from the JSON array:
((BookAssets)bookData.Assets[i]).AssetContent);
The code builds without any errors, I would very much appreciate any help to display the remote images.

You are not waiting for your downloads to complete.
The WWW class is asynchronous, and will commence the download. However, you need to either poll it (using the code you do have above) at a later time, or use a yield WWW in a CoRoutine that will block your execution (within that CoRoutine) until the download finishes (either successfully or due to failure).
Refer to Unity Documentation for WWW
Note however, that page sample code is wrong, and Start is not a CoRoutine / IEnumarator. Your code would look something like :
void Start()
{
... your code above ...
StartCoroutine(DownloadImage(bookData.Assets[i]).AssetContent, newAsset.renderer.material.mainTexture));
}
IEnumerator DownloadImage(string url, Texture tex)
{
WWW www = new WWW(url);
yield return www;
tex.LoadImage(www.bytes)
}

Related

Multiple Asset Bundle Download Cache Problem

I'm using UnityWebRequestAssetBundle.GetAssetBundle(url, bundleData.version, bundleData.crc); system and I can successfully download and use bundleAssets online. But when I want to download multiple bundle assets and save them to use offline for later I have a problem.
I have 2 files for example, 'A' and 'B'.
Case 1: When I download A and go offline I can load A anytime even I close the app. But when I want to download B and come back to A can't load A again because it deletes cache for some reason and tries to download it again.
Case 2: When I download A and B together and go offline, if I load B it loads. But if I try A it can't load and needs internet connection. After that when I try to load B again I loose package so it needs internet connection again.
So basicly I want to download multiple asset bundles and I want to use them whenever I want. How can I solve this problem? Thanks.
Code example:
using UnityWebRequest uwr = UnityWebRequestAssetBundle.GetAssetBundle(url, bundleData.version, bundleData.crc);
yield return uwr.SendWebRequest();
bundle = DownloadHandlerAssetBundle.GetContent(uwr);
Debug.Log(bundle);
if (bundle==null)
{
Debug.Log("COULDN'T CONNECT TO URL");
callback(null);
}
else
{
Debug.Log("FOUND AT SEARCH!");
// Get downloaded asset bundle
bundle = DownloadHandlerAssetBundle.GetContent(uwr);
Sprite sprite = isDiff ? bundle.LoadAsset<Sprite>(levelText + "Diff") : bundle.LoadAsset<Sprite>(levelText);
callback(sprite);
}
I solved my issue by saving images to persistentFolder.
I write image to disk first by Texture2D.EncodeToJPG();
private void writeImageOnDisk(Sprite sprite, string fileName)
{
Texture2D texture = DeCompress(sprite.texture);
byte[] textureBytes = texture.EncodeToJPG();
File.WriteAllBytes(Application.persistentDataPath + "/" + fileName+".jpg", textureBytes);
Debug.Log("File Written On Disk!");
}
To EncodeToJPG image must be Read/Write enabled before having AssetBundles and you should decompress them first, I found solution from this topic.
private Texture2D DeCompress(Texture2D source)
{
RenderTexture renderTex = RenderTexture.GetTemporary(
source.width,
source.height,
0,
RenderTextureFormat.Default,
RenderTextureReadWrite.Linear);
Graphics.Blit(source, renderTex);
RenderTexture previous = RenderTexture.active;
RenderTexture.active = renderTex;
Texture2D readableText = new Texture2D(source.width, source.height);
readableText.ReadPixels(new Rect(0, 0, renderTex.width, renderTex.height), 0, 0);
readableText.Apply();
RenderTexture.active = previous;
RenderTexture.ReleaseTemporary(renderTex);
return readableText;
}
And finally you can load image with this code:
private Sprite loadImageFromDisk(string fileName)
{
string dataPath = Application.persistentDataPath + "/" + fileName + ".jpg";
if (!File.Exists(dataPath))
{
Debug.LogError("File Does Not Exist!");
return null;
}
byte[] textureBytes = File.ReadAllBytes(dataPath);
Texture2D loadedTexture = new Texture2D(2048,2048,TextureFormat.ARGB32, false);
loadedTexture.LoadImage(textureBytes);
Sprite spr = Sprite.Create(loadedTexture, new Rect(0f, 0f, loadedTexture.width, loadedTexture.height), new Vector2(.5f,.5f), 2048); // 2048 is pixel per unit if you have a custom pixelPerUnit value you should set it here. Or you will see only some part of pixels of image.
spr.name = fileName;
return spr;
}
I hope this will work for you guys. This is how I store my images after downloading assetbundles and how I call them in game.

ARCore + Unity + Augmented Images - Load different prefabs for different Images

How do I assign different prefabs to different images?
right now, I have all my prefabs loading in on top of each other but how do I get it so each prefab loads in only once on top of one image so each image has a different prefab?
I've modified the sample code (the frame corners) to load in my own prefab and used a dictionary to pair the prefabs with images from the database but when the program runs it instatiates all the prefabs in the same place rather than putting one prefrab on each image it puts all the prefabs on every image - this is the code I've been using:
public GameObject obj1, obj2, obj3, obj4;
Dictionary<int, GameObject> imagePrefabPairs;
public AugmentedImage Image;
void Start()
{
instantiateDictionary();
}
// TODO: make initialisation dynamic, to match the size of the db.
public void instantiateDictionary()
{
imagePrefabPairs = new Dictionary<int, GameObject>
{
{ 0, obj1 },
{ 1, obj2 },
{ 2, obj3 },
{ 3, obj4 }
};
}
public void Update()
{
if (Image == null || Image.TrackingState != TrackingState.Tracking)
{
obj1.SetActive(false);
obj2.SetActive(false);
obj3.SetActive(false);
obj4.SetActive(false);
return;
}
Pose _centerPose = Image.CenterPose;
imagePrefabPairs[Image.DatabaseIndex].transform.localPosition = _centerPose.position;
imagePrefabPairs[Image.DatabaseIndex].transform.localScale = new Vector3(0.1f, 0.1f, 0.1f);
imagePrefabPairs[Image.DatabaseIndex].SetActive(true);
}
I figure that I need to have some kind of if statement to ask if one prefab is loaded in and then just choose to load in the next one and have them instantiate one at a time but I am not sure how to do that, or if there is a more direct way to make that happen...?
You could change your AugmentedImageVisualizer prefab, so that at the start all your different objects are inactive (.SetActive(false);) For changing a prefab you can add it to your hierarchy, set all the objects inactive and then apply the changes. After apply you can delete the prefab from your game hierarchy.
Img 1: Add existing prefab to game hierarchy
Img 2: set all the attached objecty to inactive (1) and apply the changes to the prefab (2)
So when you detect a image from your imagedatabase the AugmentedImageVisualizer prefab is attached and only the object with the given index is set to active. Then your code should work.
Because at the start all your objects are inactive you could change this part of your code:
if (Image == null || Image.TrackingState != TrackingState.Tracking)
{
obj1.SetActive(false);
obj2.SetActive(false);
obj3.SetActive(false);
obj4.SetActive(false);
return;
}
so that you only deactivate the active object:
if (Image.TrackingState != TrackingState.Tracking)
{
imagePrefabPairs[Image.DatabaseIndex].SetActive(false);
return;
}
I assign different prefabs to different images by this way:
I modified the AugmentedImageExampleController.cs:.
I added a list for prefabs:
public List<AugmentedImageVisualizer> prefabs = new List<AugmentedImageVisualizer>();
For the related image for the prefab I did a reference by using the image.DatabaseIndex in the visualizer:
visualizer = (AugmentedImageVisualizer)Instantiate(prefabs[image.DatabaseIndex], anchor.transform);
In the inspector of ExampleController you can put in the prefabs (AugmentedImageVisualizer) now.
That's it, and its working fine!

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'

Unity3d Issue NGUI UITexture/Facebook www Profile Pic

Having small issue here with Unity and NGUI. NGUI has UITexture as its main texture such as Unity has GUITexture.
I sent a request to facebook to get the users profile image which sends back a perfect url which if I put in the browser works fine.
My issue is taking that Texture2D (Facebook API does it as a Texture2D) and putting it on my UITexture. For some reason it just does not take it correctly. I keep getting a null value for it. I am also using Mobile Social as well from the asset store any help, helps.
Here is my snippet of code.
private void UserDataLoaded(FB_Result result)
{
SPFacebook.OnUserDataRequestCompleteAction -= UserDataLoaded;
if (result.IsSucceeded)
{
Debug.Log("User Data Loaded!");
IsUserInfoLoaded = true;
string FBNametxt = SPFacebook.Instance.userInfo.Name;
UILabel Name = GameObject.Find("FBName").GetComponent<UILabel>();
Name.text = FBNametxt;
Texture2D FBLoadTex = SPFacebook.Instance.userInfo.GetProfileImage(FB_ProfileImageSize.normal);
FBGetProfile.GetComponent<UITexture>().mainTexture = FBLoadTex != null ? FBLoadTex : PlaceHolderImg;
}
else {
Debug.Log("User data load failed, something was wrong");
}
}
The placeholder image is just a already selected image used if the fbprofile pic does is null. Which I keep getting.....
It's possible you're looking for RawImage which exist for this purpose
http://docs.unity3d.com/Manual/script-RawImage.html
Since the Raw Image does not require a sprite texture, you can use it to display any texture available to the Unity player. For example, you might show an image downloaded from a URL using the WWW class or a texture from an object in a game.
Use Unity.UI and use that feature.
Add "Ngui Unity2d Sprite" component instead of UiTexture to your profile picture in editor and use below code in your Fb callback
private void ProfilePicCallBack (FBResult result)
{
if (result.Error != null) {
Debug.Log ("Problem with getting Profile Picture");
return;
}
ProfileImage.GetComponent<UI2DSprite> ().sprite2D = Sprite.Create(result.Texture, new Rect(0,0,128,128), new Vector2(0,0));
}
there might be an another way to do this. but this worked for me

Load Scene in AssetBundle and Unload

I load a scene in my AssetBundle like this:
IEnumerator AsyncLoad ()
{
using (var www = new WWW("file://" + Application.dataPath + "/AssetBundles/scenes"))
{
yield return www;
var bundle = www.assetBundle;
Application.LoadLevel("Scene1");
bundle.Unload(false);
}
}
The problem is sometimes Application.LoadLevel("Scene1"); works fine and sometimes Scene1 is empty. What's wrong here?
Side note: I noticed LoadLevel destroys the current scene before loading, so the lines after LoadLevel are not being executed (since the script was destroyed). As I need to unload the asset bundle in order to use it again another time, what's the solution?
Unity 5
you want to use dontdestroyonload on the object that has this loading script attached
http://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Application.LoadLevel will postpone load sceneobjects next frame, so you need to postpone unload AssetBundles.
IEnumerator AsyncLoad (){
using (var www = new WWW("file://" + Application.dataPath + "/AssetBundles/scenes")){
yield return www;
var bundle = www.assetBundle;
Application.LoadLevel("Scene1");
yield return null; // THIS LINE IS ADDED
bundle.Unload(false);
}
}
This may work for you.