Skip to content

Commit 095ca3f

Browse files
committed
Fixed crash when using LoadImageAtPathAsync on newer Unity versions (fixed #302)
1 parent 4516d91 commit 095ca3f

File tree

2 files changed

+16
-57
lines changed

2 files changed

+16
-57
lines changed

.github/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ There are two functions to handle permissions with this plugin:
154154
- **generateMipmaps** determines whether texture should have mipmaps or not
155155
- **linearColorSpace** determines whether texture should be in linear color space or sRGB color space
156156

157-
`async Task<Texture2D> NativeGallery.LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )`: asynchronous variant of *LoadImageAtPath* (requires Unity 2018.4 or later). Works best when *linearColorSpace* is *false*. It's also slightly faster when *generateMipmaps* is *false*. Note that it isn't possible to load multiple images simultaneously using this function.
157+
`async Task<Texture2D> NativeGallery.LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true )`: asynchronous variant of *LoadImageAtPath* (requires Unity 2018.4 or later). Whether or not the returned Texture2D has mipmaps enabled depends on *UnityWebRequestTexture*'s implementation on the target Unity version. Note that it isn't possible to load multiple images simultaneously using this function.
158158

159159
`Texture2D NativeGallery.GetVideoThumbnail( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )`: creates a Texture2D thumbnail from a video file and returns it. Returns *null*, if something goes wrong.
160160
- **maxSize** determines the maximum size of the returned Texture2D in pixels. Larger thumbnails will be down-scaled. If untouched, its value will be set to *SystemInfo.maxTextureSize*. It is recommended to set a proper maxSize for better performance
161161
- **captureTimeInSeconds** determines the frame of the video that the thumbnail is captured from. If untouched, OS will decide this value
162162
- **markTextureNonReadable** (see *LoadImageAtPath*)
163163

164-
`async Task<Texture2D> NativeGallery.GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )`: asynchronous variant of *GetVideoThumbnail* (requires Unity 2018.4 or later). Works best when *linearColorSpace* is *false*. It's also slightly faster when *generateMipmaps* is *false*. Note that it isn't possible to generate multiple video thumbnails simultaneously using this function.
164+
`async Task<Texture2D> NativeGallery.GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true )`: asynchronous variant of *GetVideoThumbnail* (requires Unity 2018.4 or later). Whether or not the returned Texture2D has mipmaps enabled depends on *UnityWebRequestTexture*'s implementation on the target Unity version. Note that it isn't possible to generate multiple video thumbnails simultaneously using this function.
165165

166166
## EXAMPLE CODE
167167

Plugins/NativeGallery/NativeGallery.cs

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -823,7 +823,7 @@ public static Texture2D LoadImageAtPath( string imagePath, int maxSize = -1, boo
823823
}
824824

825825
#if UNITY_2018_4_OR_NEWER && !NATIVE_GALLERY_DISABLE_ASYNC_FUNCTIONS
826-
public static async Task<Texture2D> LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )
826+
public static async Task<Texture2D> LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true )
827827
{
828828
if( string.IsNullOrEmpty( imagePath ) )
829829
throw new ArgumentException( "Parameter 'imagePath' is null or empty!" );
@@ -846,71 +846,30 @@ public static async Task<Texture2D> LoadImageAtPathAsync( string imagePath, int
846846

847847
Texture2D result = null;
848848

849-
if( !linearColorSpace )
849+
using( UnityWebRequest www = UnityWebRequestTexture.GetTexture( "file://" + loadPath, markTextureNonReadable ) )
850850
{
851-
using( UnityWebRequest www = UnityWebRequestTexture.GetTexture( "file://" + loadPath, markTextureNonReadable && !generateMipmaps ) )
852-
{
853-
UnityWebRequestAsyncOperation asyncOperation = www.SendWebRequest();
854-
while( !asyncOperation.isDone )
855-
await Task.Yield();
851+
UnityWebRequestAsyncOperation asyncOperation = www.SendWebRequest();
852+
while( !asyncOperation.isDone )
853+
await Task.Yield();
856854

857855
#if UNITY_2020_1_OR_NEWER
858-
if( www.result != UnityWebRequest.Result.Success )
859-
#else
860-
if( www.isNetworkError || www.isHttpError )
861-
#endif
862-
{
863-
Debug.LogWarning( "Couldn't use UnityWebRequest to load image, falling back to LoadImage: " + www.error );
864-
}
865-
else
866-
{
867-
Texture2D texture = DownloadHandlerTexture.GetContent( www );
868-
869-
if( !generateMipmaps )
870-
result = texture;
871-
else
872-
{
873-
Texture2D mipmapTexture = null;
874-
try
875-
{
876-
// Generate a Texture with mipmaps enabled
877-
// Credits: https://forum.unity.com/threads/generate-mipmaps-at-runtime-for-a-texture-loaded-with-unitywebrequest.644842/#post-7571809
878-
NativeArray<byte> textureData = texture.GetRawTextureData<byte>();
879-
880-
mipmapTexture = new Texture2D( texture.width, texture.height, texture.format, true );
881-
#if UNITY_2019_3_OR_NEWER
882-
mipmapTexture.SetPixelData( textureData, 0 );
856+
if( www.result != UnityWebRequest.Result.Success )
883857
#else
884-
NativeArray<byte> mipmapTextureData = mipmapTexture.GetRawTextureData<byte>();
885-
NativeArray<byte>.Copy( textureData, mipmapTextureData, textureData.Length );
886-
mipmapTexture.LoadRawTextureData( mipmapTextureData );
858+
if( www.isNetworkError || www.isHttpError )
887859
#endif
888-
mipmapTexture.Apply( true, markTextureNonReadable );
889-
890-
result = mipmapTexture;
891-
}
892-
catch( Exception e )
893-
{
894-
Debug.LogException( e );
895-
896-
if( mipmapTexture )
897-
Object.DestroyImmediate( mipmapTexture );
898-
}
899-
finally
900-
{
901-
Object.DestroyImmediate( texture );
902-
}
903-
}
904-
}
860+
{
861+
Debug.LogWarning( "Couldn't use UnityWebRequest to load image, falling back to LoadImage: " + www.error );
905862
}
863+
else
864+
result = DownloadHandlerTexture.GetContent( www );
906865
}
907866

908867
if( !result ) // Fallback to Texture2D.LoadImage if something goes wrong
909868
{
910869
string extension = Path.GetExtension( imagePath ).ToLowerInvariant();
911870
TextureFormat format = ( extension == ".jpg" || extension == ".jpeg" ) ? TextureFormat.RGB24 : TextureFormat.RGBA32;
912871

913-
result = new Texture2D( 2, 2, format, generateMipmaps, linearColorSpace );
872+
result = new Texture2D( 2, 2, format, true, false );
914873

915874
try
916875
{
@@ -966,7 +925,7 @@ public static Texture2D GetVideoThumbnail( string videoPath, int maxSize = -1, d
966925
}
967926

968927
#if UNITY_2018_4_OR_NEWER && !NATIVE_GALLERY_DISABLE_ASYNC_FUNCTIONS
969-
public static async Task<Texture2D> GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false )
928+
public static async Task<Texture2D> GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true )
970929
{
971930
if( maxSize <= 0 )
972931
maxSize = SystemInfo.maxTextureSize;
@@ -982,7 +941,7 @@ public static async Task<Texture2D> GetVideoThumbnailAsync( string videoPath, in
982941
#endif
983942

984943
if( !string.IsNullOrEmpty( thumbnailPath ) )
985-
return await LoadImageAtPathAsync( thumbnailPath, maxSize, markTextureNonReadable, generateMipmaps, linearColorSpace );
944+
return await LoadImageAtPathAsync( thumbnailPath, maxSize, markTextureNonReadable );
986945
else
987946
return null;
988947
}

0 commit comments

Comments
 (0)