From 3b4ea31b5069b07799f9d760f004f7b44a38f13e Mon Sep 17 00:00:00 2001 From: sinaioutlander <49360850+sinaioutlander@users.noreply.github.com> Date: Thu, 24 Dec 2020 18:10:17 +1100 Subject: [PATCH] Fix Texture2D saver in Mono and for non-readable textures --- src/ExplorerCore.cs | 2 +- src/Helpers/Texture2DHelpers.cs | 10 ++++++++++ src/Inspectors/Reflection/InstanceInspector.cs | 9 ++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ExplorerCore.cs b/src/ExplorerCore.cs index 4194b97..b8f26c2 100644 --- a/src/ExplorerCore.cs +++ b/src/ExplorerCore.cs @@ -16,7 +16,7 @@ namespace UnityExplorer public class ExplorerCore { public const string NAME = "UnityExplorer"; - public const string VERSION = "3.1.2"; + public const string VERSION = "3.1.3"; public const string AUTHOR = "Sinai"; public const string GUID = "com.sinai.unityexplorer"; public const string EXPLORER_FOLDER = @"Mods\UnityExplorer"; diff --git a/src/Helpers/Texture2DHelpers.cs b/src/Helpers/Texture2DHelpers.cs index 5679d44..645ef47 100644 --- a/src/Helpers/Texture2DHelpers.cs +++ b/src/Helpers/Texture2DHelpers.cs @@ -15,6 +15,16 @@ namespace UnityExplorer.Helpers private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod(); private static MethodInfo m_encodeToPNGMethod; + public static byte[] EncodeToPNGSafe(this Texture2D tex) + { + var method = EncodeToPNGMethod; + + if (method.IsStatic) + return (byte[])method.Invoke(null, new object[] { tex }); + else + return (byte[])method.Invoke(tex, new object[0]); + } + private static MethodInfo GetEncodeToPNGMethod() { if (ReflectionHelpers.GetTypeByName("UnityEngine.ImageConversion") is Type imageConversion) diff --git a/src/Inspectors/Reflection/InstanceInspector.cs b/src/Inspectors/Reflection/InstanceInspector.cs index d58e4cc..9de3ae7 100644 --- a/src/Inspectors/Reflection/InstanceInspector.cs +++ b/src/Inspectors/Reflection/InstanceInspector.cs @@ -272,7 +272,14 @@ namespace UnityExplorer.Inspectors.Reflection if (File.Exists(path)) File.Delete(path); - var data = tex.EncodeToPNG(); + if (!tex.IsReadable()) + tex = Texture2DHelpers.ForceReadTexture(tex); +#if CPP + byte[] data = tex.EncodeToPNG(); +#else + byte[] data = tex.EncodeToPNGSafe(); +#endif + File.WriteAllBytes(path, data); } });