Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Unity IOS plugin in Swift code

Is it possible to write unity IOS plugin in Swift?

I already have a working swift framework and want to use it as a plugin in Unity

I saw some places which say it can only be done on Objective-c but is there a workaround for swift?

like image 796
Michael A Avatar asked Jul 26 '15 11:07

Michael A


People also ask

Can you code Unity in Swift?

You can use any Swift code in your Unity project. You can call Swift void's from Unity but also actual return types. You can pass on variables from Unity to the Swift Functions.

How do I embed a Unity game into an iOS native Swift app?

Download the Unity folder and copy all the items inside it to the Unity folder under the Xcode target folder. Open . xcworkspace in the project navigator, drag the Unity folder you just downloaded to your target. Select Copy items if needed and Create Groups .

Can I use Xcode with Unity?

Build the Xcode projectDepending on your versions of Unity and Xcode, you may be able to select Build and Run instead. In this instance, Unity will automatically open the built project in Xcode and run it on a connected device, if possible.


2 Answers

How to call Unity methods

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

To call Unity methods, use UnitySendMessage function like 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 your 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 .mm file (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]];
    }
}

Step 3: Create 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 of UnitySwift-Bridging-Header.h and unityswift-Swift.h are defined in "Objective-C Bridging Header" entry and "Objective-C Generated Interface Header Name" entry in Build Settings. These settings and other settings about Swift compiler are set automatically by PostProcesser when the Unity build runs.

Requirements

iOS 7 or later

Compatibility

Unity 5.3.5f1 Xcode 7.3.1

like image 83
Abs3akt Avatar answered Sep 28 '22 08:09

Abs3akt


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

Depending on the amount and complexity of your Swift code that might still be the most optimal approach.

like image 43
Paul-Jan Avatar answered Sep 28 '22 07:09

Paul-Jan