diff --git a/crates/bevy_asset/src/asset_server.rs b/crates/bevy_asset/src/asset_server.rs index 88f20511ab..0d8f1fe951 100644 --- a/crates/bevy_asset/src/asset_server.rs +++ b/crates/bevy_asset/src/asset_server.rs @@ -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(()) @@ -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)), }), } } diff --git a/crates/bevy_asset/src/io/file_asset_io.rs b/crates/bevy_asset/src/io/file_asset_io.rs index 1bd7f1515c..efe762b542 100644 --- a/crates/bevy_asset/src/io/file_asset_io.rs +++ b/crates/bevy_asset/src/io/file_asset_io.rs @@ -27,12 +27,26 @@ pub struct FileAssetIo { } impl FileAssetIo { - pub fn new>(path: P) -> Self { - FileAssetIo { + pub fn new>(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 { diff --git a/crates/bevy_asset/src/lib.rs b/crates/bevy_asset/src/lib.rs index 0067c266ce..b62d7537f2 100644 --- a/crates/bevy_asset/src/lib.rs +++ b/crates/bevy_asset/src/lib.rs @@ -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, } } } @@ -64,7 +68,7 @@ pub fn create_platform_default_asset_io(app: &mut App) -> Box { .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")] diff --git a/examples/asset/hot_asset_reloading.rs b/examples/asset/hot_asset_reloading.rs index 9216b10311..fc3cc5e8e7 100644 --- a/examples/asset/hot_asset_reloading.rs +++ b/examples/asset/hot_asset_reloading.rs @@ -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(); @@ -14,9 +19,6 @@ fn setup(mut commands: Commands, asset_server: Res) { // 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.