• 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
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
_met44

People who like this

1 Show 0
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
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

People who like this

0 Show 0 · 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

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
baba

People who like this

1 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 instead 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Error loading a 3.0 framework dll (WFC) 1 Answer

How Can Format Shader (.Fx) to (BYTE kVertexShaderCode[]) and (BYTE kPixelShaderCode[]) 0 Answers

Can´t found android.support.v4.content.ContextCompact 0 Answers

get_isActiveAndEnabled can only be called from the main thread 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