Comments (10)
Hi Jakub,
Indeed, that behaviour is expected because of the execution order of MonoBehaviours' functions:
- When a game is finishing it invokes the
OnDisable
method of the GameObject that controls the COM port, and then the COM port is closed. - Then, Unity invokes the
OnApplicationQuit
method, but by then the COM port is already closed, so your message will never arrive at the other side.
Check here the order of execution of the functions: https://docs.unity3d.com/Manual/ExecutionOrder.html
Now, there are some solutions:
- If you can guarantee the
OnDisable
of other of the scripts of that GameObject gets invoked before theSerialController.OnDisable
then you can send your custom message from there (be sure of adding a delay after sending the message). - I will modify the library to include a "tear-down" function, as I think yours will be a pretty common use-case.
Do you want to try with the first option in the meantime? I can add the new functionality later on the weekend.
from ardity.
Thanks for fast response. I'll start with OnDisable solution. But how can I add a delay and be sure that the app will wait until it closes? Is adding a coroutine with WaitForSeconds(1); in OnDisable enough?
from ardity.
It turns out you cannot place coroutines in OnDisable, and the fact that I'm using Robotlegs Sharp doesn't help either. I'll wait for your "tear-down" implementation, unless you have any other advices for me :)
from ardity.
Hi,
I am at this moment making the last tests for the library to ensure the tear-down works correctly (so far, it does).
It seems weekend arrived earlier than expected 😃
from ardity.
Alright, the feature is finished.
Could you give it a try and let me know if it works well for you too?
I put in the README another sample Arduino program, the one I used for testing this feature.
Also, there is a new scene called "DemoSceneUserPoll_ReadWrite_TearDown" where I use the tear-down functionality.
The Unity package was also updated with this change.
from ardity.
It works with the device in your supplied test scene. I'll have time to check it in my applilcation tomorrow.
from ardity.
Hello.
Did it work?
from ardity.
Sorry for not writing you back. I'm in the middle of a project and it's a little hot in here :) Your teardown method worked for me. Since I'm a fresh C#/Unity programmer i was wondering what's the diffrence between calling:
void OnDisable() { SendSerialMessage("@REL#0@"); ...
and
void OnDisable() { if (userDefinedTearDownFunction != null) userDefinedTearDownFunction(); ...
and userDefinedTearDownFunction
calling SendSerialMessage("@REL#0@");
on SerialController? Do delegates hold the execution stack or something?
from ardity.
For this to work:
void OnDisable() { SendSerialMessage("@REL#0@");
you must modify the SerialController
file, which is not a good idea with 3rd-party libraries as you will have trouble when upgrading to a new version. Other than that, the lines behave exactly in the same way.
Now, if you meant putting:
void OnDisable() { SendSerialMessage("@REL#0@");
in a different file, then it won't necessarily work as it depends on which OnDisable
gets executed first. If SerialController
's execute first then by the time the OnDisable
of your class gets executed, the COM port is already closed.
from ardity.
And about you last question, no, delegates don't hold the execution stack or anything like that, they are plain function pointers.
I guess your question is why it works now, check at the SerialThread
class, it has an additional change there that was needed so the tear-down worked:
Now, the OnDisable
puts something in the "out-queue", and the thread ensures that it doesn't exit while there is something in that queue, so you can think of the queue being the one that holds down the execution of the other OnDisable
's.
from ardity.
Related Issues (20)
- queue is full dropping messege HOT 8
- Works In Editor but now on windows build HOT 3
- From Unity to Arduino HOT 2
- Working with BLE and Mobile HOT 3
- Can ardity read serial information from arduino attached to an android device via usb c (unity android build)? or is it only for windows?
- The Data is visible in Arduino serial monitor but not reciving in Unity3D HOT 1
- Can't compare strings (sorry am noob) HOT 1
- Game crashes when exiting HOT 2
- cant find the COM HOT 2
- PortMapper trial HOT 2
- Does Ardity work on Linux ? HOT 1
- Dropping too much messages when frame rate is below 100 fps HOT 3
- Flooding queue when device is removed unexpectedly on linux HOT 5
- how to use aridity to send byte HOT 6
- Use with Mobile HOT 1
- Unity Package contains un-update source
- Windows 11 - Exception: Access is Denied HOT 1
- Sending data from unity to arduino
- Thread is blocking for readTimeout even when no data is available
- A specific question HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ardity.