1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . IO ;
4
+ using System . Linq ;
4
5
using System . Reflection ;
5
6
using System . Text . RegularExpressions ;
6
7
using UnityEditor ;
@@ -151,6 +152,34 @@ public static Texture2D GetTextureFromColor(Color color)
151
152
152
153
static class StreamExtensions
153
154
{
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
+
154
183
public static Texture2D ToTexture2D ( this Stream input )
155
184
{
156
185
var buffer = new byte [ 16 * 1024 ] ;
@@ -163,7 +192,7 @@ public static Texture2D ToTexture2D(this Stream input)
163
192
}
164
193
165
194
var tex = new Texture2D ( 1 , 1 ) ;
166
- tex . LoadImage ( ms . ToArray ( ) ) ;
195
+ tex = invokeLoadImage ( tex , ms ) ;
167
196
return tex ;
168
197
}
169
198
}
0 commit comments