簡體   English   中英

如何將參數從 iOS (Objc/Swift) 傳遞給 Unity 結構/類? (不使用 UnitySendMessage)

[英]How to pass arguments from iOS (Objc/Swift) to Unity that are structs/classes? (without using UnitySendMessage)

我正在嘗試找到一種將參數從 objc/swift 中的本地統一插件傳遞到 C# 統一的好方法。

似乎我需要使用 Marshall,但我在任何地方都找不到這樣的好例子。

我發現的唯一一件事是 UnitySendMessage,但它僅將字符串作為參數傳遞,即使這些字符串也被限制為 1024 字節,這對於對象的 JSON 表示來說是不夠的,並且處理多個消息似乎有點矯枉過正這個。

這個想法是能夠從 MTLTexture 質疑用於對象檢測的插件並返回被識別的對象。

代碼示例:

雨橋

import Foundation
import UIKit
import Vision

@objc public class SwiftBridge: NSObject {

  var delegate: DelegateCallbackFunction?

  @objc static let shared = SwiftBridge()

  @objc func evaluate(texture: MTLTexture) -> Bool {
    guard let delegate = self.delegate else {
      return false
    }

    let rect = CGRect(x: 1, y: 2, width: 100, height: 200)
    delegate(rect)

    return true
  }

  @objc func setDelegate(callback: @escaping DelegateCallbackFunction) -> Bool {
    self.delegate = callback

    return true
  }
}

統一

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

[StructLayout(LayoutKind.Sequential)]
public struct CGPoint {
    public float x;
    public float y;
};

[StructLayout(LayoutKind.Sequential)]
public struct CGSize {
    public float width;
    public float height;
};

[StructLayout(LayoutKind.Sequential)]
public struct CGRect {
    public CGPoint origin;
    public CGSize size;

}


public class UnityBridge : MonoBehaviour {

    #region Declare external C interface    
    // #if UNITY_IOS && !UNITY_EDITOR

    [DllImport("__Internal")]
    private static extern int _vision_detectObjectsIn(IntPtr texture);

    delegate bool ObjectDetectedCallback(ref CGRect rect);
    [DllImport("__Internal")]
    private static extern void _vision_setDelegate(ObjectDetectedCallback callback);

    [MonoPInvokeCallback(typeof(ObjectDetectedCallback))] 
    private static bool delegateMessageReceived(ref CGRect rect) {
        Debug.Log("Message received: " + rect.origin.x);
        return true;
    }
    // #endif
    #endregion

    public void initializeDelegate() {
        if (Application.platform == RuntimePlatform.IPhonePlayer) {
            _vision_setDelegate(delegateMessageReceived);
        }
    }

   #region Wrapped methods and properties
   public void EvaluateTexture(IntPtr texture) {
    initializeDelegate();

    if (texture == IntPtr.Zero) {
      Debug.LogError("[Texture] Pointer to buffer is null.");
      return;
    }

    bool success;

     #if UNITY_IOS && !UNITY_EDITOR
     _vision_detectObjectsIn(texture);
     #endif
   }
   #endregion

   #region Singleton implementation
   private static WeRDetectorUnity _instance;
   public static WeRDetectorUnity Instance {
       get {
           if (_instance == null) {
               var obj = new GameObject("WeRDetectorUnity");
               _instance = obj.AddComponent<WeRDetectorUnity>();
           }
           return _instance;
       }
   }

   void Awake() {
       if (_instance != null) {
           Destroy(gameObject);
           return;
       }

       DontDestroyOnLoad(gameObject);
   }
   #endregion


}

Unity 中的消息接收打印不會返回 1,而是一個奇怪的指數小數。 任何的想法??

塊引用

您需要為傳遞參數創建結構,您可以多次使用參數,也可以在主類中訪問。

結構位置{讓緯度:“緯度”讓經度:“經度”}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM