Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

General Advice - Format for Hex Input to Unity in 'Real Time'

Discussion in 'Scripting' started by tcmeric, Apr 14, 2020.

  1. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Hi, unity dev for several years. Looking for some advice or feedback.

    I am working on a project that exports hex in 'real time'. I need unity to import said hex stream and parse it.

    In the past I have worked with various DB, json, XML, SO, text files, etc. However, in this case, I am not sure what would be the fastest method for bringing in the data from the first application (which will be constantly updating). The first program is written in C++. The method of export/ import has yet to be decided.

    Reading a text file 10 - 15 FPS doesn't seem like the best method.

    Both applications will be running on windows 10 locally.

    What is the quickest/least garbage creating method for importing data that is constantly being updated?

    Thanks, everyone!
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    By hex do you mean binary data?

    I would assume the fastest would be directly accessing the same memory location. May that be in C#/mono/.net you allocate an array (or other chunk of memory) and tell your C++ lib to stream the data into it... or vice versa.

    But I'm not sure where you're C++ program is running, or how it is running. In which case multiple other methods could come into play.
     
    tcmeric likes this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    I've had very good results with this:
    Code (csharp):
    1.  
    2.             Directory.CreateDirectory(filePath.Substring(0, filePath.LastIndexOfAny(new char[] { '\\', '/' })));
    3.             FileStream stream = File.Create(filePath);
    4.             var formatter = new BinaryFormatter();
    5.             formatter.Serialize(stream, this);
    6.             stream.Close();
    7.  
    Presuming of course that this code is run on a serializable object. I used this to basically save meshes to disk via an intermediate class for later loading and it's incredibly fast and efficient. The intermediate class can be used in a thread as well (unlike anything derived from a Unity class) so running code like this in the background shouldn't affect your framerate at all.
     
    tcmeric likes this.
  4. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    Sorry, yes binary data from an emulator (was looking at in a hex editor).
     
  5. tcmeric

    tcmeric

    Joined:
    Dec 21, 2016
    Posts:
    190
    I guess to adjust the question, can an external program call a function directly to a built unity game, or does unity need to constantly be checking an external file/ stream?
     
  6. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    Native code interop is perfectly possible. Heck, it's how Unity and the .net runtime (mono) communicate. Most of Unity is actually a C++ program, then the scripting runtime is mono/.net (or C# compiled via il2cpp) running side by side with one another. Things like GameObject are really managed object wrappers around internal C++ objects. This is why you have to call 'Destroy' and you have the whole == null override for unity objects.

    Unity has built in support for what they call "native plug-ins". Here is some documentation:
    https://docs.unity3d.com/Manual/NativePlugins.html

    Of course this mostly covers the calling a native plug-in. Getting a callback can occur in multiple ways. For example if you create a delegate you can get a function pointer to pass to the native plug-in which the C++ program can then call to send messages back. How you get that funciton pointer is Marshal.GetFunctionPointerForDelegate:
    https://docs.microsoft.com/en-us/do...ctedfrom=MSDN&view=netframework-4.8#overloads

    That Marshal class has a LOT of what you'll need for well... marshalling data/objects between your .net code and your native plug-in.

    Here is a more in depth about doing the delegate funciton pointer stuff.
    https://docs.microsoft.com/en-us/cp...d-delegates-by-using-cpp-interop?view=vs-2019

    You may want to pay attention to the "pinning" part/link if it's going to be a long living pointer for repeated callbacks. Pinning function pointer will keep the .net memory manager from moving the delegate around in memory invalidating the pointer.
     
    tcmeric likes this.