using System;
namespace UnityEngine.Rendering
{
///
/// The attribute specifies paths for loading or reloading resources and has no direct action.
/// Used with the to define where to load data for null fields.
///
///
/// This attribute is designed for use in the Unity Editor and has no effect at runtime.
///
/// have their own attribute to do this.
/// When using them, resource reloading is handled automatically by the engine and does not require calling ResourceReloader.
///
/// While ResourceReloader was originally created for handling Scriptable Render Pipeline (SRP) resources, it has been replaced by .
/// The , and remain available for for user-defined assets.
///
///
///
///
/// This shows how to use the attribute in the expected scenario. This is particularly useful for content creators.
/// Adding a new field to a class that defines an asset results in null values for existing instances missing the field in their serialized data. Therefore, when a new field is added, a system for reloading null values may be necessary.
///
///using UnityEngine;
///using UnityEditor;
///
///public class MyResourcesAsset : ScriptableObject
///{
/// [Reload("Shaders/Blit.shader")]
/// public Shader blit;
///
/// // Added in version 2
/// [Reload("Shaders/betterBlit.shader")]
/// public Shader betterBlit;
///}
///
///public static class MyResourceHandler
///{
/// public static MyResourcesAsset GetAndReload()
/// {
/// var resources = AssetDatabase.LoadAssetAtPath<MyResourcesAsset>("MyResources.asset");
///
/// // Ensure that update of the data layout of MyResourcesAsset
/// // will not result in null value for asset already existing.
/// // (e.g.: added betterBlit in the case above)
/// ResourceReloader.ReloadAllNullIn(resources, "Packages/com.my-custom-package/");
/// return resources;
/// }
///}
///
///
[AttributeUsage(AttributeTargets.Field)]
public sealed class ReloadAttribute : Attribute
{
///
/// Lookup method for a resource.
///
public enum Package
{
///
/// Used for builtin resources when the resource isn't part of the package (i.e. builtin
/// shaders).
///
Builtin,
///
/// Used for resources inside the package.
///
Root,
///
/// Used for builtin extra resources when the resource isn't part of the package (i.e. builtin
/// extra Sprite).
///
BuiltinExtra,
};
#if UNITY_EDITOR
///
/// The lookup method.
///
public readonly Package package;
///
/// Search paths.
///
public readonly string[] paths;
#endif
///
/// Creates a new for an array by specifying each resource
/// path individually.
///
/// Search paths
/// The lookup method
///
/// This example demonstrates how to handle arrays with different resource paths.
///
///using UnityEngine;
///
///public class MyResourcesAsset : ScriptableObject
///{
/// [ResourcePaths(new[]
/// {
/// "Texture/FilmGrain/Thin.png",
/// "Texture/FilmGrain/Medium.png",
/// "Texture/FilmGrain/Large.png",
/// })]
/// public Texture[] filmGrains;
///}
///
///
public ReloadAttribute(string[] paths, Package package = Package.Root)
{
#if UNITY_EDITOR
this.paths = paths;
this.package = package;
#endif
}
///
/// Creates a new for a single resource.
///
/// Search path
/// The lookup method
///
/// This example shows how to directly specify the path of an asset.
///
///using UnityEngine;
///
///public class MyResourcesAsset : ScriptableObject
///{
/// [Reload("Shaders/Blit.shader")]
/// public Shader blit;
///}
///
///
public ReloadAttribute(string path, Package package = Package.Root)
: this(new[] { path }, package)
{ }
///
/// Creates a new for an array using automatic path name
/// numbering.
///
/// The format used for the path
/// The array start index (inclusive)
/// The array end index (exclusive)
/// The lookup method
///
/// This example demonstrates handling arrays with resource paths that share a common format, differing only by an index.
///
///using UnityEngine;
///
///public class MyResourcesAsset : ScriptableObject
///{
/// // The following will seek for resources:
/// // - Texture/FilmGrain/Thin1.png
/// // - Texture/FilmGrain/Thin2.png
/// // - Texture/FilmGrain/Thin3.png
/// [ResourcePaths("Texture/FilmGrain/Thin{0}.png", 1, 4)]
/// public Texture[] thinGrains;
///}
///
///
public ReloadAttribute(string pathFormat, int rangeMin, int rangeMax,
Package package = Package.Root)
{
#if UNITY_EDITOR
this.package = package;
paths = new string[rangeMax - rangeMin];
for (int index = rangeMin, i = 0; index < rangeMax; ++index, ++i)
paths[i] = string.Format(pathFormat, index);
#endif
}
}
}