Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Robert · Dec 30, 2009 at 05:40 PM · pluginobjective-c

Objective-C Plugin - Arrays

I would like to pass an array of structs from C# to C over the plug-in bridge.

I already have methods working perfectly where I pass primitive values, and also a method where I pass a struct back and forth.

I have searched the Unity Forum and UnityAnswers to see how one would pass an array back but have not found anything for C/Obj-C.

Just looking for a really simple example.

Thanks

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by fhmiv · Jan 07, 2010 at 03:17 PM

System.Collections.ArrayList probably won't pass over the bridge, since it's a .NET class, and C/Obj-C won't know how to interpret it. If you want or need to use this type of array for some other reason, you can take the approach used in the Midi Plugin Example, and reconstruct the ArrayList/NSArray on each side of the bridge by passing each element one at a time and add()ing it.

Your other choice is to use fixed-sized arrays, which are just pointers to the first element in declared like this in C#:

[DllImport ("PluginName")] private static extern int FooPluginFunction(ref YourStruct[] aStruct);

int[] arrayOfInts = { 0, 1, 2, 3 }; // One way to declare it YourStruct[2] arrayOfYourStructs;
arrayOfYourStructs[0] = yourStruct; arrayOfYourStructs[1] = anotherYourStruct; // Declare it with fixed size and assign to it later FooPluginFunction(ref arrayOfYourStructs);

And on the C++ side:

int FooPluginFunction(YourStruct* &inStruct)
{
   inStruct[0].whatever = whatever;
   return someResult;
}

Comment
Add comment · Show 3 · 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 Robert · Jan 07, 2010 at 03:38 PM 0
Share

I will give the fixed-size array a try. I have a variable number of items to move across the bridge, I just wanted to avoid paying the bridging overhead for each item. This method gives me a way to batch in 5's or 10's as appropriate and reduce the bridging overhead.

Thanks for this!!

avatar image jonas-echterhoff ♦♦ · Feb 04, 2010 at 05:45 PM 0
Share

For this to work, you just have to make sure to pass an array of structs, not of classes, as structs store the actual data in the array. Also, you will probably want to pass the size of the array to C++ in another parameter, as you will not be able to get that from the array otherwise.

avatar image idbrii · Feb 12, 2021 at 11:39 PM 0
Share

Here's a formatted version of the code (don't understand how it would work since I don't see how you transfer the definition of YourStruct between C# and C++):

C#

 [DllImport("PluginName")]
 private static extern int FooPluginFunction(ref YourStruct[] aStruct);
 
 void Test()
 {
     int[] arrayOfInts = { 0, 1, 2, 3 }; // One way to declare it 
     YourStruct[2] arrayOfYourStructs; 
     arrayOfYourStructs[0] = yourStruct;
     arrayOfYourStructs[1] = anotherYourStruct; // Declare it with fixed size and assign to it later 
     FooPluginFunction(ref arrayOfYourStructs); 
 }

C++

 int FooPluginFunction(YourStruct* &inStruct)
 {
     inStruct[0].whatever = whatever;
     return someResult;
 }

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

1 Person is following this question.

avatar image

Related Questions

Call Unity class in XCode 1 Answer

UnitySendMessage and NSString allocation 1 Answer

Can you build iOS plugins using objective-c or only with pure c? 1 Answer

Problem with ios plugin to show web view 1 Answer

iOS location permission plugin build fails 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