Prerequisites
This project assumes you are familiar with the basic functionality of Unity. You should also review the following before moving on:
Setup
VINT Hub

Configure Unity
In order to use Phidgets with Unity, you have to configure your project. Follow the instructions below.
Step 1
Download a copy of the Phidget libraries and unzip it as shown.
Step 2
Next, open Unity and create a new 3D project. Name it Phidgets_Unity and place it in the location of your choice.
Step 3
Navigate to the file you previously unzipped and import Phidget22.NET.dll and phidget22.dll as shown.
Note: If you are developing on macOS, you do not need to import phidget22.dll
Create Environment
Now that your project is configured, you can create your environment.
Step 6
Drag the new script onto the Directional Light object in the Hierarchy tab.
Write Code (C#)
Copy the code below into your LightController file.
using UnityEngine;
using Phidget22;
public class LightController : MonoBehaviour
{
public Light myLight;
public bool useRealLight = false;
public float maxRoomIlluminance = 50.0f; //this will vary based on your room, try modifying it in the editor window
LightSensor lightSensor;
// Start is called before the first frame update
void Start()
{
//Create
myLight = GetComponent<Light>();
lightSensor = new LightSensor();
//Open
lightSensor.Open(1000);
//Set data interval to minimum | This will increase the data rate fromthe sensor and allow for smoother transitions
lightSensor.DataInterval = lightSensor.MinDataInterval;
}
// Update is called once per frame
void Update()
{
//Use your Phidgets
if(useRealLight){
myLight.intensity = ((float)lightSensor.Illuminance/maxRoomIlluminance)/8.0f; //max light intensity is 8.0
}
}
//Required for Unity and Phidgets
private void OnApplicationQuit()
{
lightSensor.Close();
lightSensor.Dispose();
}
}
Run Your Program
Make sure to toggle the Use Real Light button in your simulator as shown in the video above. Try modifying the amount of light hitting your Light Phidget by covering it with your hand or shining a flashlight on it. You will see the light in your scene increase and decrease accordingly!
Code Review
When using the Light Phidget, it's important to set the data interval to the minimum as shown in the code above. This will ensure your sensor is as responsive as possible.
Practice
- Try adding this functionality to another project. Real lighting can make your project incredibly immersive!