Unity和OC交互

2023-02-27  本文已影响0人  浪淘沙008

步骤一:

将.m文件修改为.mm文件,并在其中实现需要调用的方法,然后通过C代码调用已经实现的方法,代码如下:

#import "UKit.h"
#import <UIKit/UIKit.h>

static UKit * _instance = nil;
@implementation UKit

+ (UKit *)instance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[UKit alloc] init];
    });
    return _instance;
}

- (void)ShowWarningBox:(NSString *)strTitle text:(NSString *)message
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:strTitle message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击ok按钮");
        UnitySendMessage("Canvas", "OnButtonClick", [@"你好啊Unity" UTF8String]);
    }];
    
    [alertController addAction:okAction];
    
    UIAlertAction * cancleAction = [UIAlertAction actionWithTitle:@"cancle" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了取消按钮");
    }];
    [alertController addAction:cancleAction];
    UIViewController * viewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
    [viewController presentViewController:alertController animated:YES completion:^{
        NSLog(@"显示了对话框");
    }];
}
extern "C" {
    void ShowWarningBox(char *strTitle, char *message)
    {
        [[UKit instance] ShowWarningBox:[NSString stringWithUTF8String:strTitle] text:[NSString stringWithUTF8String:message]];
    }
    
    void CallOC(int val1, float val2, bool val3, const char *val4) {
        NSLog(@"Object-c Called. val1=%d, val2=%f, val3=%d, val4=%s", val1, val2, val3, val4);
        NSString * title = @"标题";
        NSString * message = [NSString stringWithFormat:@"Object-c Called. val1=%d, val2=%f, val3=%d, val4=%s", val1, val2, val3, val4];
        [[UKit instance] ShowWarningBox:title text:message];
    }
    
    // C#函数的函数指针
    typedef void (*cs_callback)(int);
    void CallBack(cs_callback callback) {
        callback(999);
    }
    
}
@end

步骤二:

创建Assets/Plugins/iOS文件夹,并将OC文件拖到该文件夹下;打包后该文件也会包含在工程项目中,如图:

WeChat81ed0cab0d1ceabe481581792de472fc.png
WeChat213a3cf0c54e0a02a8e7b62cb0b9f44a.png

步骤三:

创建C#调用方法的文件,在其中导入OC文件中声明的C方法并进行调用,代码如下:

using UnityEngine;
using System.Runtime.InteropServices;
using AOT;

public static class UnityIOSKit
{
    delegate void callback_delegate(int val);


    [DllImport("__Internal")]
    private static extern void ShowWarningBox(string strTitle, string message);

    [DllImport("__Internal")]
    private static extern void CallOC(int val1, float val2, bool val3, string val4);

    // 声明包含回调方法的方法
    [DllImport("__Internal")]
    private static extern void CallBack(callback_delegate callback);

    static public void ShowAlert(string strTitle, string strText)
    {
        
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            ShowWarningBox("提醒弹框", "弹框内容");
        }
    }

    static public void myCallOC()
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            CallOC(1314, 3.1415926f, true, "文字内容");
        }
    }

    //回调函数,必须MonoPInvokeCallback并且是static
    [MonoPInvokeCallback(typeof(callback_delegate))]
    private static void cs_callback(int val)
    {
        UnityEngine.Debug.Log("cs_callback : " + val);
    }

    static public void CallBack()
    {
        if (Application.platform == RuntimePlatform.IPhonePlayer)
        {
            //直接把函数传过去
            CallBack(cs_callback);
        }
    }
}

参考:unity 与oc交互

上一篇 下一篇

猜你喜欢

热点阅读