franciscochonge 发表于 2022-11-23 10:05

Unity开发C#和OC相互调用

一、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 = ;      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        private static extern void test();        private static extern void testInt(int param);        private static extern void testString(string param);        private static extern bool testReturn();        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{        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);    }        public delegate void 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
页: [1]
查看完整版本: Unity开发C#和OC相互调用