|
| 1 | +--- |
| 2 | +title: Registering asset sources |
| 3 | +pull_requests: [] |
| 4 | +--- |
| 5 | + |
| 6 | +In previous versions, asset sources had to be registered **before** adding the `AssetPlugin` (which |
| 7 | +usually meant before adding the `DefaultPlugins`). Now, asset sources must be registered **after** |
| 8 | +`AssetPlugin` (and in effect, after `DefaultPlugins`). So if you had: |
| 9 | + |
| 10 | +```rust |
| 11 | +App::new() |
| 12 | + .register_asset_source("my_source", |
| 13 | + AssetSource::build() |
| 14 | + .with_reader(move || Box::new(todo!())) |
| 15 | + ) |
| 16 | + .add_plugins(DefaultPlugins) |
| 17 | + .run(); |
| 18 | +``` |
| 19 | + |
| 20 | +Now, it will be: |
| 21 | + |
| 22 | +```rust |
| 23 | +App::new() |
| 24 | + .add_plugins(DefaultPlugins) |
| 25 | + .register_asset_source("my_source", |
| 26 | + AssetSourceBuilder::new(move || Box::new(todo!())) |
| 27 | + ) |
| 28 | + .run(); |
| 29 | +``` |
| 30 | + |
| 31 | +Note: See also "Custom asset sources now require a reader" for changes to builders. |
| 32 | + |
| 33 | +In addition, default asset sources **can no longer be registered like custom sources**. There are |
| 34 | +two cases here: |
| 35 | + |
| 36 | +## 1. File paths |
| 37 | + |
| 38 | +The `AssetPlugin` will create the default asset source for a pair of file paths. Previously, this |
| 39 | +was written as: |
| 40 | + |
| 41 | +```rust |
| 42 | +App::new() |
| 43 | + .add_plugins(DefaultPlugins.set( |
| 44 | + AssetPlugin { |
| 45 | + file_path: "some/path".to_string(), |
| 46 | + processed_file_path: "some/processed_path".to_string(), |
| 47 | + ..Default::default() |
| 48 | + } |
| 49 | + )); |
| 50 | +``` |
| 51 | + |
| 52 | +Now, this is written as: |
| 53 | + |
| 54 | +```rust |
| 55 | +App::new() |
| 56 | + .add_plugins(DefaultPlugins.set( |
| 57 | + AssetPlugin { |
| 58 | + default_source: DefaultAssetSource::FromPaths { |
| 59 | + file_path: "some/path".to_string(), |
| 60 | + // Note: Setting this to None will just use the default path. |
| 61 | + processed_file_path: Some("some/processed_path".to_string()), |
| 62 | + }, |
| 63 | + ..Default::default() |
| 64 | + } |
| 65 | + )); |
| 66 | +``` |
| 67 | + |
| 68 | +## 2. Custom default source |
| 69 | + |
| 70 | +Users can also completely replace the default asset source to provide their own implementation. |
| 71 | +Previously, this was written as: |
| 72 | + |
| 73 | +```rust |
| 74 | +App::new() |
| 75 | + .register_asset_source( |
| 76 | + AssetSourceId::Default, |
| 77 | + AssetSource::build() |
| 78 | + .with_reader(move || Box::new(todo!())) |
| 79 | + ) |
| 80 | + .add_plugins(DefaultPlugins); |
| 81 | +``` |
| 82 | + |
| 83 | +Now, this is written as: |
| 84 | + |
| 85 | +```rust |
| 86 | +App::new() |
| 87 | + .add_plugins(DefaultPlugins.set( |
| 88 | + AssetPlugin { |
| 89 | + default_source: DefaultAssetSource::FromBuilder(Mutex::new( |
| 90 | + AssetSourceBuilder::new(move || Box::new(todo!())) |
| 91 | + )), |
| 92 | + ..Default::default() |
| 93 | + } |
| 94 | + )); |
| 95 | +``` |
0 commit comments