浏览代码

Revamp postprocessing default volume

/seans-add-link-xml
Andre McGrail 4 年前
当前提交
0be6da0e
共有 7 个文件被更改,包括 141 次插入46 次删除
  1. 1
      Assets/Scripts/Editor/QualitySettingsExtenstion.cs
  2. 23
      Assets/Scripts/GameSystem/AppSettings.cs
  3. 112
      Assets/Scripts/GameSystem/DefaultVolume.cs
  4. 1
      Assets/Scripts/System/Benchmark.cs
  5. 3
      Assets/Scripts/System/PerfomanceStats.cs
  6. 36
      Assets/Scripts/Utility/Utility.cs
  7. 11
      Assets/Scripts/Utility/Utility.cs.meta

1
Assets/Scripts/Editor/QualitySettingsExtenstion.cs


if (EditorGUI.EndChangeCheck() && displayAssetIndex != 0)
{
QualitySettings.SetQualityLevel(displayAssetIndex-1);
DefaultVolume.Instance.UpdateVolume();
}
if (displayAssetIndex != 0)

23
Assets/Scripts/GameSystem/AppSettings.cs


using System;
using System.Collections;
using System.Linq;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;

// Use this for initialization
private void OnEnable()
{
if(UniversalRenderPipeline.asset.debugLevel == PipelineDebugLevel.Profiling)
Debug.Log("AppManager initializing");
RenderPipelineManager.beginCameraRendering += SetRenderScale;
SetRenderScale();
SceneManager.sceneLoaded += LevelWasLoaded;
}

Application.targetFrameRate = 300;
MainCamera = Camera.main;
if(DefaultVolume.Instance == null)
StartCoroutine(LoadPrefab<GameObject>(volumeManager, new AsyncOperationHandle()));
}
private void Start()

private void OnDisable()
{
RenderPipelineManager.beginCameraRendering -= SetRenderScale;
SceneManager.sceneLoaded -= LevelWasLoaded;
}
private static void LevelWasLoaded(Scene scene, LoadSceneMode mode)

}
}
private void SetRenderScale(ScriptableRenderContext context, Camera cam)
private void SetRenderScale()
{
float res;
switch (maxRenderSize)

res = 2560f;
break;
default:
res = Screen.currentResolution.width;
res = Screen.width;
var renderScale = Mathf.Clamp(res / Screen.width, 0.1f, 1.0f);
if(UniversalRenderPipeline.asset.debugLevel == PipelineDebugLevel.Profiling)
Debug.Log($"Settings render scale to {renderScale * 100}% based on {maxRenderSize.ToString()}");
var renderScale = Mathf.Clamp(res / Screen.currentResolution.width, 0.1f, 1.0f);
maxScale = renderScale;
#if !UNITY_EDITOR
UniversalRenderPipeline.asset.renderScale = renderScale;

private void Update()
{
#if !UNITY_EDITOR
Utility.CheckQualityLevel(); //TODO - hoping to remove one day when we have a quality level callback
#endif
if (!MainCamera) return;
if (variableResolution)

112
Assets/Scripts/GameSystem/DefaultVolume.cs


using System.Collections;
using System;
using System.Collections;
using BoatAttack;
using UnityEngine.SceneManagement;
using Object = UnityEngine.Object;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
#endif
[ExecuteAlways]

public Volume volBaseComponent;
public Volume volQualityComponent;
public AssetReference[] qualityVolumes;
private int _currentQualityLevel;
private void Start()
{

if(Application.isPlaying)
DontDestroyOnLoad(gameObject);
_currentQualityLevel = QualitySettings.GetQualityLevel();
UpdateVolume();
SafeDestroy();
}
Utility.QualityLevelChange += UpdateVolume;
}
public static void SafeDestroy()
{
DestroyImmediate(gameObject);
return;
DestroyImmediate(Instance);
return;
Destroy(gameObject);
return;
Destroy(Instance);
return;
}
private void LateUpdate()
private void OnDestroy()
if (_currentQualityLevel != QualitySettings.GetQualityLevel())
{
_currentQualityLevel = QualitySettings.GetQualityLevel();
UpdateVolume();
}
Utility.QualityLevelChange -= UpdateVolume;
public void UpdateVolume()
public void UpdateVolume(int from, int to)
if (qualityVolumes?.Length > _currentQualityLevel && qualityVolumes[_currentQualityLevel] != null)
if (qualityVolumes?.Length > to && qualityVolumes[to] != null)
#if UNITY_EDITOR
LoadVolEditor(_currentQualityLevel);
#else
StartCoroutine(LoadAndApplyQualityVolume(_currentQualityLevel));
#endif
StartCoroutine(LoadAndApplyQualityVolume(to));
}
else
{

var obj = assetRef.editorAsset;
volQualityComponent.sharedProfile = obj as VolumeProfile;
}
#else
#endif
private IEnumerator LoadAndApplyQualityVolume(int index)
{
var volLoading = qualityVolumes[index].LoadAssetAsync<VolumeProfile>();

#endif
public class StartupVolume
internal class InjectDefaultVolume : IProcessSceneWithReport
public int callbackOrder => 1;
static StartupVolume()
static InjectDefaultVolume()
EditorApplication.delayCall += () =>
var vols = GameObject.FindGameObjectsWithTag("volume_manager");
foreach (var vol in vols)
var obj = AssetDatabase.LoadAssetAtPath("Assets/objects/misc/DefaultVolume.prefab", typeof(GameObject)) as GameObject;
if (obj == null) return;
if(UniversalRenderPipeline.asset.debugLevel != PipelineDebugLevel.Disabled)
Debug.Log($"Creating Volume Manager");
_vol = Object.Instantiate(obj);
_vol.hideFlags = HideFlags.HideAndDontSave;
};
Object.DestroyImmediate(vol);
}
EditorApplication.delayCall += () => { CreateVolumeManager(HideFlags.HideAndDontSave); };
EditorApplication.playModeStateChanged += StateChange;
}
private static void StateChange(PlayModeStateChange obj)
{
switch (obj)
{
case PlayModeStateChange.EnteredEditMode:
if(DefaultVolume.Instance == null)
CreateVolumeManager(HideFlags.HideAndDontSave);
break;
case PlayModeStateChange.ExitingEditMode:
if (DefaultVolume.Instance != null)
DefaultVolume.SafeDestroy();
break;
case PlayModeStateChange.EnteredPlayMode:
break;
case PlayModeStateChange.ExitingPlayMode:
break;
default:
throw new ArgumentOutOfRangeException(nameof(obj), obj, null);
}
}
public void OnProcessScene(Scene scene, BuildReport report)
{
if(scene.buildIndex != 0)
return;
Debug.Log($"Injecting Default volume into scene:{scene.name}");
CreateVolumeManager();
}
private static void CreateVolumeManager(HideFlags flags = HideFlags.None)
{
var obj =
AssetDatabase.LoadAssetAtPath("Assets/objects/misc/DefaultVolume.prefab", typeof(GameObject)) as
GameObject;
if (obj == null) return;
if (UniversalRenderPipeline.asset.debugLevel != PipelineDebugLevel.Disabled)
Debug.Log($"Creating Volume Manager");
_vol = Object.Instantiate(obj);
_vol.hideFlags = flags;
#endif

1
Assets/Scripts/System/Benchmark.cs


private static void Cleanup(PlayModeStateChange state)
{
Debug.Log("statechange");
if (state == PlayModeStateChange.EnteredEditMode)
{
var go = GameObject.Find("BenchmarkManager");

3
Assets/Scripts/System/PerfomanceStats.cs


public int runLength = 2000;
// Frame time stats
private List<PerfBasic> _stats = new List<PerfBasic>();
private List<PerfBasic> _stats;
public PerfBasic Stats => _stats[_runNumber - 1];
private List<float> samples = new List<float>();
private int totalSamples = 250;

private void OnEnable()
{
_stats = new List<PerfBasic>();
_stats.Add(new PerfBasic(runLength));
}

36
Assets/Scripts/Utility/Utility.cs


#if UNITY_EDITOR
using UnityEditor;
#endif
using System;
using UnityEngine;
namespace BoatAttack
{
#if UNITY_EDITOR
[InitializeOnLoad]
#endif
public class Utility
{
public static event Action<int, int> QualityLevelChange;
private static int lastQualityLevel = -1;
#if UNITY_EDITOR
static Utility()
{
// setup the things
Debug.Log("Setting up some utilities");
EditorApplication.update += CheckQualityLevel;
}
#endif
public static void CheckQualityLevel()
{
var curLevel = QualitySettings.GetQualityLevel();
if (lastQualityLevel == curLevel) return;
Debug.Log($"Quality level changed:{lastQualityLevel} to {curLevel}");
QualityLevelChange?.Invoke(lastQualityLevel, curLevel);
lastQualityLevel = curLevel;
}
}
}

11
Assets/Scripts/Utility/Utility.cs.meta


fileFormatVersion: 2
guid: 6644d24e743e4944cb63bd5acc037a43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存