Unity Object-c Interaction

Source: Internet
Author: User

One, Unity calls Object-c

A/C + + can interact directly with object-c, as long as the file suffix. m is changed directly to. mm and becomes a mixed file between C + + and object-c.

C # can also call C + + methods, so C # can interact with OBJECT-C.

Procedure: C # calls the C + + call Object-c

1.c/c++ Call Object-c

Create a new test.mm file that defines a C-style interface function. Such as:

test.mm

extern " C " {  void Log (char* Arg) {  //  can call Object-c's function here  //
   
     such as [[Alertview alloc] init]; ... 
    }}
   

Place the test.mm file under the assets/plugins/iOS path for Unity project

2.c# Call C + +

New TestPluginiOS.cs, such as:

TestPluginiOS.cs

#ifUnity_ios &&! Unity_editor//compile only on iOS platformusingUnityengine;usingSystem.Collections;usingSystem;usingSystem.Runtime.InteropServices;//dllimport under this namespace Public classUniwebviewplugin {[DllImport ("__internal")]        Private Static voidLog (stringArg); //1. Access rights are not necessarily private//2. Must be static//argument is string instead of char*}#endif

Note: The parameter is string instead of char*, and the relationship between C and C # parameter types is mapped http://blog.csdn.net/yatusiter/article/details/9221861

Second, object-c call C #

Use this method directly in object-c to call C # functions

Unitysendmessage ("GameObjectName1""MethodName1" "  Message to send"); // parameter 1: Added the Gameobject name to invoke the function Script Component // parameter 2: Name of the callback function // Parameter 3: Parameters of the callback function

Like Java callback C # in Android development, three parameters are string types! But the third parameter in Android development cannot be null (if there are no arguments, you can use an empty string "", because using a null program will collapse)

Unity Object-c Interaction

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.