Unity拉取相册功能

Unity拉取相册功能


最近在做一款手游的时候,需求是要从手机相册中选区相片,然后上传到服务器。

最后找到了一款插件非常好用,在这里推荐给大家——NativeGallery,在Unity的Assets Store中也可以搜索的到,资源是免费的。


附上调用插件的外部方法:

    //此方法是外部调用插件内部的方法
    public void TakePhoto(int maxSize = -1)
    {
        //调用插件自带接口,拉取相册,内部有区分平台
        NativeGallery.Permission permission = NativeGallery.GetImageFromGallery((path) =>
        {
            Debug.Log("Image path: " + path);
            if (path != null)
            {
                // 此Action为选取图片后的回调,返回一个Texture2D 
                Texture2D texture = NativeGallery.LoadImageAtPath(path, maxSize);
                if (texture == null)
                {
                    Debug.Log("Couldn't load texture from " + path);
                    return;
                }
                Debug.Log(texture.name);
                StartCoroutine(setRoleImage(texture));
            }
        }, "选择图片", "image/png", maxSize);

        Debug.Log("Permission result: " + permission);
    }

这里是插件的内部方法:

private static Permission GetMediaFromGallery( MediaPickCallback callback, bool imageMode, string mime, string title, int maxSize )
    {
        Debug.Log("GetMediaFromGallery_1" + "_" + callback + "_" + imageMode + "_" + mime + "_" + title + "_" + maxSize);
        Permission result = RequestPermission();
        if( result == Permission.Granted && !IsMediaPickerBusy() )
        {
#if !UNITY_EDITOR && UNITY_ANDROID
            object threadLock = new object();
            lock( threadLock )
            {
                NGMediaReceiveCallbackAndroid nativeCallback = new NGMediaReceiveCallbackAndroid( threadLock );

                Debug.Log("GetMediaFromGallery_2" + "_" + nativeCallback);

                AJC.CallStatic( "PickMedia", Context, nativeCallback, imageMode, false, mime, title );

                if( string.IsNullOrEmpty( nativeCallback.Path ) )
                    System.Threading.Monitor.Wait( threadLock );

                string path = nativeCallback.Path;
                if( string.IsNullOrEmpty( path ) )
                    path = null;

                if( callback != null )
                    callback( path );
            }
#elif !UNITY_EDITOR && UNITY_IOS
            NGMediaReceiveCallbackiOS.Initialize( callback );
            if( imageMode )
            {
                if( maxSize <= 0 )
                    maxSize = SystemInfo.maxTextureSize;

                _NativeGallery_PickImage( IOSSelectedImagePath, maxSize );
            }
            else
                _NativeGallery_PickVideo();
#else
            if ( callback != null )
                callback( null );
#endif
        }

        Debug.Log("GetMediaFromGallery_3" + "_" + result);

        return result;
    }

此插件不仅仅可以使用拉取相册的功能,还可以保存图片到相册,拉取相机权限等。
最后附上作者的GitHub地址:https://github.com/yasirkula/UnityNativeGallery
以及网盘地址:链接:https://pan.baidu.com/s/1y6hH2xfX844DENbqCwUE9g 密码:s4ij


补充一个坑爹的地方,就是获取的这个Texture2D是不可读的,假如你需要上传到服务器的话,会报错:Texture is not readable,the Texture memory can not be accessed form scripts。
这个可以看我的下一篇文章:
《如何通过脚本使Texture2D可读》https://blog.csdn.net/qq_39776199/article/details/81506293

猜你喜欢

转载自blog.csdn.net/qq_39776199/article/details/81505802