【问题标题】:Write Unity IOS plugin in Swift code用 Swift 代码编写 Unity IOS 插件
【发布时间】:2021-02-13 22:59:48
【问题描述】:

是否可以用 Swift 编写 unity IOS 插件?

我已经有一个可以工作的 swift 框架,并希望将它用作 Unity 中的插件

我看到一些地方说它只能在 Objective-c 上完成,但是有 swift 的解决方法吗?

【问题讨论】:

    标签: ios objective-c swift unity3d


    【解决方案1】:

    如何调用 Unity 方法

    Unity接口函数定义在Unity构建的Xcode项目的UnityInterface.h中。此头文件在 UnitySwift-Bridging-Header.h 中导入,因此您可以直接在 Swift 代码中调用这些函数。

    要调用 Unity 方法,请使用 UnitySendMessage 函数,如下所示:

        //  Example.swift
    
    import Foundation
    
    class Example : NSObject {
        static func callUnityMethod(_ message: String) {
            // Call a method on a specified GameObject.
            UnitySendMessage("CallbackTarget", "OnCallFromSwift", message)
        }
    }
    

    如何从 Unity 访问 Swift 类

    第 1 步:创建您的 Swift 类。

    //  Example.swift
    
    import Foundation
    
    class Example : NSObject {
        static func swiftMethod(_ message: String) {
            print("\(#function) is called with message: \(message)")
        }
    }
    

    第 2 步:包含“unityswift-Swift.h”并定义 C 函数以将 Swift 类包装在 .mm 文件中(Objective-C++)。

    //  Example.mm
    
    #import <Foundation/Foundation.h>
    #import "unityswift-Swift.h"    // Required
                                    // This header file is generated automatically when Xcode build runs.
    
    extern "C" {
        void _ex_callSwiftMethod(const char *message) {
            // You can access Swift classes directly here.
            [Example swiftMethod:[NSString stringWithUTF8String:message]];
        }
    }
    

    第 3 步:创建接口类以从 C# 调用导出的 C 函数。

    // Example.cs
    
    using System.Runtime.InteropServices;
    
    public class Example {
        #if UNITY_IOS && !UNITY_EDITOR
        [DllImport("__Internal")]
        private static extern void _ex_callSwiftMethod(string message);
        #endif
    
        // Use this method to call Example.swiftMethod() in Example.swift
        // from other C# classes.
        public static void CallSwiftMethod(string message) {
            #if UNITY_IOS && !UNITY_EDITOR
            _ex_callSwiftMethod(message);
            #endif
        }
    }
    

    第 4 步:从 C# 代码中调用方法。

    Example.CallSwiftMethod("Hello, Swift!");
    

    UnitySwift-Bridging-Header.h和unityswift-Swift.h的文件名在“Objective-C Bridging Header”条目和“Objective-C Generated Interface Header Name”条目中定义构建设置。这些设置和其他关于 Swift 编译器的设置由 PostProcesser 在 Unity 构建运行时自动设置。

    要求

    iOS 7 或更高版本

    兼容性

    Unity 5.3.5f1 Xcode 7.3.1

    【讨论】:

    • 这是旧版本的 Swift 但它是一个很好的例子
    • @Abs3akt,不幸的是,这种方法不再起作用 - 只是找不到“unityswift-Swift.h”
    【解决方案2】:

    由于 Unity 根本无法访问顶级 Swift,因此 Swift 的“解决方法”是围绕它编写一个 Objective-C 包装类,然后访问 那个

    取决于您的 Swift 代码的数量和复杂性,这可能仍然是最佳方法。

    【讨论】:

    • 谢谢,你能指导我如何制作你提到的桥吗?我是否需要编写objective-c包装器并在统一构建xcode项目后导入swift框架?任何相关的文档都会很棒
    猜你喜欢
    • 2014-08-07
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2023-03-06
    • 1970-01-01
    • 1970-01-01
    • 2014-11-27
    相关资源
    最近更新 更多