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.git
openupm add com.yasirkula.nativecamera
NativeCamera 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.
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: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:node="replace"/>
.
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.Permission> 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<Texture2D> 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<Texture2D> 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:
void Update() { if( Input.GetMouseButtonDown( 0 ) ) { // Don't attempt to use the camera if it is already open if( NativeCamera.IsCameraBusy() ) return; if( Input.mousePosition.x < Screen.width / 2 ) { // Take a picture with the camera // If the captured image's width and/or height is greater than 512px, down-scale it TakePicture( 512 ); } else { // Record a video with the camera RecordVideo(); } } } private void TakePicture( int maxSize ) { NativeCamera.TakePicture( ( path ) => { Debug.Log( "Image path: " + path ); if( path != null ) { // Create a Texture2D from the captured image Texture2D texture = NativeCamera.LoadImageAtPath( path, maxSize ); if( texture == null ) { Debug.Log( "Couldn't load texture from " + path ); return; } // Assign texture to a temporary quad and destroy it after 5 seconds GameObject quad = GameObject.CreatePrimitive( PrimitiveType.Quad ); quad.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 2.5f; quad.transform.forward = Camera.main.transform.forward; quad.transform.localScale = new Vector3( 1f, texture.height / (float) texture.width, 1f ); Material material = quad.GetComponent<Renderer>().material; if( !material.shader.isSupported ) // happens when Standard shader is not included in the build material.shader = Shader.Find( "Legacy Shaders/Diffuse" ); material.mainTexture = texture; Destroy( quad, 5f ); // If a procedural texture is not destroyed manually, // it will only be freed after a scene change Destroy( texture, 5f ); } }, maxSize ); } private void RecordVideo() { NativeCamera.RecordVideo( ( path ) => { Debug.Log( "Video path: " + path ); if( path != null ) { // Play the recorded video Handheld.PlayFullScreenMovie( "file://" + path ); } } ); }
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4