Taking a picture with the Camera plugin with Unity
We’ve now connected Unity with Google to provide us maps for our insurance app. Now we’ll use Unity to take a photo with our mobile device! Usually, there’s a lot of heavy code involved, but thankfully there’s an open-source plugin called Native Camera
Go to AssetStore.Unity.Com and search for Native Camera. It won’t be the first thing listed so scroll down a bit. It should be on the second row. Then select and install it and you’re good to go, but there’s one more step to take.
After enabling the camera from the store, you’ll have to install it from the package manager, just select ‘My Assets’ instead of Unity registry and then search for ‘native’, and it should immediately appear. Then select download and then install and then you’ll be ready to take the next step.
There should be a new plugins directory in the assets folder with a NativeCamera folder inside. We’ll look at the README file to see how to access its scripting API. On older versions of this plugin would require you to set write permissions on the sd card as well as create a manifest file for Android but thankfully setup is all automatic now.
For the code part, we’ll use the github example as our template and we’ll copy the TakePhoto function and paste it right into our TakePhotoPanell class!
The first few lines take the picture, we can get rid of the rest.
When the camera takes a picture, it will save it into a holder called texture. We’ll just copy what’s in texture into our UI holder image and turn it on. We need to turn off the original take photo button, since we didn’t create a variable holder for it, we’ll have to use the GameObject.Find method to hook it in. It’s not a processing intensive app so we can be a little loose.
We’re almost done. We just need to create a function we can hook into our take photo button.
There’s a current minor issue of out take photo button not being a button but an image. We can fix that by adding the button component, and we’ll hook it up to the TakePicture function while we’re at it.
For the next button, we’ll want to copy our photo into the active case before moving on to the next screen.
And with this, we have a working camera in Unity!