Skip to content
This repository was archived by the owner on Dec 5, 2024. It is now read-only.

Commit ce46d04

Browse files
committed
Fix 2017.x support
Unity 2017 has broken ABI compat on Texture2D.LoadImage by moving the method from the Texture2D type to a separate class as an extension method. Sigh.
1 parent bb0d6f1 commit ce46d04

File tree

1 file changed

+30
-1
lines changed
  • src/UnityExtension/Assets/Editor/GitHub.Unity/Misc

1 file changed

+30
-1
lines changed

src/UnityExtension/Assets/Editor/GitHub.Unity/Misc/Utility.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Linq;
45
using System.Reflection;
56
using System.Text.RegularExpressions;
67
using UnityEditor;
@@ -151,6 +152,34 @@ public static Texture2D GetTextureFromColor(Color color)
151152

152153
static class StreamExtensions
153154
{
155+
private static MethodInfo loadImage;
156+
private static Func<Texture2D, MemoryStream, Texture2D> invokeLoadImage;
157+
158+
static StreamExtensions()
159+
{
160+
var t = typeof(Texture2D).Assembly.GetType("UnityEngine.ImageConversion", false, false);
161+
if (t != null)
162+
{
163+
// looking for ImageConversion.LoadImage(this Texture2D tex, byte[] data)
164+
loadImage = t.GetMethods().FirstOrDefault(x => x.Name == "LoadImage" && x.GetParameters().Length == 2);
165+
invokeLoadImage = (tex, ms) =>
166+
{
167+
loadImage.Invoke(null, new object[] { tex, ms.ToArray() });
168+
return tex;
169+
};
170+
}
171+
else
172+
{
173+
// looking for Texture2D.LoadImage(byte[] data)
174+
loadImage = typeof(Texture2D).GetMethods().FirstOrDefault(x => x.Name == "LoadImage" && x.GetParameters().Length == 1);
175+
invokeLoadImage = (tex, ms) =>
176+
{
177+
loadImage.Invoke(tex, new object[] { ms.ToArray() });
178+
return tex;
179+
};
180+
}
181+
}
182+
154183
public static Texture2D ToTexture2D(this Stream input)
155184
{
156185
var buffer = new byte[16 * 1024];
@@ -163,7 +192,7 @@ public static Texture2D ToTexture2D(this Stream input)
163192
}
164193

165194
var tex = new Texture2D(1, 1);
166-
tex.LoadImage(ms.ToArray());
195+
tex = invokeLoadImage(tex, ms);
167196
return tex;
168197
}
169198
}

0 commit comments

Comments
 (0)