I have this code for saving and loading a texture2D (that you can modify in my game). When I load the texture although it does load it correctly (with all the modifications to the texture) after I try to modify it again in the game, it doesn’t update visually like before the load (it does work under the hood because when I save and load again, the new loaded texture is displayed with the modifications I couldn’t see in the game after the first save and load).
Has anyone had this issue before? (I assume it’s a unity material update thing)
Here is the code for saving and loading the texture:
// Convert Texture2D to Base64 string
private static string TextureToBase64(Texture2D texture)
{
byte[] textureBytes = texture.EncodeToPNG(); // or EncodeToJPG
return System.Convert.ToBase64String(textureBytes);
}
// Convert Base64 string back to Texture2D
public static Texture2D Base64ToTexture(string base64)
{
byte[] textureBytes = System.Convert.FromBase64String(base64);
Texture2D texture = new Texture2D(2, 2); // Size will be overwritten by LoadImage
texture.LoadImage(textureBytes);
texture.Apply();
return texture;
}
Here is the code for applying the texture to the material after the load:
private Texture2D CloneTexture(Texture2D originalTexture, Renderer renderer)
{
Texture2D dirtMaskTextureClone = new Texture2D(originalTexture.width, originalTexture.height);
dirtMaskTextureClone.SetPixels32(originalTexture.GetPixels32());
dirtMaskTextureClone.Apply();
Debug.Log($"The redability is: {originalTexture.isReadable}");
renderer.material.SetTexture("_Mask", dirtMaskTextureClone);
return dirtMaskTextureClone;
}
Thanks for anyone who can help me!