Thursday, 21 April 2016

Unity3D - Creating a simple Loading bar using AsyncOperation & UI Images using C#

This is to show how to create a simple loading progress using unity AsyncOperation.

Step 1; Create a UI image and name it as Progressbar and set the pivot point of the image to left center and width = 50 , height = 20.
Step 2: Create a new C# script and name it as LoadingBar.
Step 3 ; Copy and paste the below Code.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
using System.Collections.Generic;

public class LoadingBar : MonoBehaviour 
{
    public Image loader;
    RectTransform objectRectTransform;
    AsyncOperation async;
    float tempval;

    IEnumerator Start()
    {
        objectRectTransform = loader.GetComponent<RectTransform>();
        tempval = objectRectTransform.rect.width;
        Debug.Log(tempval);

        yield return new WaitForSeconds(0.1f);
        async = Application.LoadLevelAsync("Scene1");//add the next scene name that to be loaded
        yield return async;
    }
  void Update()
    {
        try
        { 
            Debug.Log(async.progress);
            tempval += async.progress;
            loader.rectTransform.sizeDelta = new Vector2(tempval, 20f);
        
        }
        catch (NullReferenceException ex)
        {
            Debug.Log("Async not loaded!");
        }
    }

}
Step 4: Create a Empty Game object and drag the LoadingBar script on to it.
Step 5: In inspector add the ProgressBar image to variable loader.
Step 6 : Replace "Scene1" with next scene name that to be loaded in line   async = Application.LoadLevelAsync("Scene1"); on the above code
Step 7; Verify that you have added the current scene and next scene in Build settings.
Step 8: That's it. You can now play the scene and check it.

Wednesday, 13 April 2016

Setting Up EmguCV for Unity3D

In this we will see how to set up Emgucv for Unity3D

I am using

  • Unity3D 5.3.3 personal edition
  • EmguCv - emgucv-windesktop 3.1.0.2282.exe

download link : https://sourceforge.net/projects/emgucv/

Getting Started


  1. Download and run the emgucv-windows setup exe. It will create a folder in C drive named as Emgu
  2. Fire up Unity3D and create a new project.
  3. Go to C:\Emgu\emgucv-windesktop 3.1.0.2282\bin  folder and copy all the .dll files including some .dll files from the folder x64( if you are using a 32bit or copy the files from x86 if you working on a 64bit)
  4. Now open the new unity project folder which you have created.
  5. Then open the Assets folder under new project and paste all the copied .dll files.
  6. Now browse the folder where you have installed unity3D normally it should be under C:\Program Files\
  7. Under this search for the file System.Drawing.dll. Copy this file and paste it along with the other .dll files under NewProject>>Assets folder
  8. You need to add EmguCv path to System Environment Variables(Create a new system variable and also add the X64 directory under path variable too something similar to this ";Emgu\emgucv-windesktop 3.1.0.2282\x64")

  9. That's it EmguCv is now setup for Unity3D. You can start playing around.


Wednesday, 6 April 2016

Detecting Webcam in Unity3D using C#

This is a basic tutorial explains how to detect webcam in Unity3D.


  • Create a new project in Unity3D
  • Create a new plane in unity3D under GameObject >> 3D Object >> Plane
  • Set the Y rotation of plane to 180.
  • Create a new folder named "Scripts" under assets.
  • Create a new C# file under the Scripts folder and name it as DetectWebcam
  • Drag the script over plane / Attach the script on the newly created plane

  • Double click the DetectWebcam script file to launch Monodevelop/ Visual studio.
  • Copy & paste the below code.

using UnityEngine;
using System.Collections;
public class DetectWebcam : MonoBehaviour
{
    void Start() 
    {
        WebCamDevice[] devices = WebCamTexture.devices;
        for( var i = 0 ; i < devices.Length ; i++ )
        {
            Debug.Log(devices[i].name);// Gets the list of available devices and prints its name
        }

        string deviceName = WebCamTexture.devices[0].name;
        WebCamTexture newTexture = new WebCamTexture(deviceName, 640, 360, 30);
        newTexture.Play();

        gameObject.GetComponent<Renderer>().material.mainTexture = newTexture;
            
    }
}
  • Save the code and play the scene in Unity3D.
  • That's it 

Setting Up OPenCv & Tesseract for Visual Studio 2013 -OCR

Step 1: Download all the required tools
  • OpenCv - https://sourceforge.net/projects/opencvlibrary/files/opencv-win/3.1.0/opencv-3.1.0.exe/download
  • Tesseract - https://github.com/tesseract-ocr/tesseract/wiki/Downloads
  • Visual Studio 2013
Step 2 : Setting up OpenCV
  1. Extract the downloaded OpenCv installer to C drive or any preferred drives.
Step 3: Setting up Tesseract

Copy all the downloaded files to C drive where you have already extracted OpenCv. 

Step 4: Setting up VS 2013

Run Visual Studio 2013 set to get it installed.
By this step I hope all the required tools are downloaded and got it installed/copied to respective drives.

Step 4: Setting up environment variables
  • Right click on My Computer>> properties
  • Then click on Advanced  System Settings
  • Click on Environment Variables under Advanced tab.
  • Then click on new under System variables
  1. Add variable name as OPENCV_DIR and variable value as C:\opencv\build\x64\vc12\bin (this is the path of openCv bin folder check where you have copied your openCv files)
  2. Create one more new System variable and fill the details as variable name as TESSDATA_PREFIX variable value as C:\Tesseract-Build\tesseract-vs2013\ (this is the path of tesseract bin folder check where you have copied your tesseract files)
  3. Now search for Path variable under System variables and add the following followed by a ;C:\opencv\build\x64\vc12\bin;C:\Tesseract-Build\tesseract-vs2013\build\lib\x64
Step 6: Configuring OpenCv and Tesseract for VS 2013 
  • Launch VS 2013.
  • File >> New >> Project(Ctrl+Shift+N)
  • Choose Visual C++ >> Win32 Console Application
  • Give a name for your new project and click OK to continue

  • Click Next to continue
  • Click Finish to complete
  • Right over the project under Solution Explorer and select the option Properties(Alt + Enter)
  • Under C/C++ .. select the Additional Include Directories and add the path of Tesseract and OpenCv include folders something as below
C:\Tesseract-Build\tesseract-vs2013\include
C:\Tesseract-Build\tesseract-vs2013
C:\opencv\build\include
  • Then select General under Linker and add all the library paths for tesseract and OpenCv, something as below. You can check the tesseract and OpenCv folders which you have already copied to your C drive to get the exact path
C:\Tesseract-Build\tesseract-vs2013\build\lib\x64
C:\Tesseract-Build\tesseract-vs2013\lib
C:\opencv\build\x64\vc12\lib\
$(OPENCV_DIR)\lib
  • Then Move on to Input under Linker and add the following, which can be found under C:\opencv\build\x64\vc12\lib and C:\Tesseract-Build\tesseract-vs2013\build\lib\x64

opencv_world310.lib
opencv_world310d.lib
liblept168.lib
libtesseract302.lib
  • That's it now OPenCv & Tesseract is successfully configured for Visual Studio 2013.
  • Note - You need to configure similarly whenever you create a new project.
  • So Let us test it now
  • Save the below hello world  image and copy it to your project folder something similar as below path: Visual Studio 2013\Projects\openCvtest\openCvtest

  • Now create a new C++ file by right clicking on source files
  • Now open the newly created C++ file and paste the below code

#include "opencv2/core/core.hpp"
#include"opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
#include <iostream>

int main(int argc, const char** argv)
{
Mat img = imread("helloworld.jpg", CV_LOAD_IMAGE_UNCHANGED);

namedWindow("MyWindow", CV_WINDOW_AUTOSIZE); //create a window with the name "MyWindow"
imshow("MyWindow", img); //display the image which is stored in the 'img' in the "MyWindow" window

waitKey(0); //wait infinite time for a keypress

return 0;
}
  • Then press Ctrl+F5 to compile the code
  • This window opens up an image window with Hello World Text as shown below.
  • That's it you have successfully configured openCv and tesseract for VS2013