Appear iOS SDK
Appear是一个应用程序开发平台,提供工具帮助您构建具有动态增强现实内容的APP。此框架允许您将增强现实资源上传到数据库并在需要时在您的应用程序中访问它们。您还可以更新现实内容,而无需更新应用程序。
安装SDK
预备条件
在开始之前,您需要在您的环境中设置一些内容
- Xcode 11或更高版本
- 针对iOS 13或更高版本的Xcode项目
- Swift项目必须使用Swift 5.0或更高版本
- 您的应用程序的包标识符
- CocoaPods 1.4.0或更高版本
添加SDK
登录到 Appear Console,创建一个项目。创建项目 创建客户端 上传.Reality文件 下载plist文件并将其拖放到Xcode项目。
将 Appear 框架添加到您的项目
Appear 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile
pod 'Appear'
在您的应用程序中初始化 Appear
在您的 UIApplicationDelegate 中导入 Appear 模块
import Appear
在您的应用程序的应用:didFinishLaunchingWithOptions: 方法中配置 AppearApp。可选地添加启用调试或禁用缓存等选项
// without options
AppearApp.configure()
// with options
AppearApp.configure([.enableDebugging, .disableCaching])
如果您还没有这样做,请记住在项目中添加 NSCameraUsageDescription。将以下代码添加到您的 info.plist 文件中
<key>NSCameraUsageDescription</key>
<string>{YOUR APP NAME} requires access to your phone’s camera.</string>
从 appear 控制台网站下载 AppearInfo.plist 文件并将其拖入您的项目
使用说明
现实项目的快速简单实现
使用 Storyboard
创建 RealityProjectViewController 的子类
class YourCustomViewController: RealityProjectViewController { }
在 storyboard 中,现在可以添加一个 UIViewController 并将类设置为 YourCustomViewController
编程方式
创建 RealityFileViewController 的实例。将其放置在按钮点击函数或 viewDidAppear 中以导航到相机视图。
let vc = RealityFileViewController()
present(vc, animated: true, completion: nil)
您可以自定义教程视图
if let tutorialView = gameVC.tutorialView as? SimpleTutorialView {
// update the title
tutorialView.setTitle("Hold kameraet mot ansiketet ditt")
// update the description
tutorialView.setDescription("Laster...")
// update the background color
tutorialView.setBackgroundColor(UIColor.black.withAlphaComponent(0.8))
}
或者您可以用自己的 UIView 替换默认的教程视图。只需确保在呈现之前替换 RealityFileViewController 上的 tutorialView 即可。
// create an instance of your own subclass of UIView
let customTutorialView = CustomTutorialView()
// create an instance of the RealityFileViewController
let vc = RealityFileViewController()
// replace the tutorialView
vc.tutorialView = customTutorialView
// present
present(vc, animated: true, completion: nil)
默认情况下,RealityFileViewController 将获取所有已上传的活跃 .reality 文件。如果您想指定要使用的 .reality 文件,只需使用标识符配置 RealityFileViewController 即可。
// create an instance of the RealityFileViewController
let vc = RealityFileViewController()
// configure with the identifier of the .reality file that should be displayed
vc.configure(withIdentifier: "")
每当从行为接收通知时,您都可以运行代码。例如,当用户点击 3D 模型、检测平面/图像/对象等时,可以运行网络请求。
vc.onAction { (identifier, entity) in
if identifier == "your_identifier" {
// Do something
}
}
要从 Xcode 中通知/触发行为,可以轻松调用 sendAction 方法来通知行为。您可以传递表示受影响对象之一的实体或实体名称。
// passing in the name of the enitity
vc.sendAction(withIdentifier: "your_identifier", entityName: "nameOfObject")
// passing in the entity
vc.sendAction(withIdentifier: "your_identifier", entity: entity)
其他功能
// Enable people occlusion
vc.isPeopleOcclusionEnabled = true
// Adds a occlusion material to the detected plane.
vc.isOcclusionFloorEnabled = true
真实项目的高级实现
在您的ViewController中创建一个AppearManager实例,以获取所有上传的资源。
获取项目
// create an instance of the AppearManager
let manager = AppearManager()
// fetch the project
manager.fetchRealityProject { (result) in
switch result {
case .success(let project):
print(project)
case .failure(let error):
// Handle error here
}
}
此项目对象包含一组RealityMedia对象。这些是上传的真实文件。
如果您不想获取项目,而只想加载具有特定标识符上传的真实文件,您也可以这样做。
manager.fetchMedia(withID: "c7dc2f20-2330-4b59-b5c2-379d55a860a7") { (result) in
switch result {
case .success(let media):
case .failure(let error):
print(error.localizedDescription)
}
}
}
为了将这些真实文件放置到场景中,我们目前需要将真实文件本地存储,以便可以通过RealityKit加载。
// fetching the reality file, storing it file with URL.
manager.fetchRealityFileArchiveUrl(from: media) { (result) in
switch result {
case .success(let url):
// load Entity with URL
case .failure(let error):
// Handle error here
}
}
为了处理来自真实文件收到的警报,确保ViewController符合AppearManagerDelegate。此代理有一个可以实现的函数来处理传入的警报。
func didReceiveActionNotification(withIdentifier identifier: String, entity: RealityKit.Entity?)
为了从Xcode中通知/触发行为,您必须首先在管理器上调用setupActionListener方法。
manager.setupActionListener()
一旦设置好了动作监听器,您就可以通过在管理器上调用onAction方法轻松地通知行为。您可以(可选)传递表示受该行为影响的对象之一的部分或名称。
// passing in the name of the enitity
manager.sendAction(withIdentifier: identifier, entityName: "nameOfObject")
// passing in the entity
manager.sendAction(withIdentifier: identifier, entity: entity)
版本控制
有关可用的版本,请参阅此存储库的标签。
作者
Purpl, [email protected]
许可
Appear iOS SDK在MIT许可下提供。有关更多信息,请参阅LICENSE文件。