11using System ;
22using System . Collections . Generic ;
33using System . IO ;
4+ using System . Linq ;
45using System . Reflection ;
56using System . Text . RegularExpressions ;
67using 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