首页 > 解决方案 > 如何重用加载在紫水晶中的资产?

问题描述

我正在Amethyst中构建一个简单的按钮,现在我想重用font_handle用于加载资产的按钮。这是我当前的代码:

pub fn create_button(world: &mut World) {
    let font_handle = world.read_resource::<Loader>().load(
        "fonts/Bangers-Regular.ttf",
        TtfFormat,
        (),
        &world.read_resource(),
    );
    
// ....


   world
        .create_entity()
        // ....
            .with(UiTransform::new(
            String::from("simple_button"), // id
            Anchor::Middle,                // anchor
            Anchor::Middle,                // pivot
            40.0,
            -40.,
            1.,
            200.,
            50.,
        ))
        // button text
        .with(UiText::new(
            font_handle,                   // font
            String::from("Simple Button"), // text
            [1.0, 1.0, 1.0, 0.5],          // color
            25f32,                         // font_size
            LineMode::Single,              // line mode
            Anchor::Middle,                // alignment
        ))
        .with(UiTransform::new(
            String::from("simple_button_2"), // id
            Anchor::Middle,                // anchor
            Anchor::Middle,                // pivot
            50.0,
            -40.,
            1.,
            200.,
            50.,
        ))
        // button text
        .with(UiText::new(
            font_handle,                   // font
            String::from("Simple Button 2"), // text
            [1.0, 1.0, 1.0, 0.5],          // color
            25f32,                         // font_size
            LineMode::Single,              // line mode
            Anchor::Middle,                // alignment
        ))
        .build();

我得到的错误非常明确,我的直觉说它与指针和克隆有关 - 就像我们对紫水晶中的大多数对象所做的那样。我试过了,但没有用。这是错误消息:

error[E0382]: use of moved value: `font_handle`
   --> src/state.rs:188:13
    |
135 |     let font_handle = world.read_resource::<Loader>().load(
    |         ----------- move occurs because `font_handle` has type `amethyst::amethyst_assets::Handle<FontAsset>`, which does not implement the `Copy` trait
...
169 |             font_handle,                   // font
    |             ----------- value moved here
...
188 |             font_handle,                   // font
    |             ^^^^^^^^^^^ value used here after move

我能想到的另一种方法是制作自定义资产加载器 - 这是唯一的方法还是有更简单的方法来实现重用?

标签: rustamethyst

解决方案


推荐阅读