Last active
March 28, 2023 00:57
-
-
Save troy-lamerton/6499cb8861596281596f2b3604a03359 to your computer and use it in GitHub Desktop.
Swift "Hello World" Unity plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Runtime.InteropServices; | |
// Put this C# file anywhere in Assets/ | |
public static class HelloPlugin { | |
/// Call into swift | |
#if UNITY_IOS && !UNITY_EDITOR | |
[DllImport("__Internal", EntryPoint = "LIBHelloWorld")] | |
public static extern void HelloWorld(); | |
#else | |
public static void HelloWorld() { } | |
#endif | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Put this Swift file in Assets/Plugins/iOS/ | |
@_cdecl("LIBHelloWorld") | |
func libHelloWorld() { | |
// do anything here | |
print("Hello World!") | |
} |
Fixed compile error when building for iOS.
If you already have loose swift files building in your Xcode project, it just works.
With an empty unity project you may need to toggle an Xcode build setting so that swift files are built (a few years ago this was required).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Passing parameters also works well.
While the
@_cdecl
is undocumented, I use this annotation a lot in production and have no issues to report.Official support has been proposed in this thread.