Friday, December 5, 2014

Unity3d C# and JavaScript Communication

        Hmm.. problem is you have a code written in javascript and most of your codes written in c# and you really want to use javascript with your c# script or other way around. Is there way to do that. Of course you can. Unity3d has this special topic in their documentation [Special Folders and Script Compilation Order].

Simplified note
  • If you want to use javascript on your c# script javascript need to compile before the c# script.
  • There are some special folders to do that (compilation folder order)
    1. Standard Assets
    2. Pro Standard Assets
    3. Plugins
    4. Editor
    5. Other folders will compile last
  • Lets see a simple cube moving example for this c# and javascript communication.
Setting up the environment
  1. Add a cube game object
  2. Add a empty game object called JavaScriptComponent (This is for attach js)
  3. Add a folder name called Script (to put our calling scripts)
  4. Add a folder name called Standard Assets or Plugins (Any one of the special folder)


How to call a javascript from c# script

  • The JavaScript
  1. Go to Input.GetAxis documentation sample and grab the JS version of the code.
  2. Make the rotation & translation variables global.
  3. Add 2 public functions call getRotation(), getTranslation() that returns rotation & translation
  4. Save the script inside Standard Assets
  5. Attach this script to JavaScriptComponent empty gameobject
  6. Find the code snippet of the MoveScript.js
      //[code snippet from http://docs.unity3d.com/ScriptReference/Input.GetAxis.html]
 // A very simplistic car driving on the x-z plane.
 var speed : float = 10.0;
 var rotationSpeed : float = 100.0;
 
 
 private var translation : float;
 private var rotation : float;
 
 function Update () {
  // Get the horizontal and vertical axis.
  // By default they are mapped to the arrow keys.
  // The value is in the range -1 to 1
  translation = Input.GetAxis ("Vertical") * speed;
  rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
  
  // Make it move 10 meters per second instead of 10 meters per frame...
  translation *= Time.deltaTime;
  rotation *= Time.deltaTime;
 }
 
 //We are going to access these 2 methods from csharp script
 public function getRotation(){
  return rotation;
 }
 
 public function getTranslation(){
  return translation;
}
  • The CSharp Script
  1. So now we know which game object holds our javascript.
  2. Use GameObject.Find ("JavaScriptComponent").GetComponent<MoveScriptJs>();  this code to get the js script reference.
  3. Attach the script to the Cube game object
  4. Find the Player.cs code snippet.
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

 private MoveScriptJs javaScript;

 // Use this for initialization
 void Start () {
  javaScript = GameObject.Find ("JavaScriptComponent").GetComponent<MoveScriptJs>();
 }
 
 // Update is called once per frame
 void Update () {
  // Move translation along the object's z-axis
  transform.Translate (0, 0, javaScript.getTranslation());
  // Rotate around our y-axis
  transform.Rotate (0, javaScript.getRotation(), 0);
 }
}


Now you can run the scene and see cube can be rotate and move, Most part of the code is done by javascript. Only rotation and transform floating point values are applying to this game object vectors by using c# script.How cool is that. Yes I know, this is a stupid way to move a game object but I wanted to show a kinda complex example and in next tutorial I want to show how to use the unity standard asset mobile joystick with c# script. The problem is unity ships this joystick assets with only javascript so how do we use this for mobile game that programmed using c# ? I know if you had follow this tutorial properly you can do it alone :) #happyCoding

Download the unity3d project [link]

Sunday, July 20, 2014

Apache TCPMon Axis2 [How To]


  1. Download the TCPMon-bin zip and extract 
  2. Go to tcpmon-1.0-bin/build path
  3. There you see tcpmon.sh and tcpmon.bin files to run tcpmon-1.0.jar easily
  4. If you are using Linux version first make it as executable file sudo chmod +x tcpmon.sh
  5. Run the jar file using sudo ./tcpmon.sh 
  6. Go to Admin panel in TCPMon app 

    • Change the Listen port to anything eg: 8081 instead of your default listen port 8080
    • Change the Target port to 8080
    • And click Add button it will create a new tab
  7. Now the TCPMon will listen to 8081 port and handle the communication with the default port 8080
  8. Start the apache tomcat server and make sure instead of using localhost:8080 use TCPMon Listen port localhost:8081
  9. Simply navigate to the service or run the client application and explorer the result

Saturday, January 18, 2014

Unity3D and Webcam using WebCamDevice, WebCamTexture with C#, JavaScript


C# solution 


using UnityEngine;
using System.Collections;

public class WebCam : MonoBehaviour {

    void Start () {
        WebCamDevice[] devices = WebCamTexture.devices;
        if (devices.Length > 0) {
            WebCamTexture webcamTexture = new WebCamTexture(devices[0].name ,320, 240, 10);
            renderer.material.mainTexture = webcamTexture;
            webcamTexture.Play();
        }       
    }
}

JavaScript solution 


function Start () {
    var devices : WebCamDevice[]  = WebCamTexture.devices;
    if (devices.length > 0) {
        var webcamTexture : WebCamTexture = WebCamTexture(320, 240, 10);
        renderer.material.mainTexture = webcamTexture;
        webcamTexture.Play();
    }       
}

Saturday, April 13, 2013

Visual c++ Converting String ^ to std::string (basic string)


private:string toString(String ^ s){
                string n;
                using namespace Runtime::InteropServices;
                const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
                n = chars;
                Marshal::FreeHGlobal(IntPtr((void*)chars));
                return n;
}

Tuesday, April 2, 2013

Oracle server & Oracle sqldeveloper installation on windows


Setting up oracel server

  1. Download Oracle SQL Express Edition 11g Click Here
  2. Unzip the downloaded file open DISK1 folder and run the setup.exe
  3. While installation enter a password for System (administrator) account **(important)**
  4. After installation is done Go to Start -> Run -> services.msc in windows
  5. Check the service called OracleServiceORCL if there is nothing called OracleServiceORCLservice
  6. Start -> Run -> cmd type on cmd to create the service c:\ORADIM -NEW -SID ORCL
  7. Again navigate to services.msc and select OracleServiceORCL service and start the service
  8. Again navigate to cmd it's good that you restart cmd prompt type c:\sqlplus system@orcl on cmd
  9. Then it will ask to Enter password: so enter the password that you entered while installation process (3rd step)
  10. Then cmd will ask to Enter user-name: system default user name is system
  11. Again Enter password: -> and enter
  12. Finally this is the success message Connected to:Oracle Database 11g Express Edition Release 11.2.0.2.0 - Production
  13. Open the desktop icon called Get Started With Oracle Database 11g Express Edition
  14. Server installation DONE!

Setting up oracle sqldeveloper

  1. Download the oracle sqldeveloper Click Here
  2. Extract and open sqldeveloper.exe
  3. Tick all the file types, then click OK button
  4. Then right click on Connections and click New Connection
  5. Inside New Connection enter any connection name
  6. Enter username as default one called system
  7. Enter password as server password that you entered while oracle server installation
  8. Test the connection if succeeded, Save and Connect DONE!