用Swift代码编写Unity IOS插件
可以用Swift编写unity IOS插件吗 我已经有了一个工作的swift框架,并希望将其用作Unity中的插件用Swift代码编写Unity IOS插件,ios,objective-c,swift,unity3d,Ios,Objective C,Swift,Unity3d,可以用Swift编写unity IOS插件吗 我已经有了一个工作的swift框架,并希望将其用作Unity中的插件 我看到一些地方说它只能在Objective-c上完成,但是swift有解决办法吗?因为顶级swift根本无法从Unity访问,所以swift的“解决办法”是围绕它编写一个Objective-c包装类,并访问它 取决于Swift代码的数量和复杂性,这可能仍然是最理想的方法。如何调用Unity方法 Unity接口函数在Unity构建的Xcode项目中的UnityInterface.h中
我看到一些地方说它只能在Objective-c上完成,但是swift有解决办法吗?因为顶级swift根本无法从Unity访问,所以swift的“解决办法”是围绕它编写一个Objective-c包装类,并访问它
取决于Swift代码的数量和复杂性,这可能仍然是最理想的方法。如何调用Unity方法 Unity接口函数在Unity构建的Xcode项目中的UnityInterface.h中定义。此头文件在UnitySwift Bridgeting header.h中导入,因此您可以直接在Swift代码中调用函数 要调用Unity方法,请使用如下函数:
// 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)")
}
}
// 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]];
}
}
// 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
}
}
Example.CallSwiftMethod("Hello, Swift!");
步骤2:包括“unityswift Swift.h”并定义C函数以将Swift类封装在.mm文件(Objective-C++)中。
// Example.swift
import Foundation
class Example : NSObject {
static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
// 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]];
}
}
// 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
}
}
Example.CallSwiftMethod("Hello, Swift!");
步骤4:从C#代码调用方法。
// Example.swift
import Foundation
class Example : NSObject {
static func swiftMethod(_ message: String) {
print("\(#function) is called with message: \(message)")
}
}
// 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]];
}
}
// 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
}
}
Example.CallSwiftMethod("Hello, Swift!");
UnitySwift桥接头.h和UnitySwift Swift.h的文件名在生成设置中的“Objective-C桥接头”条目和“Objective-C生成接口头名称”条目中定义。这些设置和关于Swift编译器的其他设置是在Unity build运行时自动设置的
要求
iOS 7或更高版本
兼容性
统一5.3.5f1代码7.3.1
谢谢,你能告诉我怎么做你提到的那座桥吗?我是否需要编写objective-c包装器,并在unity构建xcode项目后导入swift框架?任何相关文件都会很好。这是旧版本的Swift,但它很棒example@Abs3akt不幸的是,这种方法不再有效了,只是找不到“unityswift Swift.h”