private static DynamicResolutionHandler GetOrCreateDrsInstanceHandler(Camera camera) { if (camera == null) { return(null); } DynamicResolutionHandler instance = null; var key = camera.GetInstanceID(); if (!s_CameraInstances.TryGetValue(key, out instance)) { //if this camera is not available in the map of cameras lets try creating one. //first and foremost, if we exceed the dictionary capacity, lets try and recycle an object that is dead. if (s_CameraInstances.Count >= CameraDictionaryMaxcCapacity) { int recycledInstanceKey = 0; DynamicResolutionHandler recycledInstance = null; foreach (var kv in s_CameraInstances) { //is this object dead? that is, belongs to a camera that was destroyed? if (kv.Value.m_OwnerCameraWeakRef == null || !kv.Value.m_OwnerCameraWeakRef.IsAlive) { recycledInstance = kv.Value; recycledInstanceKey = kv.Key; break; } } if (recycledInstance != null) { instance = recycledInstance; s_CameraInstances.Remove(recycledInstanceKey); s_CameraUpscaleFilters.Remove(recycledInstanceKey); } } //if we didnt find a dead object, we create one from scratch. if (instance == null) { instance = new DynamicResolutionHandler(); instance.m_OwnerCameraWeakRef = new WeakReference(camera); } else { //otherwise, we found a dead object, lets reset it, and have a weak ref to this camera, //so we can possibly recycle it in the future by checking the camera's weak pointer state. instance.Reset(); instance.m_OwnerCameraWeakRef.Target = camera; } s_CameraInstances.Add(key, instance); } return(instance); }
/// <summary> /// Update the state of the dynamic resolution system for a specific camera. /// Call this function also to switch context between cameras (will set the current camera as active). /// Passing a null camera has the same effect as calling Update without the camera parameter. /// </summary> /// <param name="camera">Camera used to select a specific instance tied to this DynamicResolutionHandler instance.</param> /// <param name="settings">(optional) The settings that are to be used by the dynamic resolution system. passing null for the settings will result in the last update's settings used.</param> /// <param name="OnResolutionChange">An action that will be called every time the dynamic resolution system triggers a change in resolution.</param> public static void UpdateAndUseCamera(Camera camera, GlobalDynamicResolutionSettings?settings = null, Action OnResolutionChange = null) { int newCameraId; if (camera == null) { s_ActiveInstance = s_DefaultInstance; newCameraId = 0; } else { s_ActiveInstance = GetOrCreateDrsInstanceHandler(camera); newCameraId = camera.GetInstanceID(); } s_ActiveInstanceDirty = newCameraId != s_ActiveCameraId; s_ActiveCameraId = newCameraId; s_ActiveInstance.Update(settings.HasValue ? settings.Value : s_ActiveInstance.m_CachedSettings, OnResolutionChange); }
/// <summary> /// Will clear the currently used camera. Use this function to restore the default instance when UpdateAndUseCamera is called. /// </summary> public static void ClearSelectedCamera() { s_ActiveInstance = s_DefaultInstance; s_ActiveCameraId = 0; s_ActiveInstanceDirty = true; }