bevy/examples/asset/extra_source.rs
Joona Aalto 25bfa80e60
Migrate cameras to required components (#15641)
# Objective

Yet another PR for migrating stuff to required components. This time,
cameras!

## Solution

As per the [selected
proposal](https://hackmd.io/tsYID4CGRiWxzsgawzxG_g#Combined-Proposal-1-Selected),
deprecate `Camera2dBundle` and `Camera3dBundle` in favor of `Camera2d`
and `Camera3d`.

Adding a `Camera` without `Camera2d` or `Camera3d` now logs a warning,
as suggested by Cart [on
Discord](https://discord.com/channels/691052431525675048/1264881140007702558/1291506402832945273).
I would personally like cameras to work a bit differently and be split
into a few more components, to avoid some footguns and confusing
semantics, but that is more controversial, and shouldn't block this core
migration.

## Testing

I ran a few 2D and 3D examples, and tried cameras with and without
render graphs.

---

## Migration Guide

`Camera2dBundle` and `Camera3dBundle` have been deprecated in favor of
`Camera2d` and `Camera3d`. Inserting them will now also insert the other
components required by them automatically.
2024-10-05 01:59:52 +00:00

49 lines
1.6 KiB
Rust

//! An example of registering an extra asset source, and loading assets from it.
//! This asset source exists in addition to the default asset source.
use bevy::{
asset::{
io::{AssetSourceBuilder, AssetSourceId},
AssetPath,
},
prelude::*,
};
use std::path::Path;
fn main() {
App::new()
// Add an extra asset source with the name "example_files" to
// AssetSourceBuilders.
//
// This must be done before AssetPlugin finalizes building assets.
.register_asset_source(
"example_files",
AssetSourceBuilder::platform_default("examples/asset/files", None),
)
// DefaultPlugins contains AssetPlugin so it must be added to our App
// after inserting our new asset source.
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2d);
// Now we can load the asset using our new asset source.
//
// The actual file path relative to workspace root is
// "examples/asset/files/bevy_pixel_light.png".
let path = Path::new("bevy_pixel_light.png");
let source = AssetSourceId::from("example_files");
let asset_path = AssetPath::from_path(path).with_source(source);
// You could also parse this URL-like string representation for the asset
// path.
assert_eq!(asset_path, "example_files://bevy_pixel_light.png".into());
commands.spawn(SpriteBundle {
texture: asset_server.load(asset_path),
..default()
});
}