Converting PXC scripts for use in Unity script files

Converting PXC scripts for use in Unity script files

Hi everyone,

Some of you may be wanting to use PXC camera scripts in Unity instead of the supplied Action scripts in the RS Toolkit but are not sure how to go about it.  So I thought I'd post a quick guide here.

When you look at a PXC script like those in the RealSense manual, you'll see that they have no header and footer section like most scripts you are probably familiar with.  This can be a great cause of confusion as you wonder how you can use them in a Unity project.  

The solution is actually quite simple though.  You can just paste the PXC script into the middle of a header-and-footer template that you can use over and over.  You then paste the merged script into a C# script file that you have created in Unity and place the script inside an object and run the program.

Here's the steps involved.

STEP 1:  Create a C# script file in Unity

STEP 2:  Delete all of the default code that was in the script when it was created so that you have a totally empty script.  Then paste the following hader lines at the top of the script.

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

 

STEP 3:  Use the Enter / Return key to move down the script editor window and create empty space.  Going about 10 lines down should be fine.

 

STEP 4:  At the base of the script, place a closed bracket

 

}

 

Your script should now look like this:

 

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

 

 

 

 

 

}

 

STEP 5:  Paste the PXC script into the empty space above the end-bracket.  Here's an example of a PXC script found on page 135 of the RealSense SDK manual that we inserted between the header and footer.  The script loads a gesture pack and enables a gesture.

 

using UnityEngine;

using System.Collections;

using System.Collections.Generic;

 

public class Rotation : BaseAction {

 

 

 

// hcfg is a PXCMHandConfiguration instance

// Load a gesture pack

hcfg.LoadGesturePack("navigation");

 

// Enable a gesture

hcfg.EnableGesture("swipe");

 

// Apply changes

hcfg.ApplyChanges(); 

 

 

}

 

STEP 6:  On line 4, the Public Class one, ensure that the word after 'Class' matches the name of your C# script file.  Our script file was called 'Rotation', hence why our line says 'public class Rotation'.  If your own script was called UnityCamera then the line of your script should read:

 

public class UnityCamera : BaseAction {

 

STEP 7:  Place your C# script inside an object in Unity, such as a simple test cube, and run the program.  In the case of the particular script above, the large green and small red lights on the camera should come on to signify that the script is running successfully.

 

Hope this helps folks out!

 

4 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

Added note: make sure that the script code you are using is in the C# format.  In the RealSense SDK manual, the line 'C# Example' is always above the C# version of a script, not below it.

HI..

Here is an error while running your code.please help me to sort it out.

Error is looks like this:

Assets/scripts/Rotation.cs(21,29): error CS1519: Unexpected symbol `(' in class, struct, or interface member declaration

 

Thanks.

Hi there,

C# is not my first language so it's hard for me to debug problems in it.  However, I spent some time doing some testing and found that you can get rid of the error you mentioned by using the structure below.

***********

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class NAMEOFYOURSCRIPT : MonoBehaviour {

    void Start () {
    

// Use the term void Update() if it is an Update type function you are going to be using/ 

 

INSERT YOUR BLOCK OF C# CODE HERE

    }

}

 

*********

The error you encountered seems to be because it wants a Void statement after the public class and then an extra } symbol at the end to take account of the Void statement you inserted.

 

Leave a Comment

Please sign in to add a comment. Not a member? Join today