- Home /
Data is lagging the external device. Why?
I am trying using a 3D input device. I have the C/C++ DDL file in the plugin folder. I was able to read the data from the input device using the DLL API.
I am now experiencing this problem. When I move the device, the data I read and displayed using Debug.Log, lagged by about 1 second. This is not good because whatever I want to animate in Unity cannot respond to me instantanteously. I have no idea what causes the lag.
I wrote another program in C++ to read the data through the same DLL and display it. Interestingly, there is no lagging at all. Seem like the problem is interaction between Unity and the DLL.
I have been looking through the forum for anyone with similar problem, but don't seem to find any. Any advice and pointer will be deeply appreciated. Thanks.
Answer by ferraribeng · Jul 27, 2011 at 03:19 AM
Hi,
I have finally determined what the problem is and fixes it. Here is a write up of what the problem is and how I resolve it.
Cause of problem:
I have a read device data API function inside my DLL to read the device data once. I called this function from Unity C# script each time I want to read the device data. The data read is stored in variables. The variables can then be assessed by calling another DLL API function to get the varaible data. Naturally inside the C# script Update function, I called the read data API function to initiate a read device data and then I call the API function to get the stored device data from the variables. The update function effectively allows an endless loop reading of the device data.
It is apparent that each time I call the DLL API read device data function, there is a 1 second delay in execution of the function due to interface between Unity and DLL read function. This subsequently result in delay in updating the variables. Therefore, when I call the DLL API function to read the variables immeidately after the call the read device data functon, I am really reading the old data. Therefore, this caused a lagging respond.
.
Solution to the problem:
I have to change the C++ DLL coding a bit.
I created a thread inside the DLL to read the device data and store the data in private variables in a While loop. Therefore, the DLL will continuously reading the device data in the background. This ensure that variables containing the device data is constantly being updated.
At Unity C# script, I just call the API function to get the device data from the variable. In this way, I do not have any lagging respond at all as oppose to having to initiate a read device data.
This is my solution. It is very much like the mouse under windows, C# script or JavaScript is only reading the current data from a variable, rather than tell the mouse to read.
hope this info is helpful to someone outthere. Have fun...*
Your answer
![](https://koobas.hobune.stream/wayback/20220612143129im_/https://answers.unity.com/themes/thub/images/avi.jpg)