- Home /
Open/Start Device Native Camera in Unity3D app using C# script, Without 3rd Party Plugins
Recently I started learning Unity3D with iOS and Android Platforms. and gone through bunch of resources to get basics of Unity3D and scripting language. Example : **Unity3D Beginner Scripting**
but still I am not able to start/open iPhone/Android device camera using C# script in my Unity3D application. everytime I found regarding Game object camera not device camera.
Can anyone please guide me in this, what is the Class name for Device Camera or How to initiate.
my C# script file is (OpenCamera.cs) :
using UnityEngine;
using System.Collections;
public class OpenCamera : MonoBehaviour {
// Use this for initialization
void Start ()
{
Debug.Log ("Start function get called");
//I want to start camera here
}
// Update is called once per frame
void Update () {
}
}
if anyone want to do this by using plugin, then go for this plugin
http://u3d.as/content/devesh-pandey/camera-shot
Or more info http://unitydevelopers.blogspot.in/p/blog-page_13.html
hai i want to use ipad camera in my application and followed the steps as u suggested above but not able to launch camera...as soon as application is launched it is giving a pop up saying do u want to allow device cam..on clicking ok the popup disappeared but cam is not launched..do we need add any plugin..please help me on this thanks, sudha
Answer by RDC · Jun 17, 2014 at 11:48 AM
After some more digging on Google and Official docs. I got solution which I am going to share with you , It help someone .. someday..
1.Create New Project.
2.Select Main Camera in GameObject and change Transform via Inspector
Position X= -90 Y=785 Z=0 Rotation X=90 Y=90 Z=0 Scale X=1 Y=1 Z=1
3.Now go to GameObject — > Create Other — > Plane.
4.Select Main Camera in GameObject and
4.1change Transform via Inspector
Position X=0 Y=0 Z=0 Rotation X=0 Y=0 Z=0 Scale X=100 Y=100 Z=100
4.2change Tag=Player
Now create a c# script with name “CameraController” and replace the code with below one
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public WebCamTexture mCamera = null;
public GameObject plane;
// Use this for initialization
void Start ()
{
Debug.Log ("Script has been started");
plane = GameObject.FindWithTag ("Player");
mCamera = new WebCamTexture ();
plane.renderer.material.mainTexture = mCamera;
mCamera.Play ();
}
// Update is called once per frame
void Update ()
{
}
}
5.Finally save and Drag this Script file onto “Plane” GameObject
Note - you may see preview rotated in Unity Game view but on RealDevice it works well. tested on iPhone5 and Android - Nexus 5.
Here is the snap shot how it comes if you change rotation angle to 180:
Thank you for this, this is great. I have a question I was hoping you could answer. I am trying to use what you wrote in C# and expand a little. I want the camera to scan words and bring my information bases on a database offline? Any idea where I could start, I am new to unity and still learning.
@joerty You'd have to use an AR extension to recognize the words and then work on your application. I would suggest Vuforia.
Thank , But it is rendering opposite in case of LandscapeLeft or LandscapeRight. in one orientation it is working fine but different orientation it is co$$anonymous$$g opposite.
Brilliant! Thanks bro! ;D
If it's upside down for you, just scale the plane -1 in the x-direction or the corresponding dir - it will look upside down for an image in the editor (before running) but will come out correctly on the iPhone.
Also, make sure it isn't allowed to rotate -otherwise the output will be incorrect half the time. If you want to allow a rotate then you'll need a bit of extra code to handle that case.
Answer by Jacruz · Jan 04, 2016 at 02:24 PM
thanks! In Unity 5, change the line:
plane.renderer.material.mainTexture = mCamera;
replace for:
plane.GetComponent<Renderer>().material.mainTexture = mCamera;
Answer by branek42 · Aug 02, 2018 at 09:39 AM
Hello how switch back camera on front camera? help me pls
Hello branek42, I am just starting to review information about Unity and I found a tutorial that helps using the backCamera as you requested. https://www.youtube.com/watch?v=c6NXkZWXHnc
I hope it helps.
Regards Juan
WebCamDevice[] devices = WebCamTexture.devices;
for(int i =0; i
WebCamDevice[] devices = WebCamTexture.devices;
for(int i =0; i
Your answer
Follow this Question
Related Questions
2D Camera (Android, Iphone) 1 Answer
Accessing the iPhone or Android camera for screenshot with overlay 3 Answers
Setup for iPhone portrait game 0 Answers
Disabling all Cameras Android/Iphone 1 Answer