Guest User

Untitled

a guest
Nov 22nd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.24 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.PostProcessing;
  3. using System.Collections.Generic;
  4.  
  5. /// <summary>
  6. /// Use this component to dynamically create a PostProcessingBehaviour and instantiate a PostProcessingProfile on a Camera
  7. /// This allows you to dynamically modify at runtime the PostProcessingProfile, without modifying the asset.
  8. /// This component keeps track of the Profile and Instances. This means that if 2 different camera use the same Profile, they will use the same Instance.
  9. /// </summary>
  10. [RequireComponent(typeof(Camera))]
  11. public class InstantiatePostProcessingProfile : MonoBehaviour
  12. {
  13. [SerializeField] PostProcessingProfile m_Profile = null;
  14.  
  15. static Dictionary<PostProcessingProfile, PostProcessingProfile> ms_RefToInstance = new Dictionary<PostProcessingProfile, PostProcessingProfile>();
  16. static PostProcessingProfile AssignProfile(PostProcessingProfile reference)
  17. {
  18. if (!reference)
  19. return null;
  20.  
  21. // keep track of the profile and instances: only 1 instance is created per profile
  22. // (event if multiple cameras share 1 profile)
  23. if (!ms_RefToInstance.ContainsKey(reference))
  24. {
  25. var profileInstance = Object.Instantiate(reference);
  26. QualitySettingsAdjustments(profileInstance);
  27. ms_RefToInstance.Add(reference, profileInstance);
  28. }
  29.  
  30. return ms_RefToInstance[reference];
  31. }
  32.  
  33. T GetOrAddComponent<T>() where T : Component
  34. {
  35. var component = gameObject.GetComponent<T>();
  36. if (component == null)
  37. component = gameObject.AddComponent<T>();
  38. return component;
  39. }
  40.  
  41. void Start()
  42. {
  43. if (m_Profile)
  44. {
  45. var ppb = GetOrAddComponent<PostProcessingBehaviour>();
  46. Debug.Assert(ppb);
  47. ppb.profile = AssignProfile(m_Profile);
  48. }
  49.  
  50. Object.Destroy(this);
  51. }
  52.  
  53. static void QualitySettingsAdjustments(PostProcessingProfile profile)
  54. {
  55. // Here you can adjust the PostProcessingProfile just after instantiation.
  56. // For example you can change it according to the Quality Settings.
  57. // Like here we disable Motion Blur for low quality.
  58. if (QualitySettings.GetQualityLevel() < 1)
  59. profile.motionBlur.enabled = false;
  60. }
  61. }
Add Comment
Please, Sign In to add comment