1. 程式人生 > 其它 >unity-與ios互動

unity-與ios互動

技術標籤:Iosunityios互動


title: unity-與ios互動
categories: Unity3d
tags: [unity, ios]
date: 2021-01-31 14:16:54
comments: false
mathjax: true
toc: true

unity-與ios互動


unity 與 ios 互動

  1. 寫 ios 外掛, 丟到平臺層目錄 Assets/Plugins/iOS

    • 標頭檔案 iOSBridgePlugin.h

      // ----------------------------------------
      // --- 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++ 檔案

      #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]; }
  2. 在 csharp 程式碼匯入並使用這個外掛 api

    GameMgr.cs

    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
    }
    

    目錄目錄

  3. build 一下生產 xcode 工程

  4. 開啟 xcode.

    Plugins 目錄會移到 Libraries 目錄下

  5. cmd + R 編譯並執行到手機.

    不能執行到 ios 模擬器上, 因為 unity 匯出的是 arm 架構的庫, 而 ios 模擬器時 x86 架構.

    • 效果