A native Unity plugin to take pictures/record videos with device camera on Android & iOS
A native Unity plugin to take pictures/record videos with device camera on Android & iOS
Available on Asset Store: https://assetstore.unity.com/packages/tools/integration/native-camera-for-android-ios-117802
Forum Thread: https://forum.unity.com/threads/native-camera-for-android-ios-open-source.529560/
Discord: https://discord.gg/UJJt549AaV
This plugin helps you take pictures/record videos natively with your device's camera on Android & iOS (other platforms aren't supported). It has built-in support for runtime permissions, as well.
There are 5 ways to install this plugin:
https://github.com/yasirkula/UnityNativeCamera.gitopenupm add com.yasirkula.nativecameraNativeCamera no longer requires any manual setup on Android. For reference, the legacy manual setup documentation is available at: https://github.com/yasirkula/UnityNativeCamera/wiki/Manual-Setup-for-Android
There are two ways to set up the plugin on iOS:
Please see: https://forum.unity.com/threads/native-camera-for-android-ios-open-source.529560/page-9#post-8207157
Only Android & iOS platforms are supported. Editor functionality is for preview purposes only and uses Unity's Editor-only API.
The plugin is using "Gradle version" 7.5.1 and "Android Gradle plug-in version" 7.4.2. Make sure your Unity version is compatible: https://docs.unity3d.com/2021.3/Documentation/Manual/android-gradle-overview.html
If you are sure that your plugin is up-to-date, then enable Custom Proguard File option from Player Settings and add the following line to that file: -keep class com.yasirkula.unity.* { *; }
Declare WRITE_EXTERNAL_STORAGE permission manually in your Plugins/Android/AndroidManifest.xml with the tools:node="replace" attribute as follows: ``.
NativeCamera.TakePicture( CameraCallback callback, int maxSize = -1, PreferredCamera preferredCamera = PreferredCamera.Default ): opens the camera and waits for user to take a picture.
NativeCamera.RecordVideo( CameraCallback callback, Quality quality = Quality.Default, int maxDuration = 0, long maxSizeBytes = 0L, PreferredCamera preferredCamera = PreferredCamera.Default ): opens the camera and waits for user to record a video.
NativeCamera.DeviceHasCamera(): returns false if the device doesn't have a camera. In this case, TakePicture and RecordVideo functions will not execute.
NativeCamera.IsCameraBusy(): returns true if the camera is currently open. In that case, another TakePicture or RecordVideo request will simply be ignored.
Note that TakePicture and RecordVideo functions automatically call NativeCamera.RequestPermissionAsync. More details available below.
Beginning with 6.0 Marshmallow, Android apps must request runtime permissions before accessing certain services, similar to iOS. There are two functions to handle permissions with this plugin:
bool NativeCamera.CheckPermission( bool isPicturePermission ): checks whether the app has access to camera or not.
void NativeCamera.RequestPermissionAsync( PermissionCallback callback, bool isPicturePermission ): requests permission to access the camera from the user and returns the result asynchronously. It is recommended to show a brief explanation before asking the permission so that user understands why the permission is needed and doesn't click Deny or worse, "Don't ask again". Note that TakePicture and RecordVideo functions call RequestPermissionAsync internally and execute only if the permission is granted.
NativeCamera.Permission permission parameterNativeCamera.Permission is an enum that can take 3 values:
Task NativeCamera.RequestPermissionAsync( bool isPicturePermission ): Task-based overload of RequestPermissionAsync.
NativeCamera.OpenSettings(): opens the settings for this app, from where the user can manually grant permission in case current permission state is Permission.Denied (Android requires Storage and, if declared in AndroidManifest, Camera permissions; iOS requires Camera permission).
NativeCamera.ImageProperties NativeCamera.GetImageProperties( string imagePath ): returns an ImageProperties instance that holds the width, height, mime type and EXIF orientation information of an image file without creating a Texture2D object. Mime type will be null, if it can't be determined.
NativeCamera.VideoProperties NativeCamera.GetVideoProperties( string videoPath ): returns a VideoProperties instance that holds the width, height, duration (in milliseconds) and rotation information of a video file. To play a video in correct orientation, you should rotate it by rotation degrees clockwise. For a 90-degree or 270-degree rotated video, values of width and height should be swapped to get the display size of the video.
Texture2D NativeCamera.LoadImageAtPath( string imagePath, int maxSize = -1, bool markTextureNonReadable = true, bool generateMipmaps = true, bool linearColorSpace = false ): creates a Texture2D from the specified image file in correct orientation and returns it. Returns null, if something goes wrong.
async Task NativeCamera.LoadImageAtPathAsync( string imagePath, int maxSize = -1, bool markTextureNonReadable = true ): asynchronous variant of LoadImageAtPath. 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.
Texture2D NativeCamera.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.
async Task NativeCamera.GetVideoThumbnailAsync( string videoPath, int maxSize = -1, double captureTimeInSeconds = -1.0, bool markTextureNonReadable = true ): asynchronous variant of GetVideoThumbnail. 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.
The following code has two functions:
…No open issues yet, or sync has not completed.