Augmented Reality has minimizes the distances of imaginary and real world. In this tutorial you will learn to Display YouTube Video on an image target. The video below will help you for better understanding of this blog.
For better understanding make sure to read the previous blogs i.e. DISPLAY 3D MODEL ON IMAGE TARGET USING VUFORIA IN UNITY3D and DISPLAY VIDEO ON AN IMAGE TARGET USING VUFORIA IN UNITY 3D.
Create a new unity project:
In augmented reality creating a new unity project requires some of the things to be done. For creating a new unity project you need to write project name and then click on create project.
Adding new image:
For adding a new image target you need to go to vuforia engine. After going to vuforia engine click on image and re position it. Now go to vuforia portal. For opening vuforia developer portal make sure that you are sign in. Click on develop and after that go to click on target manager. For adding the target click on single image. Now choose the image by clicking on the browse button, then add certain width and rename it. Then click on add.
Now we have to set scene for augmented reality. For that you have to delete main camera and add AR camera and image. As you can see this warning sign, go to build settings then go to player settings. Go to the bottom click on XR settings and check the vuforia augmented reality.
Downloading the database:
Now download that data base. Click on unity editor then click on download. When it is downloaded drag it to the unity timeline put it in assets. Make sure that all the items are selected and click on import. Go to image target and select your database. Now you can see your image has loaded.
YouTube video plugin:
Now what we have to do is to display YouTube video on an image target. For that we need a YouTube video plugin. The link of YouTube video plugin is available here. You can buy YouTube video plugin from it.
Importing the YouTube video plugin:
Drag the YouTube video plugin in unity and import it. Now click on console and click on clear. You can see an error here and double click on it.
Change the “LOW” word into “STANDARD”. Now press Ctrl S to save. We need to go back to unity. Now in Assets. Click on the plugin folder “Light Shaft” and go to resources.
In the resources go to prefabs and then click on YouTube player. Now crop it in image target.
Settings positions and rotations:
We need to set its positions and rotations. All x,y and z axis are set as 0 in the rotations and positions. Now click in YouTube player and then click on Video Render Area and make the positions x,y and z axis as 0.
Go to Canvas and make the rotations x,y and z axis as 0. We need to set the positions of Y axis as 0.01 in YouTube player.
Now we need to rotate it also. Set the y axis of rotation as 180. You can set it according to your choice as well. Now paste a YouTube URL that you want to display on an image target in the YouTube player URL.
Scanning the image target:
Now the thing is when we will scan the image target video will start playing but when the target is lost video will not stop. Video will continue playing in the background. Thus, we will continue listening to the sounds of the video. So, in order to avoid that we need to code few lines. So, go to create and click on C# script and name it as track able event handler. Open this file by double clicking on it.
Coding of track able event handler:
The code of this track able event handler file is totally same as to the default track able event handler file that is attached to our image target. We will open this file. We only have to do few minor changing in code. Press Ctrl A and then Ctrl C and paste it to the track able event handler file.
Now in the track able event handler file delete the default placed code and press Ctrl V to paste the default track able event handler code in this file. Coding of track able event handler:heck the name of the file which is track able event handler file and give the same name to the class.
Adding unity engine event:
So, basically what we have to change in this file is that we are going to add unity engine event. So, we will be using codes as:
using Unity Engine. Events;
Declaring public variables:
We need to declare two public variables onTrack and onLost.
public UnityEvent onTrack;
public UnityEvent onLost;
The event onTrack needs to be called when tracking is found. So, here we will write
The second unity event onLost needs to be called when tracker is lost. So, here we will write
Adding track able event handler:
Now in image target we have to declare this delete this default track able event handler and add the track able event handler . Now in these events click on + in onTrack () and drop in the youtube player here. Click on + in on Lost in the YouTube player here.
Select YouTube player from the drop down and select play () function. Select YouTube player from the drop down and select pause() or stop() function according to your choice.
From top menu click on file, then click on build settings. Check the platform is android if not switch it. And then click on add open scenes. Now click on player settings. Go to other settings. Change the package name according to your choice. Unchecked x86 and Android TV compatibility. Change the stripping level to low. Click on XR settings and make sure vuforia augmented reality is selected. And then you can insert the icon of your app. That’s all for settings now connect your android mobile with your computer and click on build and run. Now name it and save it.