I Have a multiple if else statements under another if else statements and I want to be able to fold all child statements under certain parent without have to fold each one separately cause its like 100's of them example:
This Is Unfolded
func example(v1: String, v2: Int, v3: Int)
{
if v1 == "Food"
{
if v2 == 2 && v3 == 7
{
// Some Long Lines Codes
}
else if v2 == 2 && v3 == 33
{
// Some Long Lines Codes
}
else if v2 == 44 && v3 == 72
{
// Some Long Lines Codes
}
else if v2 == 12 && v3 == 7
{
// Some Long Lines Codes
}
else if v2 == 102 && v3 == 97
{
// Some Long Lines Codes
}
}
}
And I Wanted To Be Folded Like That With A Simple Click Not Fold It One By One
func example(v1: String, v2: Int, v3: Int)
{
if v1 == "Food"
{
if v2 == 2 && v3 == 7
{...}
else if v2 == 2 && v3 == 33
{...}
else if v2 == 44 && v3 == 72
{...}
else if v2 == 12 && v3 == 7
{...}
else if v2 == 102 && v3 == 97
{...}
}
}
Imaging like 100 lines of those else if conditions that i need to fold each one manually using CMD + Opt + <- or even using the arrow beside, I hope there is a fast and reliable shortcut or a click to help folding child codes or selected codes
btw I know there is a cmd + opt + shift + <- to fold all but that's not helping cause when I unfold the function I would see all remains as it was before.
I hope anyone could have a clue or if there is an ability to add a script function to do so.
Post
Replies
Boosts
Views
Activity
Im searching for a simple swiftUI code that can move a cube in immersive space when I click the X button on Sony Playstation Game Controller,
I did load a USD entity and connect the game controller but when it comes to get a simple click into the reality view to change position or rotate the entity it always not even accepted in codes. although I can control it with a finger or hand gestures, and yes I have closed the "Send Game Controller to device" in the simulator to make sure the controller would not effect the system but only effect in the game,
here is a sample
import SwiftUI
import RealityKit
import GameController
struct ImmersiveView: View {
@State var entity = ModelEntity()
@State var z: Float = -4.0
var body: some View {
RealityView { content in
if let cube = try? await ModelEntity(named: "am.usdz") {
entity = cube
entity.generateCollisionShapes(recursive: true)
entity.name = "Cube"
entity.position = [0, 0, -4]
entity.components.set(InputTargetComponent(allowedInputTypes: .indirect))
entity.components.set(HoverEffectComponent())
content.add(entity)
}
}
.gesture(
SpatialTapGesture()
.targetedToEntity(entity)
.onEnded { value in
print(value)
z -= 1.0
entity.position = [0, 0, z]
}
)
// What to do here to make game controller button X act as the gesture do
}
}
How can i play a USDZ entity animation in reverse? I have tried to put a negative value to the speed as I was doing in SceneKit to make the animation reverse play but it did not work. here is my code:
import SwiftUI
import RealityKit
struct ImmersiveView: View {
@State var entity = Entity()
@State var openDoor: Bool = true
var body: some View {
RealityView { content in
if let mainDoor = try? await Entity(named: "Door.usdz") {
if let frame = mainDoor.findEntity(named: "DoorFrame")
{
frame.position = [0, 0, -8]
frame.orientation = simd_quatf(angle: (270 * (.pi / 180)), axis: SIMD3(x: 1, y: 0, z: 0))
content.add(frame)
entity = frame.findEntity(named: "Door")!
entity.components.set(InputTargetComponent(allowedInputTypes: .indirect))
entity.components.set(HoverEffectComponent())
let entityModel = entity.children[0]
entityModel.generateCollisionShapes(recursive: true)
}
}
}
.gesture(
SpatialTapGesture()
.targetedToEntity(entity)
.onEnded { value in
print(value)
if openDoor == true
{
let animController = entity.playAnimation(entity.availableAnimations[0], transitionDuration: 0 , startsPaused: true)
animController.speed = 1.0
animController.resume()
openDoor = false
}
else
{
let animController = entity.playAnimation(entity.availableAnimations[0], transitionDuration: 0 , startsPaused: true)
animController.speed = -1.0 // it does not work to reverse
animController.resume()
openDoor = true
}
}
)
}
}
The Door should open with first click which is already happening and close with second click which is not happening as it does not reverse play the animation
I have a quiet big USDZ file which have my 3d model that I run on Realityview Swift Project and it takes sometime before I can see the model on the screen, So I was wondering if there is a way to know how much time left for the RealityKit/RealityView Model to be loaded or a percentage that I can add on a progress bar to show for the user how much time left before he can see the full model on screen. and if there is a way how to do this on progress bar while loading.
Something like that
I have an app which have an Immersive Space view and it needs the user to have a button in the bottom which have a fixed place in front of the user head like a dashboard in game or so but when the user get too close to any3d object in the view it could cover the button and make it inaccessible and it mainly would prevent the app for being approved like that in appstoreconnect I was working before on SceneKit and there was something like camera view Znear and Zfar which decide when to hide the 3d model if it comes too close or gets too far and I wonder if there is something like that in realityView / RealityKit 4.
Here is My Code and the screenshots follows
import SwiftUI
import RealityKit
struct ContentView: View {
@State var myHead: Entity = {
let headAnchor = AnchorEntity(.head)
headAnchor.position = [-0.02, -0.023, -0.24]
return headAnchor
}()
@State var clicked = false
var body: some View {
RealityView { content, attachments in
// create a 3d box
let mainBox = ModelEntity(mesh: .generateBox(size: [0.1, 0.1, 0.1]))
mainBox.position = [0, 1.6, -0.3]
content.add(mainBox)
content.add(myHead)
guard let attachmentEntity = attachments.entity(for: "Dashboard") else {return}
myHead.addChild(attachmentEntity)
}
attachments: {
// SwiftUI Inside Immersivre View
Attachment(id: "Dashboard") {
VStack {
Spacer()
.frame(height: 300)
Button(action: {
goClicked()
}) {
Text(clicked ? "⏸️" : "▶️")
.frame(maxWidth: 48, maxHeight: 48, alignment: .center)
.font(.extraLargeTitle)
}
.buttonStyle(.plain)
}
}
}
}
func goClicked() {
clicked.toggle()
}
}
I have an issue using RealityView to show two screens of AR, while I did succeed to make it as a non AR but now my code not working.
Also it is working using Storyboard and Swift with SceneKit, so why it is not working in RealityView?
import SwiftUI
import RealityKit
struct ContentView : View {
var body: some View {
HStack (spacing: 0){
MainView()
MainView()
}
.background(.black)
}
}
struct MainView : View {
@State var anchor = AnchorEntity()
var body: some View {
RealityView { content in
let item = ModelEntity(mesh: .generateBox(size: 0.2), materials: [SimpleMaterial()])
content.camera = .spatialTracking
anchor.addChild(item)
anchor.position = [0.0, 0.0, -1.0]
anchor.orientation = .init(angle: .pi/4, axis:[0,1,1])
// Add the horizontal plane anchor to the scene
content.add(anchor)
}
}
}
I have this code to make ARVR Stereo View To Be Used in VR Box Or Google Cardboard, it uses iOS 18 New RealityView but for some reason the left side showing the Entity (Box) more near to the camera than the right side which make it not identical, I wonder is this a bug and need to be fixed or what ? thanx
Here is the code
import SwiftUI
import RealityKit
struct ContentView : View {
let anchor1 = AnchorEntity(.camera)
let anchor2 = AnchorEntity(.camera)
var body: some View {
HStack (spacing: 0){
MainView(anchor: anchor1)
MainView(anchor: anchor2)
}
.background(.black)
}
}
struct MainView : View {
@State var anchor = AnchorEntity()
var body: some View {
RealityView { content in
content.camera = .spatialTracking
let item = ModelEntity(mesh: .generateBox(size: 0.25), materials: [SimpleMaterial()])
anchor.addChild(item)
content.add(anchor)
anchor.position.z = -1.0
anchor.orientation = .init(angle: .pi/4, axis:[0,1,1])
}
}
}
And Here is the View
I have this code to make ARVR Stereo View To Be Used in VR Box Or Google Cardboard, it uses iOS 18 New RealityView but it is not Act as an AR but rather Static VR on a Camera background so as I move the iPhone the cube move with it and that's not suppose to happen if its Anchored in a plane or to world coordinate.
import SwiftUI
import RealityKit
struct ContentView : View {
let anchor1 = AnchorEntity(.camera)
let anchor2 = AnchorEntity(.camera)
var body: some View {
HStack (spacing: 0){
MainView(anchor: anchor1)
MainView(anchor: anchor2)
}
.background(.black)
}
}
struct MainView : View {
@State var anchor = AnchorEntity()
var body: some View {
RealityView { content in
content.camera = .spatialTracking
let item = ModelEntity(mesh: .generateBox(size: 0.25), materials: [SimpleMaterial()])
anchor.addChild(item)
content.add(anchor)
anchor.position.z = -1.0
anchor.orientation = .init(angle: .pi/4, axis:[0,1,1])
}
}
}
the thing is if I remove .camera like this
let anchor1 = AnchorEntity()
let anchor2 = AnchorEntity()
It would work as AR Anchored to world coordinates but on the other hand is does not work but on the left view only not both views
Meanwhile this was so easy before RealityView and SwiftUI by cloning the view like in ARSCNView Example :
import UIKit
import ARKit
class ViewController: UIViewController, ARSCNViewDelegate, ARSessionDelegate {
//create Any Two ARSCNView's in Story board
// and link each to the next (dont mind dimensions)
@IBOutlet var sceneView: ARSCNView!
@IBOutlet var sceneView2: ARSCNView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
sceneView.delegate = self
sceneView.session.delegate = self
// Create SceneKit box
let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0.01)
let item = SCNNode(geometry: box)
item.geometry?.materials.first?.diffuse.contents = UIColor.green
item.position = SCNVector3(0.0, 0.0, -1.0)
item.orientation = SCNVector4(0, 1, 1, .pi/4.0)
// retrieve the ship node
sceneView.scene.rootNode.addChildNode(item)
}
override func viewDidLayoutSubviews() // To Do Add the 4 Buttons
{
// Stop Screen Dimming or Closing While The App Is Running
UIApplication.shared.isIdleTimerDisabled = true
let screen: CGRect = UIScreen.main.bounds
let topPadding: CGFloat = self.view.safeAreaInsets.top
let bottomPadding: CGFloat = self.view.safeAreaInsets.bottom
let leftPadding: CGFloat = self.view.safeAreaInsets.left
let rightPadding: CGFloat = self.view.safeAreaInsets.right
let safeArea: CGRect = CGRect(x: leftPadding, y: topPadding, width: screen.size.width - leftPadding - rightPadding, height: screen.size.height - topPadding - bottomPadding)
DispatchQueue.main.async
{
if self.sceneView != nil
{
self.sceneView.frame = CGRect(x: safeArea.size.width * 0 + safeArea.origin.x, y: safeArea.size.height * 0 + safeArea.origin.y, width: safeArea.size.width * 0.5, height: safeArea.size.height * 1)
}
if self.sceneView2 != nil
{
self.sceneView2.frame = CGRect(x: safeArea.size.width * 0.5 + safeArea.origin.x, y: safeArea.size.height * 0 + safeArea.origin.y, width: safeArea.size.width * 0.5, height: safeArea.size.height * 1)
}
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let configuration = ARWorldTrackingConfiguration()
sceneView.session.run(configuration)
sceneView2.scene = sceneView.scene
sceneView2.session = sceneView.session
}
}
And here is the video for it