Files
Convention-Unity-Demo/Assets/Scripts/Volume/BaseVolume.cs
2025-12-15 17:20:55 +08:00

105 lines
3.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Convention;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace Demo.Game
{
namespace ConfigType
{
// BaseVolume 配置抽象基类Config
public class BaseVolumeConfig : ScriptLoadableConfig
{
public override void Deserialize(BinaryReader reader)
{
base.Deserialize(reader);
}
public override void Serialize(BinaryWriter writer)
{
base.Serialize(writer);
}
}
}
public abstract class BaseVolume : ScriptableObject
{
[Resources, SerializeField] private Volume m_volume;
protected Volume MyVolume
{
get
{
if (m_volume == null)
m_volume = this.GetOrAddComponent<Volume>();
return m_volume;
}
}
private static readonly Dictionary<string, Type> VolumeOverrideTypes = new()
{
{ nameof(MotionBlur), typeof(MotionBlur)}
};
public bool HasOverride<T>() where T : VolumeComponent
{
return MyVolume.profile.Has<T>();
}
public bool HasOverride(string name)
{
if (VolumeOverrideTypes.TryGetValue(name, out var type))
return MyVolume.profile.Has(type);
return false;
}
public T GetOverride<T>() where T : VolumeComponent
{
if (MyVolume.profile.TryGet<T>(out var result))
return result;
return null;
}
public VolumeComponent GetOverride(string name)
{
if (VolumeOverrideTypes.TryGetValue(name, out var type))
if (MyVolume.profile.TryGet<VolumeComponent>(type, out var result))
return result;
return null;
}
public bool TrySetOverrideField<T, FieldType>(string name, FieldType value) where T : VolumeComponent
{
var component = GetOverride<T>();
if (component == null)
return false;
return ConventionUtility.PushValue(component, value, name, BindingFlags.Public | BindingFlags.Instance);
}
public bool TrySetOverrideField<FieldType>(string type, string name, FieldType value)
{
var component = GetOverride(type);
if (component == null)
return false;
return ConventionUtility.PushValue(component, value, name, BindingFlags.Public | BindingFlags.Instance);
}
}
public abstract class BaseForSingleVolume<T> : BaseVolume where T : VolumeComponent
{
private T m_TargetOverride;
public T TargetOverride
{
get
{
if (m_TargetOverride == null)
m_TargetOverride = GetOverride<T>();
return TargetOverride;
}
}
}
}