Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active March 6, 2024 15:47
Show Gist options
  • Save drewolbrich/946eae4af57938155456f5ca947e850a to your computer and use it in GitHub Desktop.
Save drewolbrich/946eae4af57938155456f5ca947e850a to your computer and use it in GitHub Desktop.
A wrapper around iOS Entity/loadAsync(named:in:) and visionOS Entity(named:in:) async that works on both platforms
//
// TextureResource+LoadFileAsync.swift
//
// Created by Drew Olbrich on 12/24/23.
// Copyright © 2023 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import Foundation
import RealityKit
import Combine
extension TextureResource {
// The RealityKit visionOS `TextureResource(named:in:options:) async` initializer
// isn't available in iOS 17, so we implement our own replacement on iOS and wrap
// both implementations in `TextureResource/loadFileAsync(named:in:options:)`.
#if os(visionOS)
/// Loads a texture resource from a file in a bundle asynchronously.
///
/// Unlike `TextureResource(named:in:options:) async` or
/// `TextureResource.loadAsync(named:in:options:)`, this method works on both iOS
/// and visionOS.
static func loadFileAsync(named name: String, in bundle: Bundle? = nil, options: TextureResource.CreateOptions = .init(semantic: nil)) async throws -> TextureResource {
return try await TextureResource(named: name, in: bundle, options: options)
}
#else // !os(visionOS)
/// Loads a texture resource from a file in a bundle asynchronously.
///
/// Unlike `TextureResource(named:in:options:) async` or
/// `TextureResource.loadAsync(named:in:options:)`, this method works on both iOS
/// and visionOS.
static func loadFileAsync(named name: String, in bundle: Bundle? = nil, options: TextureResource.CreateOptions = .init(semantic: nil)) async throws -> TextureResource {
return try await withCheckedThrowingContinuation { continuation in
var cancellable: AnyCancellable?
cancellable = TextureResource.loadAsync(named: name, in: bundle, options: options).sink(receiveCompletion: { completion in
switch completion {
case .finished:
break
case .failure(let error):
continuation.resume(throwing: error)
}
cancellable?.cancel()
}, receiveValue: { entity in
continuation.resume(returning: entity)
cancellable?.cancel()
})
}
}
#endif // !os(visionOS)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment