Write Unity IOS Module to Swift Code

Is it possible to write a single IOS plugin in Swift?

I already have a working quick framework and would like to use it as a plugin in Unity

I've seen a few places that say this can only be done in Objective-c, but is there a workaround for a quick one?

+3


source to share


2 answers


Since the top-level Swift is simply not available from Unity, a "workaround" for Swift is to write an Objective-C wrapper class around it and access it.



Depending on the amount and complexity of your Swift code, which might be the most optimal.

+5


source


How to call Unity methods

Unity interface functions are defined in UnityInterface.h in an Xcode project built by Unity. This header file is imported into UnitySwift-Bridging-Header.h, so you can call functions directly in your Swift codes.

To call Unity methods, use the function UnitySendMessage

as shown below:

    //  Example.swift

import Foundation

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

      

How to access Swift classes from Unity

Step 1. Create Swift Classes.

//  Example.swift

import Foundation

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

      

Step 2. Include "unityswift-Swift.h" and define C functions to wrap Swift classes in a .mm (Objective-C ++) file.

//  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]];
    }
}

      



Step 3: Create an interface class to call exported C functions from 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
    }
}

      

Step 4. Call the method from your C # code.

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

      

The file names UnitySwift-Bridging-Header.h and unityswift-Swift.h are defined in the "Objective-C Bridging Header" entry and the "Objective-C Generated interface header name" entry in the build settings. These settings and other settings for the Swift compiler are automatically set by PostProcesser when Unity builds .

Requirements

iOS 7 or later

Compatibility

Unity 5.3.5f1 Xcode 7.3.1

0


source







All Articles