如何将iOS(Objc/Swift)中的参数传递给结构/类的Unity?(不使用UnitySendMessage)
我正试图找到一种好方法,将参数从我的原生统一插件objc/swift传递到C#unity 似乎我需要使用Marshall,但我在任何地方都找不到一个很好的例子 我唯一发现的是UnitySendMessage,但它只将字符串作为参数传递,甚至那些字符串也被限制为1024字节,这对于对象的JSON表示是不够的,因此处理多条消息似乎有点过分 其想法是能够从MTLTexture中查询用于对象检测的插件,并返回已识别的对象 代码示例: SwiftBridge如何将iOS(Objc/Swift)中的参数传递给结构/类的Unity?(不使用UnitySendMessage),ios,unity3d,Ios,Unity3d,我正试图找到一种好方法,将参数从我的原生统一插件objc/swift传递到C#unity 似乎我需要使用Marshall,但我在任何地方都找不到一个很好的例子 我唯一发现的是UnitySendMessage,但它只将字符串作为参数传递,甚至那些字符串也被限制为1024字节,这对于对象的JSON表示是不够的,因此处理多条消息似乎有点过分 其想法是能够从MTLTexture中查询用于对象检测的插件,并返回已识别的对象 代码示例: SwiftBridge import Foundation impor
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
}
使用系统;
使用UnityEngine;
使用System.Runtime.InteropServices;
使用AOT;
[StructLayout(LayoutKind.Sequential)]
公共结构CGPoint{
公共浮动x;
公众浮躁;
};
[StructLayout(LayoutKind.Sequential)]
公共结构CGSize{
公共浮动宽度;
公众浮标高度;
};
[StructLayout(LayoutKind.Sequential)]
公共结构CGRect{
公共点源;
公共CG规模;
}
公共类UnityBridge:单行为{
#区域声明外部C接口
//#if UNITY_IOS&!UNITY_编辑器
[DllImport(“内部”)]
私有静态外部int _vision _detectObjectsIn(IntPtr纹理);
委托bool ObjectDetectedCallback(ref CGRect rect);
[DllImport(“内部”)]
私有静态外部void\u vision\u setDelegate(ObjectDetectedCallback);
[MonoPInvokeCallback(typeof(ObjectDetectedCallback))]
接收到私有静态bool delegatemessage(ref CGRect rect){
Log(“收到的消息:“+rect.origin.x”);
返回true;
}
//#endif
#端区
public void initializeDelegate(){
if(Application.platform==RuntimePlatform.IPhonePlayer){
_vision_setDelegate(delegateMessageReceived);
}
}
#区域包装方法和属性
公共void EvaluateTexture(IntPtr纹理){
初始化Legate();
如果(纹理==IntPtr.Zero){
LogError(“[Texture]指向缓冲区的指针为null”);
返回;
}
成功;
#如果UNITY\u IOS&!UNITY\u编辑器
_视觉检测对象(纹理);
#恩迪夫
}
#端区
#区域单例实现
私有静态WeRDetectorUnity_实例;
公共静态WeRDetectorUnity实例{
得到{
if(_instance==null){
var obj=新游戏对象(“WeRDetectorUnity”);
_instance=obj.AddComponent();
}
返回_实例;
}
}
无效唤醒(){
如果(_实例!=null){
摧毁(游戏对象);
返回;
}
DontDestroyOnLoad(游戏对象);
}
#端区
}
消息receive print in Unity并没有返回它应该返回的1,而是一个奇怪的指数小数字。
有什么想法吗?Blockquote
您需要为其pass参数创建struct,您可以多次使用参数,也可以在主类中访问参数
结构位置{
让纬度:“纬度”
让经度:“经度”
}区块报价
您需要为其pass参数创建struct,您可以多次使用参数,也可以在主类中访问参数
结构位置{
让纬度:“纬度”
让经度:“经度”
}Tnx回答得这么快!你的意思是在C#和ObjC中创建一个结构?因为我这样做了,而且由于某种原因,当我调用回调时,发送的参数不同:\struct:location{}Tnx,因为应答如此之快!你的意思是在C#和ObjC中创建一个结构?因为我这样做了,并且由于某种原因,在调用回调时,发送的参数不同:\struct:location{}