C# and Objective-C call each other in unity

foreword

When accessing the SDK or customizing some functions in Unity, you need to call the system interface. In the actual operation of the iphone, that is, Unity and iOS call each other. We use c# in Unity and Objective-C (hereafter called oc) in iOS.
The following describes how they call each other.

c# call oc

Implementation in oc:

extern "C" {
    void _SetLoginURLScheme(const char * urlSchema) // c#中的string类型 在mm里是 char *
    {
        //具体业务逻辑
    }
}

c# call:

// 提供各种各样支持 COM interop 及平台调用服务的成员
// 最重要的属性有 DllImportAttribute(可以用来定义用于访问非托管 API 的平台调用方法)
// MarshalAsAttribute(可以用来指定如何在托管内存与非托管内存之间封送数据)。
using System.Runtime.InteropServices;​

// _SetLoginURLScheme 为 .mm 中的方法名
[DllImport("__Internal")]
public static extern void _SetLoginURLScheme( string urlschema); // c#中的string类型 在mm里是 char *

// 使用时直接调用
_SetoginURLScheme(url);

oc calls c

//参数1:unity内gameObject名称
//参数2:该gameObject挂的脚本方法名称
//参数3:传递的参数
 UnitySendMessage("Cube", "buttonCall", ""); 

If there are any mistakes, please point them out.

email:dxmdxm1992#gmail.com

blog: http://blog.csdn.net/david_dai_1108

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325479421&siteId=291194637