SwiftUI 无法在实时活动中显示 SpriteView

问题描述 投票:0回答:0

我正在尝试在应用程序中显示精灵视图。我已经将它显示为主应用程序的视图,并且运行良好。当我尝试使用

将其添加为视图时出现问题
var characterView: some View {
        SpriteView(scene: characterScene)
            .frame(width: 50, height: 50)
    }

只是展示那个观点。

这里是一些最小的代码:

// Character Animation
import Foundation
import SpriteKit

class CharacterScene: SKScene {
    let characterName: String
    init(characterName: String) {
        self.characterName = characterName
        super.init(size: CGSize(width: 300, height: 400))
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    var textures: [SKTexture] = []
    override func didMove(to view: SKView) {
        let atlas = SKTextureAtlas(named: characterName)
        textures = atlas.textureNames.sorted().map { atlas.textureNamed($0) }
        
        let sprite = SKSpriteNode(texture: textures[0])
        let maxSize: CGFloat = 200
        let aspectRatio = sprite.size.height / sprite.size.width
        sprite.size = CGSize(width: min(sprite.size.width, maxSize), height: min(sprite.size.height, maxSize * aspectRatio))
        sprite.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(sprite)
        
        let animation = SKAction.animate(with: textures, timePerFrame: 0.5)
        sprite.run(SKAction.repeatForever(animation))
    }
}

//Widget

import WidgetKit
import SwiftUI
import ActivityKit
import SpriteKit

struct CharacterIslandWidget: Widget {
    var body: some WidgetConfiguration {
        ActivityConfiguration(
            for: CharacterIslandAttributes.self,
            content: { context in
                CharacterIslandWidgetView(context: context)
            },
            dynamicIsland: { context in
                DynamicIsland(
                    expanded: {
                        DynamicIslandExpandedRegion(.center) {
                            Text(context.state.CharacterName)
                        }
                        DynamicIslandExpandedRegion(.leading) {
                            SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                                .frame(width: 50, height: 50)
                        }
                    },
                    compactLeading: {
                        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                            .frame(width: 50, height: 50)
                    },
                    compactTrailing: {
                    },
                    minimal: {
                        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
                            .frame(width: 50, height: 50)
                    })
            }
    )}
}
struct CharacterIslandWidgetView: View {
    let context: ActivityViewContext<CharacterIslandAttributes>
    var body: some View {
        SpriteView(scene: CharacterScene(characterName: context.state.CharacterImage))
            .frame(width: 50, height: 50)
        Text(context.state.CharacterImage)
    }
}

我没有在日志中收到任何可执行错误,所以我不完全知道问题所在,但图像看起来像这样(我还没有为小部件设置预览)。

图集名称在图像下方给出并且是正确的,因为它在应用程序中运行得非常好。

一些额外的东西:

  • 资产文件夹在小部件和应用程序之间共享,并且它可以工作,因为它显示在应用程序上。
  • context.state.CharacterImage 是包含所有精灵的图集“文件夹”名称。

我该如何从这里着手呢? Live Activities(动态岛)中不允许使用精灵视图吗?

我尝试硬编码名称,并阅读错误日志,但没有任何显示。

swift swiftui sprite-kit widgetkit widgetliveactivity
© www.soinside.com 2019 - 2024. All rights reserved.