Unity開發(fā)C#和OC相互調(diào)用

一、C#調(diào)用OC

OC代碼:

//
//  CallOC.m
//  UnityFramework
//
//  Created by 飛楊 on 22.11.22.
//

#import "CallOC.h"

#if defined (__cplusplus)
extern "C"
{
#endif
    
    //基礎(chǔ)調(diào)用
    void test()
    {
         NSLog(@"call test");
    }
    
    //調(diào)用時傳入?yún)?shù)
    void testInt(int param)
    {
        NSLog(@"call testInt%d",param);
    }
    
    //調(diào)用時傳入字符串參數(shù)
    void testString(const char * param)
    {
        NSString *s = [NSString stringWithUTF8String:param];
        NSLog(@"call testString:%@",s);
    }
    
    //調(diào)用并返回值
    bool testReturn()
    {
        return false;
    }
    
    //調(diào)用并返回字符串
    const char* testReturnString()
    {
        NSString *privacyUrl = @"testReturnString";
        return strdup(privacyUrl.UTF8String);
    }
    
#if defined (__cplusplus)
}
#endif

C#代碼:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;

public class CallOC : MonoBehaviour
{
#if UNITY_IOS && !UNITY_EDITOR
    [DllImport("__Internal")]
    private static extern void test();

    [DllImport("__Internal")]
    private static extern void testInt(int param);

    [DllImport("__Internal")]
    private static extern void testString(string param);

    [DllImport("__Internal")]
    private static extern bool testReturn();

    [DllImport("__Internal")]
    private static extern string testReturnString();
#endif
    // Start is called before the first frame update
    void Start()
    {
        Test();
        TestInt(55);
        TestString("hello word");
        bool b1 = TestReturn();
        Debug.Log("TestReturn:"+b1);
        string s1 = TestReturnString();
        Debug.Log("TestReturnString:"+s1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void Test()
    {
#if UNITY_IOS && !UNITY_EDITOR
           test();
#endif
    }

    public void TestInt(int param)
    {
#if UNITY_IOS && !UNITY_EDITOR
           testInt(param);
#endif
    }

    public void TestString(string param)
    {
#if UNITY_IOS && !UNITY_EDITOR
           testString(param);
#endif
    }

    public bool TestReturn()
    {
#if UNITY_IOS && !UNITY_EDITOR
         return testReturn();
#endif
        return true;
    }

    public string TestReturnString()
    {
#if UNITY_IOS && !UNITY_EDITOR
         return testReturnString();
#endif
        return string.Empty;
    }
}

打印結(jié)果

二、OC調(diào)用C#

C# 代碼


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;

public class CallUnity : MonoBehaviour
{

    [DllImport("__Internal")]
    public static extern void setCallback(IntPtr showPlayer);

    // Start is called before the first frame update
    void Start()
    {
        UFPCallback handler = new UFPCallback(Callback);
        IntPtr callbackDelegate = Marshal.GetFunctionPointerForDelegate(handler);
        SetCallback(callbackDelegate);
    }

    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void UFPCallback();

    [AOT.MonoPInvokeCallback(typeof(UFPCallback))]
    static void Callback()
    {
        Debug.Log("OC 回調(diào) C#");
    }

    public void SetCallback(IntPtr showPlayer)
    {
        setCallback(showPlayer);
    }
}

OC代碼:

//
//  CallOC.m
//  UnityFramework
//
//  Created by 飛楊 on 22.11.22.
//

#import "CallOC.h"

typedef void (*CallUnity) ();

#if defined (__cplusplus)
extern "C"
{
#endif
    static CallUnity testCallUnityBlock;
    void setCallback(CallUnity showPlayer)
    {
        testCallUnityBlock = showPlayer;
        
        //模擬回調(diào),不做任何處理。
        if(testCallUnityBlock != nil)
        {
            testCallUnityBlock();
        }
    }
    
#if defined (__cplusplus)
}
#endif

打印結(jié)果

如需了解更多請看
Unity調(diào)用IOS和Android


Unity 2020.0.38f1
Xcode 13.4.1

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容