Unity开发C#和OC相互调用
2022-11-21 本文已影响0人
游戏创作者
一、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;
}
}
![](https://img.haomeiwen.com/i19957372/13920e0ec3d26c23.png)
二、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
![](https://img.haomeiwen.com/i19957372/c24466f28ce106d4.png)
如需了解更多请看
Unity调用IOS和Android
Unity 2020.0.38f1
Xcode 13.4.1