Hello,
my Code is giving me Memory leaks, each time I reload an Image 5-10 mb are gone.
I tried to set material.maintexture or material.SetTexture, it happens in both cases.
My Code to set the texture:
public void ShowImage()
{
{
try
{
ImagePlane.SetActive(true);
if (Globals.ExpertData.TexturePaths != null && !string.IsNullOrEmpty(Globals.ExpertData.TexturePaths[Globals.Step]))
{
ImagePlane.GetComponent<MeshRenderer>().material.SetTexture("_MainTex", Globals.LoadImage(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]));
//ImagePlane.GetComponent<MeshRenderer>().material.mainTexture = Globals.LoadImage(Path.Combine(Globals.DataPath, Globals.AssistantName) + "//" + Globals.ExpertData.TexturePaths[Globals.Step]);
}
}
Loading the Image from file:
public static Texture2D LoadImage(string fileName)
{
Debug.Log("Loading Image: " + fileName);
Texture2D tex = null;
byte[] fileData;
try
{
if (File.Exists(fileName))
{
fileData = File.ReadAllBytes(fileName);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData); //..this will auto-resize the texture dimensions.
return tex;
}
else
{
Debug.LogError("Image cannot be found: " + fileName);
return null;
}
}
catch (Exception ex)
{
Debug.LogError("Error loading image: " + ex.ToString());
return null;
}
}
Can anybody see, why I am losing Memory here? I opened the profiler and “Memory” there, each time I call this method Textures + 1 and + 5-10 mb…
thanks!