添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • 头文件 iOSBridgePlugin.h

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    // ----------------------------------------
    // --- ios 自定义插件接口声明
    // ----------------------------------------

    #ifdef __cplusplus
    extern "C"{
    #endif

    void ShowTips(const char* goName, const char* callFnName, const char* msg);

    #ifdef __cplusplus
    } // extern "C"
    #endif


  • 实现文件 iOSBridgePlugin.mm .

    这个是混编文件 (oc + c/c++) , xcode 可以自动识别为 Objective-C++ 文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    #import "iOSBridgePlugin.h"
    #import "Classes/Unity/UnityInterface.h" // 引入 unity 相关 api

    void ShowTips(const char* goName, const char* callFnName, const char* msg) {
    NSLog(@"--- ShowTips");
    NSString* go = [NSString stringWithUTF8String:goName]; // c 字符串 转成 oc 字符串, 这里一定要先转成 oc, 不然 const char* 调用后就会释放掉栈内存, 会导致 UnitySendMessage 回传 unity 失败
    NSString* fn = [NSString stringWithUTF8String:callFnName];
    NSString* content = [NSString stringWithUTF8String:msg];
    UIAlertController * alert = [UIAlertController
    alertControllerWithTitle:@"Hi, wilker."
    message:content
    preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* yesButton = [UIAlertAction
    actionWithTitle:@"Reply"
    style:UIAlertActionStyleDefault
    handler:^(UIAlertAction * action) {
    const char* rspMsg = [[NSString stringWithFormat: @"ios replay: %@", content] UTF8String]; // oc 字符串 转成 c 字符串
    UnitySendMessage([go UTF8String], [fn UTF8String], rspMsg); // ios 调用 unity
    }];

    [alert addAction:yesButton];

    UIViewController* rootCtrl=[UIApplication sharedApplication].keyWindow.rootViewController;
    [rootCtrl presentViewController:alert animated:YES completion:nil];
    }
  • 在 csharp 代码导入并使用这个插件 api

    GameMgr.cs

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using UnityEngine;
    using UnityEngine.UI;

    public class GameMgr : MonoBehaviour {
    public Text txt;
    public GameObject go;

    void Start() {
    gameObject.name = "GameMgr";
    DontDestroyOnLoad(gameObject);
    }

    public void OnNativeCall(string data) {
    Debug.LogFormat("--- OnNativeCall, data: {0}", data);
    txt.text = data;
    }

    public void CallNative() {
    string msg = "hello ios-" + Random.Range(1000, 9999);
    #if UNITY_IPHONE || UNITY_IOS
    ShowTips(gameObject.name, "OnNativeCall", msg); // 调用 ios api
    #else
    Debug.LogErrorFormat("--- no implemention on platform: {0}", Application.platform.ToString());
    #endif
    }

    void Update() {
    go.transform.Rotate(Vector3.up * 50 * Time.deltaTime);
    }


    // ------------------- ios native api
    #if UNITY_IPHONE || UNITY_IOS
    [DllImport("__Internal")]
    private static extern void ShowTips(string goName, string callFnName, string msg);
    #endif
    }
  • build 一下生产 xcode 工程

  • 打开 xcode.

    Plugins 目录会移到 Libraries 目录下

  • cmd + R 编译并运行到手机.

    不能运行到 ios 模拟器上, 因为 unity 导出的是 arm 架构的库, 而 ios 模拟器时 x86 架构.

    扩展 UnityAppController

  •