- Home /
Getting Multiple images using File.ReadAllBytes take a long time and Higher Cpu Usage..?
public void Run()
{
StartCoroutine (Lo (l));
}
IEnumerator Lo(int u)
{
int k = PlayerPrefs.GetInt ("Car" + (u + 1) + "LastSelectedMaterial");
if (k == 0)
{
if (u == 0)
{
k = 1;
}
else if (u == 1)
{
k = 6;
}
else if (u == 2)
{
k = 11;
}
else if (u == 3)
{
k = 16;
}
}
if (u == 0)
{
if (File.Exists (Application.persistentDataPath + "car" + k + ".jpg"))
{
byte[] byteArray = File.ReadAllBytes (Application.persistentDataPath + "car" + k + ".jpg");
Texture2D texture = new Texture2D (1, 1);
// byte[] bytes = texture.EncodeToJPG ();
texture.LoadImage (byteArray);
Material[] a1;
a1 = cars [u].transform.GetChild (0).GetComponent<MeshRenderer> ().sharedMaterials;
Destroy (a1 [1].mainTexture);
Destroy (a1 [0].mainTexture);
System.GC.Collect ();
a1 [1].mainTexture = texture;
a1 [0].mainTexture = texture;
cars [u].transform.GetChild (0).GetComponent<MeshRenderer> ().sharedMaterials = a1;
Destroy (cars [u].transform.GetChild (1).GetComponent<MeshRenderer> ().sharedMaterial.mainTexture);
System.GC.Collect ();
cars [u].transform.GetChild (1).GetComponent<MeshRenderer> ().sharedMaterial.mainTexture = texture;
}
}
else
{
if (File.Exists (Application.persistentDataPath + "car" + k + ".jpg"))
{
byte[] byteArray = File.ReadAllBytes (Application.persistentDataPath + "car" + k + ".jpg");
Texture2D texture = new Texture2D (8, 8);
texture.LoadImage (byteArray);
Material[] a1;
a1 = cars [u].transform.GetChild (0).GetComponent<MeshRenderer> ().sharedMaterials;
Destroy (a1 [0].mainTexture);
Destroy (a1 [1].mainTexture);
System.GC.Collect ();
a1 [0].mainTexture = texture;
a1 [1].mainTexture = texture;
cars [u].transform.GetChild (0).GetComponent<MeshRenderer> ().sharedMaterials = a1;
Destroy (cars [u].transform.GetChild (1).GetComponent<MeshRenderer> ().sharedMaterial.mainTexture);
System.GC.Collect ();
cars [u].transform.GetChild (1).GetComponent<MeshRenderer> ().sharedMaterial.mainTexture = texture;
}
}
u += 1;
yield return new WaitForSeconds (0.2f);
StartCoroutine (Lo (u));
}
Comment
Your answer
Follow this Question
Related Questions
Search in a text file 1 Answer
Should i use yield everytime? 1 Answer
UnauthorizedAccessException: Access to the path is denied 4 Answers
How Do I Find Path of a File? 1 Answer
Alt codes and TextMeshes, does it work? 2 Answers