pub struct BuiltInResource<T>where
T: TypedResourceData,{
pub id: PathBuf,
pub data_source: Option<DataSource>,
pub resource: Resource<T>,
}Expand description
Built-in resource is a resource embedded in the application executable file. It is a very useful mechanism when you need to bundle all game resources and put them in the executable file.
§Registration
Every built-in resource must be registered in a resource manager to be accessible via standard
crate::manager::ResourceManager::request method. It could be done pretty easily:
use fyrox_resource::{
builtin::BuiltInResource,
core::{reflect::prelude::*, type_traits::prelude::*, visitor::prelude::*, Uuid},
manager::ResourceManager,
Resource, ResourceData,
};
use std::{error::Error, path::Path};
#[derive(TypeUuidProvider, Default, Debug, Clone, Visit, Reflect)]
#[type_uuid(id = "00d036bb-fbed-47f7-94e3-b3fce93dee17")]
struct MyResource {
some_data: String,
}
impl ResourceData for MyResource {
fn type_uuid(&self) -> Uuid {
<Self as TypeUuidProvider>::type_uuid()
}
fn save(&mut self, _path: &Path) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn can_be_saved(&self) -> bool {
false
}
fn try_clone_box(&self) -> Option<Box<dyn ResourceData>> {
Some(Box::new(self.clone()))
}
}
fn register_built_in_resource(resource_manager: &ResourceManager) {
let id = "MyResourceId";
let some_data = "This string is a built-in resource with MyResourceId id.";
let resource = Resource::new_embedded(MyResource {
some_data: some_data.into(),
});
resource_manager.register_built_in_resource(BuiltInResource::new_no_source(id, resource));
assert_eq!(
resource_manager
.request::<MyResource>(id)
.data_ref()
.some_data,
some_data,
)
}Fields§
§id: PathBufId of the built-in resource.
data_source: Option<DataSource>Initial data, from which the resource is created from.
resource: Resource<T>Ready-to-use (“loaded”) resource.
Implementations§
Source§impl<T: TypedResourceData> BuiltInResource<T>
impl<T: TypedResourceData> BuiltInResource<T>
Sourcepub fn new<F>(id: impl AsRef<Path>, data_source: DataSource, make: F) -> Self
pub fn new<F>(id: impl AsRef<Path>, data_source: DataSource, make: F) -> Self
Creates a new built-in resource with an id, a data source and function that creates the resource from the given data source.
Sourcepub fn new_no_source(id: impl AsRef<Path>, resource: Resource<T>) -> Self
pub fn new_no_source(id: impl AsRef<Path>, resource: Resource<T>) -> Self
Creates a new built-in resource from an id and arbitrary resource.
Trait Implementations§
Source§impl<T: TypedResourceData> Clone for BuiltInResource<T>
impl<T: TypedResourceData> Clone for BuiltInResource<T>
Source§impl<T: TypedResourceData> From<BuiltInResource<T>> for UntypedBuiltInResource
impl<T: TypedResourceData> From<BuiltInResource<T>> for UntypedBuiltInResource
Source§fn from(value: BuiltInResource<T>) -> Self
fn from(value: BuiltInResource<T>) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl<T> Freeze for BuiltInResource<T>
impl<T> !RefUnwindSafe for BuiltInResource<T>
impl<T> Send for BuiltInResource<T>
impl<T> Sync for BuiltInResource<T>where
T: Sync,
impl<T> Unpin for BuiltInResource<T>where
T: Unpin,
impl<T> UnsafeUnpin for BuiltInResource<T>
impl<T> !UnwindSafe for BuiltInResource<T>
Blanket Implementations§
Source§impl<T> AsyncTaskResult for T
impl<T> AsyncTaskResult for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
impl<T, U> ObjectOrVariant<T> for Uwhere
PhantomData<U>: ObjectOrVariantHelper<T, U>,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.