Last active
May 8, 2024 13:12
-
-
Save gonchar/09803c77fcca51551a64f194bd656985 to your computer and use it in GitHub Desktop.
visionOS Xcode snippets
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
// hello |
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
import Foundation | |
import SwiftUI | |
import RealityKit | |
import RealityKitContent | |
import Combine | |
@MainActor | |
final class <#RealityKitSceneController#>: ObservableObject, SceneControllerProtocol { | |
private var cancellabe:AnyCancellable?; | |
private lazy var controllerRoot:Entity = { | |
var result = Entity() | |
result.name = "<#RealityKitSceneController#>" | |
return result | |
}() | |
private var mainScene:Entity? | |
init() { | |
} | |
public func cleanup() { | |
cancellabe?.cancel() | |
cancellabe = nil | |
mainScene = nil | |
} | |
public func firstInit(_ content : inout RealityViewContent, attachments: RealityViewAttachments) async { | |
cancellabe = NotificationCenter.default.publisher(for: Notification.Name("INJECTION_BUNDLE_NOTIFICATION")) | |
.sink { _ in | |
Task { @MainActor in | |
await self.updateAfterInject() | |
} | |
} | |
content.add(controllerRoot) | |
_ = content.subscribe(to: SceneEvents.Update.self, on: nil) { event in | |
self.updateFrame(event) | |
} | |
await setupSceneFirstTime() | |
} | |
public func updateFrame(_ event: SceneEvents.Update) { | |
} | |
public func updateView(_ content : inout RealityViewContent, attachments: RealityViewAttachments) { | |
print("ssc::updateview") | |
} | |
public func onTapSpatial(_ targetValue: EntityTargetValue<SpatialTapGesture.Value>) { | |
} | |
func setupSceneFirstTime() async { | |
await updateAfterInject() | |
} | |
func updateAfterInject() async { | |
} | |
} |
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
import SwiftUI | |
import RealityKit | |
struct <#RealityKitView#><SceneController: SceneControllerProtocol>: View { | |
@State var realityKitSceneController:SceneController? | |
var body: some View { | |
RealityView { content, attachments in | |
self.realityKitSceneController = SceneController() | |
await realityKitSceneController?.firstInit(&content, attachments: attachments) | |
} update: { content, attachments in | |
realityKitSceneController?.updateView(&content, attachments: attachments) | |
} placeholder: { | |
ProgressView() | |
} attachments: { | |
let _ = print("--attachments") | |
Attachment(id: "emptyAttachment") { | |
} | |
} | |
.gesture(SpatialTapGesture() | |
.targetedToAnyEntity() | |
.onEnded({ targetValue in | |
realityKitSceneController?.onTapSpatial(targetValue) | |
}) | |
) | |
.onAppear { | |
// Appear happens before realitkit scene controller init | |
} | |
.onDisappear { | |
realityKitSceneController?.cleanup() | |
realityKitSceneController = nil; | |
} | |
} | |
} |
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
import Foundation | |
import RealityKit | |
import SwiftUI | |
@MainActor | |
protocol SceneControllerProtocol { | |
func firstInit(_ content : inout RealityViewContent, attachments: RealityViewAttachments) async | |
func updateView(_ content : inout RealityViewContent, attachments: RealityViewAttachments) | |
func cleanup() | |
func onTapSpatial(_ targetValue: EntityTargetValue<SpatialTapGesture.Value>) | |
init() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment