找回密码
 立即注册
查看: 185|回复: 0

Unity开发C#和OC相互调用

[复制链接]
发表于 2022-11-23 10:05 | 显示全部楼层 |阅读模式
一、C#调用OC


OC代码:
////  CallOC.m//  UnityFramework////  Created by 飞杨 on 22.11.22.//#import "CallOC.h"#if defined (__cplusplus)extern "C"{#endif        //基础调用    void test()    {         NSLog(@"call test");    }        //调用时传入参数    void testInt(int param)    {        NSLog(@"call testInt%d",param);    }        //调用时传入字符串参数    void testString(const char * param)    {        NSString *s = [NSString stringWithUTF8String:param];        NSLog(@"call testString:%@",s);    }        //调用并返回值    bool testReturn()    {        return false;    }        //调用并返回字符串    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;    }}

打印结果

二、OC调用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 回调 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;                //模拟回调,不做任何处理。        if(testCallUnityBlock != nil)        {            testCallUnityBlock();        }    }    #if defined (__cplusplus)}#endif

打印结果

如需了解更多请看
Unity调用IOS和Android
<hr>
Unity 2020.0.38f1
Xcode 13.4.1

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-6-27 22:27 , Processed in 0.090247 second(s), 26 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表