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
1
Question by baba · Apr 07, 2014 at 11:21 PM · pluginpluginsunity plugin

How do i use typedefs, enums declared in my C plugin .h?

I am learning to write C plugins for Unity.

From C# I can access functions declared in my C code using dllImport. But now I would like to also access typedef enums etc declared in it .h file.

For instance in my C .h file I have something like:

 typedef struct _myStruct  * myStruct_handle_t;

and

 void          myCFunction(  myStruct_handle_t myStruct );


now when i try importing this function in C# script with dllImport, I get the following error:

Assets/PluginImport.cs(28,49): error CS0246: The type or namespace name `myStruct_handle_t' could not be found. Are you missing a using directive or an assembly reference?

How can I use types defined in my C code? For instance is there a way to include my .h file in my c# script?

thanks,

Baba

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

2 Replies

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

Answer by baba · Apr 09, 2014 at 10:13 PM

Actually in my case the type i want to use is an opaque pointer. Therefore I do not need (want) to expose the internals of my struct to the c# script. Therefore the solution is to simply write IntPtr instead of myStruct_handle_t in my dllImport extern declaration and it works.

For the enum I just redefined them as public enum myEnum_t : int { / .. enums, /} in my C# script

_met44 solution seems to be the correct solution in the case where you want to have access to your struct fieldsfrom C# but I haven't tried it.

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
1

Answer by _met44 · Apr 07, 2014 at 11:25 PM

you need to redefine that in C# with [StructLayout(LayoutKind.Sequential)] check out this msdn page it has some good example (and that is a rare thing :D) of what you have to do :

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.layoutkind.aspx

Comment
Add comment · Show 2 · 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 baba · Apr 07, 2014 at 11:31 PM 0
Share

Thanks! Would that process be any easier if I used javascript ins$$anonymous$$d of c# in unity? (I am equally beginner in both javascript and c#)

avatar image _met44 · Apr 08, 2014 at 12:06 AM 0
Share

I have no idea since I haven't done that using JS, but I would go with C# since it is well documented and really easy to implement...

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

22 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

Related Questions

How do I tell Unity to use my updated C plugin? 0 Answers

Failed to load 'Assets/Plugins/x86_64/FlexWrapper.dll 1 Answer

error CS1704: An assembly with the same simple name 'Editor' has already been imported. Try removing one of the references or sign them to enable side-by-side. 0 Answers

Is there any other way to call into Unity from Objective-C other than UnitySendMessage? 2 Answers

Putting Platform Specific Code in a plugin 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