- Home /
Unity Editor freeze
Hi,
I'm experiencing quite a problem and I hope someone can help me. For information, this is partly linked to another question because this explains what I did to get to my problem:
http://answers.unity3d.com/questions/515264/importing-cc-function-from-dll.html#answer-515366
The problem is that Unity completely freeze when I update a script in visual studio and then switch back to the unity editor.
Here's my code and setup:
I have a simple scene with a camera and a "Test" script applied on it. That test script reference a "DLLTest" dll to get data from a custom controller device. That device sends a number through USB each 500ms.
The "DLLTest" is the interface that reads the data from the device. It contains 2 classes and reference to a C++ DLL called AID.dll. that dll also reference another dll called FDT2XX.dll. Those 2 dll are placed in the folder /Assets/Plugins. All other files are at the root of the project.
here's the implementations
The "Test" script applied the the main camera: using UnityEngine; using System.Collections; using DLLTest;
public class Test : MonoBehaviour {
MyUtilities utils;
// Use this for initialization
void Start ()
{
utils = new MyUtilities();
if(utils.StartRead())
print("Success");
else
print("Failed");
}
// Update is called once per frame
void Update ()
{
string s = utils.GetStatus();
if ( s != null)
print(s);
else
print("Crap!");
}
}
The 2 classes in the DLLTest using System; using USBTransfer;
namespace DLLTest
{
public class MyUtilities
{
private HardwareComm hardwareComm;
private string lastData = "";
bool newData = false;
public bool StartRead()
{
// How to use the HardwareComm
hardwareComm = new HardwareComm();
hardwareComm.Received += DataReceive;
bool link = hardwareComm.startLinking();
return link;
}
public string GetStatus()
{
if (newData)
{
newData = false;
return lastData;
}
return null;
}
// Receive data
private void DataReceive(object sender, String data)
{
newData = true;
lastData = data;
}
}
}
This second class has the read logic
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
namespace USBTransfer
{
public delegate void DataReceiveHandler(object sender, String data);
class HardwareComm
{
// Event handler
public event DataReceiveHandler Received;
private Thread th;
// Import DLL
[DllImport("AID")]
static extern uint FT_ListDevices();
[DllImport("AID")]
static extern uint FT_Open();
[DllImport("AID")]
static extern uint FT_Close();
[DllImport("AID")]
static extern uint FT_Write([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
[DllImport("AID")]
static extern uint FT_SetBaudRate(ulong data);
[DllImport("AID")]
static extern uint FT_GetStatus(ref ulong rxsize, ref ulong txsize);
[DllImport("AID")]
static extern uint FT_SetBitMode(byte mask, byte enable);
[DllImport("AID")]
static extern uint FT_Read([MarshalAs(UnmanagedType.LPArray)] byte[] p_data, ulong size);
[DllImport("AID")]
static extern uint FT_EE_Read(ref ushort vid, ref ushort pid, ref ushort power);
[DllImport("AID")]
static extern uint FT_EE_Program(ushort power);
[DllImport("AID")]
static extern uint FT_EE_ProgramToDefault();
[DllImport("AID")]
static extern uint KCAN_Send(uint channel, uint id, uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
[DllImport("AID")]
static extern uint KCAN_Receive(ref uint channel, ref uint id, ref uint dlc, [MarshalAs(UnmanagedType.LPArray)] byte[] p_data);
[DllImport("AID")]
static extern uint KCAN_Init(uint baudraute);
public HardwareComm()
{
}
/// <summary>
/// This method link to the hardware.
/// Return false if no devices connected, true otherwise.
/// </summary>
public Boolean startLinking()
{
uint nbOfDevices = FT_ListDevices();
if (nbOfDevices < 1)
{
// Don't detect the board game
return false;
}
FT_Open(); // Open RS232 PORT equivalent
FT_SetBaudRate(9600); // Set baud rate to 9600, 8bit(default), no parity(default), no protection(default)
//Start thread
th = new Thread(LoopReading);
th.Start();
return true;
}
/// <summary>
/// Structure to receive data
/// </summary>
protected virtual void OnReceive(String data)
{
if (Received != null)
Received(this, data);
}
private void LoopReading()
{
while (true)
{
byte[] data = new Byte[64];
uint i, length;
String temp = "";
length = FT_Read(data, 1);
for (i = 0; i < length; i++)
{
temp += Convert.ToString(data[i], 16);
}
OnReceive(temp);
}
}
}
}
Note that this code works. If I play the game and then start the device, the numbers will show in the console. However, if I start the device and then click play, the Editor will freeze before printing anything. Also, even if the editor is stopped and well, if I make any change in my test script, the Editor freeze when I switch back to it. The only way to continue is to kill Unity in the task manager and restart it.
I'm working with Unity 4.2 with the Pro trial on Windows 7
What am I doing wrong here?
Thank you for your help.
Ok I made some more tests with that and I realized that when Unity editor is freshly opened, I can edit the test script without freezing. If I play once the game, everything is fine. However, if I try to play the game again, il will freeze. It will also freeze if I edit the test script after the first play.
Could it be related to the Thread I start in HardwareComm.StartLinking() ? I never used threads in unity before.
I had a similar problem and it ended up being the while statement for me. I got rid of the while statement and used conditions within Update ins$$anonymous$$d to solve my problem.
I'm not sure to understant what you are suggesting
Is it that I should remove the "While(true)" in the LoopReading() and just call it in the update function? With that, I don't even need a thread.
For whatever reason, while loops do not work well in unity, but the update function works like a while loop. So, what I display below should be the same as a while statement..
void Update() {
If( loopTime ) {
DoSomething();
// Once your condition has been met, make loopTime false
}
}
On the other hand, I'm not 100 percent sure that this is the problem.
Answer by Byard · Aug 14, 2013 at 07:35 PM
I found the problem.
I needed to call the function FT_Close() before stopping the game. So I added a OnDestroy() function in my test script and called a StopRead() function in my first DLL which calls the FT_Close().
Your answer
![](https://koobas.hobune.stream/wayback/20220613112221im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity crashes when I open a project Linux Ubuntu 16.04. 3 Answers
Why Unity crashes and how can I fix it? 0 Answers
Unity Editor quits silently 0 Answers
Unity Editors crashes after using a button in scene 2 Answers
URGENT!!! - Unity keeps crashing 1 Answer