Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Merged by Bors] - bevy_asset: Add AssetServerSettings watch_for_changes member #3643

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion crates/bevy_asset/src/asset_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ impl AssetServer {
loaders.push(Arc::new(loader));
}

/// Enable watching of the filesystem for changes, if support is available, starting from after
/// the point of calling this function.
pub fn watch_for_changes(&self) -> Result<(), AssetServerError> {
self.server.asset_io.watch_for_changes()?;
Ok(())
Expand Down Expand Up @@ -622,7 +624,7 @@ mod test {
handle_to_path: Default::default(),
asset_lifecycles: Default::default(),
task_pool: Default::default(),
asset_io: Box::new(FileAssetIo::new(asset_path)),
asset_io: Box::new(FileAssetIo::new(asset_path, false)),
}),
}
}
Expand Down
18 changes: 16 additions & 2 deletions crates/bevy_asset/src/io/file_asset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,26 @@ pub struct FileAssetIo {
}

impl FileAssetIo {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
FileAssetIo {
pub fn new<P: AsRef<Path>>(path: P, watch_for_changes: bool) -> Self {
let file_asset_io = FileAssetIo {
#[cfg(feature = "filesystem_watcher")]
filesystem_watcher: Default::default(),
root_path: Self::get_root_path().join(path.as_ref()),
};
if watch_for_changes {
#[cfg(any(
not(feature = "filesystem_watcher"),
target_arch = "wasm32",
target_os = "android"
))]
panic!(
"Watch for changes requires the filesystem_watcher feature and cannot be used on \
wasm32 / android targets"
);
#[cfg(feature = "filesystem_watcher")]
file_asset_io.watch_for_changes().unwrap();
}
file_asset_io
}

pub fn get_root_path() -> PathBuf {
Expand Down
6 changes: 5 additions & 1 deletion crates/bevy_asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ pub struct AssetPlugin;

pub struct AssetServerSettings {
pub asset_folder: String,
/// Whether to watch for changes in asset files. Requires the `filesystem_watcher` feature,
/// and cannot be supported on the wasm32 arch nor android os.
pub watch_for_changes: bool,
}

impl Default for AssetServerSettings {
fn default() -> Self {
Self {
asset_folder: "assets".to_string(),
watch_for_changes: false,
}
}
}
Expand All @@ -64,7 +68,7 @@ pub fn create_platform_default_asset_io(app: &mut App) -> Box<dyn AssetIo> {
.get_resource_or_insert_with(AssetServerSettings::default);

#[cfg(all(not(target_arch = "wasm32"), not(target_os = "android")))]
let source = FileAssetIo::new(&settings.asset_folder);
let source = FileAssetIo::new(&settings.asset_folder, settings.watch_for_changes);
#[cfg(target_arch = "wasm32")]
let source = WasmAssetIo::new(&settings.asset_folder);
#[cfg(target_os = "android")]
Expand Down
10 changes: 6 additions & 4 deletions examples/asset/hot_asset_reloading.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
use bevy::prelude::*;
use bevy::{asset::AssetServerSettings, prelude::*};

/// Hot reloading allows you to modify assets on disk and they will be "live reloaded" while your
/// game is running. This lets you immediately see the results of your changes without restarting
/// the game. This example illustrates hot reloading mesh changes.
fn main() {
App::new()
// Tell the asset server to watch for asset changes on disk:
.insert_resource(AssetServerSettings {
watch_for_changes: true,
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.run();
Expand All @@ -14,9 +19,6 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Load our mesh:
let scene_handle = asset_server.load("models/monkey/Monkey.gltf#Scene0");

// Tell the asset server to watch for asset changes on disk:
asset_server.watch_for_changes().unwrap();

// Any changes to the mesh will be reloaded automatically! Try making a change to Monkey.gltf.
// You should see the changes immediately show up in your app.

Expand Down