Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Kei_Jin · Sep 29, 2016 at 11:08 AM · process

How to use shared memory in Unity ?

Hello, I want to use shared memory in Unity. I create flight Simulator.

Here system idea:

process A: Calculate x,y,z position of air plane, and send position data to shared memory. (Language is C++, C#...?)

unity process: Get position data from shared memory, and translate air plane by new positon data.

processA -> sharedMemory -> Unity

I don't know that Unity can use shared memory, or comunicate to any process.

Please help me!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Batlad · Oct 12, 2016 at 12:20 AM

@Kei_Jin I recently had to solve a similar issue myself.

I was using input from a custom USB controller so I had c++ code to handle its input. I created a shared memory location in a c++ program, I used CreateFileMapping since it is stored in ram and so gives the fastest refresh rate. The guides recommend using the global namespace, but that did not work for me, and simply removing it did work.

You will then need to read from that memory address in c#, this is a little complicated as you have to use unsafe code and pinvokes. Also unity will crash if you ever try to access the pointers but they haven't successfully accessed the memory yet, so be sure to save the project before any testing.

As for transforming your data, just make sure you use the equivalent datatypes on both ends and use Marshal functions to access it. In your case maybe three floats would be best. You may need to cast them from int if there are no marshall float functions.

Useful links: http://pinvoke.net/default.aspx/kernel32/OpenFileMapping.html https://msdn.microsoft.com/en-us/library/windows/desktop/aa366551(v=vs.85).aspx

See code snippetts below for details.

c++ Code:

 //For shared memory with Unity
 #define BUF_SIZE 256
 TCHAR szMapName[] = TEXT("UnityFileMappingObject");
 TCHAR szCountName[] = TEXT("UnityFileCountObject");
 signed int sharedMemOut[7];
 
 LPCTSTR pBuf;
 HANDLE hMapFile;
 
 Init(){
 
 //     Request shared memory from the OS to share with Unity
     hMapFile = CreateFileMapping(
         INVALID_HANDLE_VALUE,    // use paging file
         NULL,                    // default security
         PAGE_READWRITE,          // read/write access
         0,                       // maximum object size (high-order DWORD)
         BUF_SIZE,                // maximum object size (low-order DWORD)
         szMapName);                 // name of mapping object
 
     if (hMapFile == NULL)
     {
         printf(TEXT("Could not create file mapping object (%d).\n"),
             GetLastError());
         return -1;
     }
     pBuf = (LPTSTR)MapViewOfFile(hMapFile,   // handle to map object
         FILE_MAP_ALL_ACCESS, // read/write permission
         0,
         0,
         BUF_SIZE);
 
     if (pBuf == NULL)
     {
         printf(TEXT("Could not map view of file (%d).\n"),
             GetLastError());
         CloseHandle(hMapFile);
         return -1;
     }
 }
 
 Update(){
 sharedMemOut[0] = (int)count;
     sharedMemOut[1] = X[0];
     sharedMemOut[2] = Y[0];
     sharedMemOut[3] = X[1];
     sharedMemOut[4] = Y[1];
     sharedMemOut[5] = SQ[0];
     sharedMemOut[6] = SQ[1];
     CopyMemory((PVOID)pBuf, sharedMemOut, (7 * sizeof(signed int)));
 }

c# unity code:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.InteropServices;
 using Microsoft.Win32.SafeHandles;
 
 public class PlaneController: MonoBehaviour {
     //Shared Memory
         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
         static extern SafeFileHandle OpenFileMapping(
          uint dwDesiredAccess,
          bool bInheritHandle,
          string lpName);
     
         [DllImport("kernel32.dll", SetLastError = true)]
         static extern IntPtr MapViewOfFile(
         SafeFileHandle hFileMappingObject,
         UInt32 dwDesiredAccess,
         UInt32 dwFileOffsetHigh,
         UInt32 dwFileOffsetLow,
         UIntPtr dwNumberOfBytesToMap);
     
     
     /*
         [DllImport("kernel32.dll", SetLastError = true)]
         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
         [SuppressUnmanagedCodeSecurity]
         [return: MarshalAs(UnmanagedType.Bool)]
         static extern bool CloseHandle(IntPtr hObject);
     */
         string szMapName = "UnityFileMappingObject";
     
         const UInt32 STANDARD_RIGHTS_REQUIRED = 0x000F0000;
         const UInt32 SECTION_QUERY = 0x0001;
         const UInt32 SECTION_MAP_WRITE = 0x0002;
         const UInt32 SECTION_MAP_READ = 0x0004;
         const UInt32 SECTION_MAP_EXECUTE = 0x0008;
         const UInt32 SECTION_EXTEND_SIZE = 0x0010;
         const UInt32 SECTION_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | SECTION_QUERY |
             SECTION_MAP_WRITE |
             SECTION_MAP_READ |
             SECTION_MAP_EXECUTE |
             SECTION_EXTEND_SIZE);
         const UInt32 FILE_MAP_ALL_ACCESS = SECTION_ALL_ACCESS;
         private SafeFileHandle sHandle;
         private IntPtr hHandle;
         private IntPtr pBuffer;
         private int sharedInputCount;
         bool attachSuccessful;
      //
         int count;
         int X0;
         int Y0;
         int X1 = -1;
         int Y1 = -1;
         int SQ0 = -1;
         int SQ1 = -1;
     
     void Start (){
     sHandle = new SafeFileHandle(hHandle, true);
             sharedInputCount = 0;
             attachSuccessful = Attach(szMapName, 256);
     }
     
     unsafe public bool Attach(string SharedMemoryName, UInt32 NumBytes)
         {
             if (!sHandle.IsInvalid) return false;
             sHandle = OpenFileMapping(FILE_MAP_ALL_ACCESS, false, SharedMemoryName);
             Debug.Log("Shared mem open: ");
             if (sHandle.IsInvalid) return false;
             Debug.Log("Shared mem open SUCCESS: ");
             pBuffer = MapViewOfFile(sHandle, FILE_MAP_ALL_ACCESS, 0, 0, new UIntPtr(NumBytes));
             Debug.Log("Shared mem mapped: ");
             return true;
         }
     
     unsafe public void Detach()
         {
             if (!sHandle.IsInvalid && !sHandle.IsClosed )
             {
                 //CloseHandle(hHandle); //fair to leak if can't close
                 sHandle.Close();
             }
             pBuffer = IntPtr.Zero;
             //lBufferSize = 0;
         }
         void Update()
         {
             if (!attachSuccessful)
             {
                 attachSuccessful = Attach(szMapName, 256);
                 return;
             }
         }
      void OnApplicationQuit()
         {
             if (attachSuccessful)
             {
                 Detach();
             }
         }
      void FixedUpdate ()
         {
             //get Shared memory Input
             if (!attachSuccessful)
             {
                 return;
             }
             count = Marshal.ReadInt32(pBuffer, 0);
             X0 = Marshal.ReadInt32(pBuffer, 4);
             Y0 = Marshal.ReadInt32(pBuffer, 8);
             X1 = Marshal.ReadInt32(pBuffer, 12);
             Y1 = Marshal.ReadInt32(pBuffer, 16);
             SQ0 = Marshal.ReadInt32(pBuffer, 20);
             SQ1 = Marshal.ReadInt32(pBuffer, 24);
     }
 }

I hope that puts you on the right track.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image doublemax · Oct 12, 2016 at 06:38 AM 0
Share

Very interesting. I will bookmark this for later use :)

Thanks for sharing!

avatar image Kei_Jin · Jan 06, 2017 at 11:25 AM 0
Share

Thank you! I try it, I can connect legacy system. Very very Thanks!

avatar image mrvv · May 20, 2017 at 06:33 PM 0
Share

it works great for about 2$$anonymous$$ and then starts lagging on the unity side. did you have this problem as well ?

avatar image Batlad mrvv · May 21, 2017 at 12:34 AM 0
Share

$$anonymous$$y implementation makes controller lag impossible to detect, because my c program stores the exact 2D location and feeds it to the unity program. So even if there's lag the next update will still put the player in the right location. If you mean whole program lag and fps loss, I'm not sure. I've never noticed this but I haven't run it intensively, (we're still waiting on some more hardware for the full application). But I'll try to do this the next chance I get.

avatar image
0

Answer by ljhvr01 · Dec 27, 2016 at 04:44 AM

@Batlad

Hello Thank you for a good example.

But, Would I able to ask sample unity project for shared memory?

I can't clearly understand how it works in Unity

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by noer3364 · Jan 26, 2021 at 05:55 AM

Have you solved the problem? I have no problem with the way upstairs at windows PC,but how to deal with Android and IOS?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Infinite loop with named pipes 1 Answer

Unity Editor is not displayed 1 Answer

Destroy/Purge GameObjects in Parent Empty Object 0 Answers

Process x86 vs x86_64 issue 0 Answers

Issues running gdb debug mode in runtime,Running GDB during run time of application 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges