Skip to content

Commit 51f3ff1

Browse files
committed
Update all the examples to handle asset locking.
1 parent 8e4a63d commit 51f3ff1

9 files changed

+20
-13
lines changed

examples/3d/animated_material.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn animate_materials(
5454
mut materials: ResMut<Assets<StandardMaterial>>,
5555
) {
5656
for material_handle in material_handles.iter() {
57-
if let Some(material) = materials.get_mut(material_handle) {
57+
if let Ok(material) = materials.get_mut(material_handle) {
5858
if let Color::Hsla(ref mut hsla) = material.base_color {
5959
*hsla = hsla.rotate_hue(time.delta_seconds() * 100.0);
6060
}

examples/3d/parallax_mapping.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,9 @@ fn update_parallax_depth_scale(
9494
}
9595
if *depth_update {
9696
let mut text = text.single_mut();
97-
for (_, mat) in materials.iter_mut() {
97+
for (_, mut mat) in materials.iter_mut() {
98+
let mat = mat.as_mut().expect("The material is unlocked");
99+
98100
let current_depth = mat.parallax_depth_scale;
99101
let new_depth = current_depth.lerp(target_depth.0, DEPTH_CHANGE_RATE);
100102
mat.parallax_depth_scale = new_depth;
@@ -120,7 +122,8 @@ fn switch_method(
120122
let mut text = text.single_mut();
121123
text.sections[2].value = format!("Method: {}\n", *current);
122124

123-
for (_, mat) in materials.iter_mut() {
125+
for (_, mut mat) in materials.iter_mut() {
126+
let mat = mat.as_mut().expect("The material is unlocked");
124127
mat.parallax_mapping_method = current.0;
125128
}
126129
}
@@ -143,7 +146,8 @@ fn update_parallax_layers(
143146
let mut text = text.single_mut();
144147
text.sections[1].value = format!("Layers: {layer_count:.0}\n");
145148

146-
for (_, mat) in materials.iter_mut() {
149+
for (_, mut mat) in materials.iter_mut() {
150+
let mat = mat.as_mut().expect("The material is unlocked");
147151
mat.max_parallax_layer_count = layer_count;
148152
}
149153
}

examples/3d/tonemapping.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn drag_drop_image(
230230
};
231231

232232
for mat_h in &image_mat {
233-
if let Some(mat) = materials.get_mut(mat_h) {
233+
if let Ok(mat) = materials.get_mut(mat_h) {
234234
mat.base_color_texture = Some(new_image.clone());
235235

236236
// Despawn the image viewer instructions

examples/3d/transparency_3d.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ fn setup(
117117
/// 6/8 opaque, then 5/8, etc.
118118
pub fn fade_transparency(time: Res<Time>, mut materials: ResMut<Assets<StandardMaterial>>) {
119119
let alpha = (ops::sin(time.elapsed_seconds()) / 2.0) + 0.5;
120-
for (_, material) in materials.iter_mut() {
120+
for (_, mut material) in materials.iter_mut() {
121+
let material = material.as_mut().expect("The material is unlocked");
121122
material.base_color.set_alpha(alpha);
122123
}
123124
}

examples/animation/animation_masks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ fn handle_button_toggles(
341341
// iterate just for clarity's sake.)
342342
for (animation_graph_handle, animation_player) in animation_players.iter_mut() {
343343
// The animation graph needs to have loaded.
344-
let Some(animation_graph) = animation_graphs.get_mut(animation_graph_handle) else {
344+
let Ok(animation_graph) = animation_graphs.get_mut(animation_graph_handle) else {
345345
continue;
346346
};
347347

examples/asset/alter_mesh.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn alter_mesh(
203203
};
204204

205205
// Obtain a mutable reference to the Mesh asset.
206-
let Some(mesh) = meshes.get_mut(handle) else {
206+
let Ok(mesh) = meshes.get_mut(handle) else {
207207
return;
208208
};
209209

examples/asset/alter_sprite.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ fn alter_asset(mut images: ResMut<Assets<Image>>, left_bird: Query<&Handle<Image
150150
};
151151

152152
// Obtain a mutable reference to the Image asset.
153-
let Some(image) = images.get_mut(handle) else {
153+
let Ok(image) = images.get_mut(handle) else {
154154
return;
155155
};
156156

examples/asset/asset_decompression.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use bevy::{
44
asset::{
55
io::{Reader, VecReader},
6-
AssetLoader, ErasedLoadedAsset, LoadContext, LoadDirectError,
6+
AssetLoader, AssetPresence, ErasedLoadedAsset, LoadContext, LoadDirectError,
77
},
88
prelude::*,
99
reflect::TypePath,
@@ -124,8 +124,10 @@ fn decompress<A: Asset>(
124124
query: Query<(Entity, &Compressed<A>)>,
125125
) {
126126
for (entity, Compressed { compressed, .. }) in query.iter() {
127-
let Some(GzAsset { uncompressed }) = compressed_assets.remove(compressed) else {
128-
continue;
127+
let GzAsset { uncompressed } = match compressed_assets.remove(compressed) {
128+
AssetPresence::None => continue,
129+
AssetPresence::Unlocked(asset) => asset,
130+
AssetPresence::Locked(_) => panic!("this example does not lock assets"),
129131
};
130132

131133
let uncompressed = uncompressed.take::<A>().unwrap();

examples/ui/ui_material.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ fn animate(
8989
) {
9090
let duration = 2.0;
9191
for handle in &q {
92-
if let Some(material) = materials.get_mut(handle) {
92+
if let Ok(material) = materials.get_mut(handle) {
9393
// rainbow color effect
9494
let new_color = Color::hsl((time.elapsed_seconds() * 60.0) % 360.0, 1., 0.5);
9595
let border_color = Color::hsl((time.elapsed_seconds() * 60.0) % 360.0, 0.75, 0.75);

0 commit comments

Comments
 (0)