- Home /
using www to fill scrollarea
I am trying to find all the images on an android device and create thumbnails for them and then put them into a scroll area. I can find the paths to the images but when I create the thumbnails the app either gets really choppy or freezes completely until it's finished. What is the correct way to do this?
Here is what I've tried:
//this one freezes it for a while
Sprite newSprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
newThumbnail.GetComponent<Image>().sprite = newSprite;
And
//this one make is really choppy
Texture2D tex = new Texture2D(www.texture.width, www.texture.height);
www.LoadImageIntoTexture(tex);
newThumbnail.GetComponent<RawImage>().texture = tex;
What do you mean by all images? I can only see one image here www.texture
. Where are the rest? How to you get and store them? This code looks alright. The problem must be somewhere else.
IEnumerator GetAllGalleryImagePaths()
{
HashSet<string> allowedExtesions = new HashSet<string>() { ".png", ".jpg", ".jpeg"};
debugText.text = "starting coroutine";
#if UNITY_ANDROID
AndroidJavaClass mediaClass = new AndroidJavaClass("android.provider.$$anonymous$$ediaStore$Images$$$anonymous$$edia");
// Set the tags for the data we want about each image. This should really be done by calling;
//string dataTag = mediaClass.GetStatic<string>("DATA");
// but I couldn't get that to work...
const string dataTag = "_data";
string[] projection = new string[] { dataTag };
AndroidJavaClass player = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = player.GetStatic<AndroidJavaObject>("currentActivity");
string[] urisToSearch = new string[] { "EXTERNAL_CONTENT_URI", "INTERNAL_CONTENT_URI" };
foreach (string uriToSearch in urisToSearch)
{
AndroidJavaObject externalUri = mediaClass.GetStatic<AndroidJavaObject>(uriToSearch);
AndroidJavaObject finder = currentActivity.Call<AndroidJavaObject>("managedQuery", externalUri, projection, null, null, null);
bool foundOne = finder.Call<bool>("moveToFirst");
while (foundOne)
{
yield return null;
int dataIndex = finder.Call<int>("getColumnIndex", dataTag);
string data = finder.Call<string>("getString", dataIndex);
if (allowedExtesions.Contains(Path.GetExtension(data).ToLower()))
{
string path = @"file:///" + data;
StartCoroutine(CreateImage(path));
}
foundOne = finder.Call<bool>("moveToNext");
}
}
#endif
}
IEnumerator CreateImage(string path)
{
WWW www = new WWW(path);
yield return www;
//Sprite newSprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), new Vector2(0, 0));
GameObject newThumbnail2 = Instantiate(videoThumbnailPrefab) as GameObject;
Texture2D tex = new Texture2D(www.texture.width, www.texture.height);
www.LoadImageIntoTexture(tex);
newThumbnail2.GetComponent<RawImage>().texture = tex;
newThumbnail2.transform.parent = imageContentHolder;
newThumbnail2.transform.localScale = Vector3.one;
newThumbnail2.transform.position = imageContentHolder.transform.position;
}
There is all my code and it's working fine. The issue is that creating the sprite or texture2D is very slow. If I create sprites it freezes the app for about .25 seconds per image. If I create texture2Ds it doesn't completely freeze but it gets really choppy. It's a Gear VR app so I need to avoid the choppiness as much as possible.
Your answer
Follow this Question
Related Questions
UI Sprite very pixelated 1 Answer
Mask a texture onto sprite? 0 Answers
Created Material Shows Dark Sprite 1 Answer
Sprite rendering texture as one solid color 1 Answer
Bad quality of the sprite in Unity 0 Answers