Unity – IOS与Unity交互

  • Unity部署IOS配置简况

  • 接口信息
  • BlueToothManagerInterface.h

    #import 
    @interface BlueToothManagerInterface : NSObject
    @end
    

    BlueToothManagerInterface.mm

    #import "BlueToothManagerInterface.h"
    #import "BlueToothManager.h"
    extern "C"
    {
        //初始化
        void InitBlueToothManager(){
            // TODO
        }
    
        // 运行
        void RunBlue() {
            [[BlueToothManager shareInstance] RunBlue];
        }
    }
    

    将上述文件放置Unity的Plugins/iOS中

  • Unity接口信息
  • using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    using System.Runtime.InteropServices;
    using LuaInterface;
    public class IOSManager {
        private static IOSManager sInstance = null;
        public static IOSManager getInstance() {
            if (sInstance == null) {
                sInstance = new IOSManager ();
            }
            return sInstance;
        }
        #region
        // 注意是两个下划线
        [DllImport("__Internal")]
        private static extern void RunBlue ();
        /// 
        /// 开起蓝牙
        /// 
        public void RunBlueNative() {
            #if UNITY_IOS && !UNITY_EDITOR
            RunBlue();
            #endif
        }
        #endregion
        // 上面即完成了Unity调用iOS的过程
    }
    

    下面我们看下iOS如何调用Unity.
    这里和Android类似,主要利用Unity的UnitySendMessage函数

  • UnitySendMessage接口
  • 同理其中参数1是场景中接受消息的对象,参数2是要执行的函数名,参数3为传入参数.
    具体参考如下,我们在iOS蓝牙模块中定义

    // .h
    /**
     * iOS To Unity
     */
    - (void) testUnity;
    // .m
    - (void) testUnity {
        UnitySendMessage("ScriptObject", "setString", "Test...");
    }
    

    上述经过测试没有问题

    欢迎留言

    avatar
      Subscribe  
    Notify of