A game about forced loneliness, made by TACStudios
at master 67 lines 3.1 kB view raw
1using System; 2using System.Collections.Concurrent; 3using System.Linq.Expressions; 4using System.Reflection; 5 6namespace UnityEngine.Rendering 7{ 8 // This file can't be in the editor assembly as we need to access it in runtime-editor-specific 9 // places like OnGizmo etc and we don't want to add the editor assembly as a dependency of the 10 // runtime one 11 12 // The UI layout/styling in this panel is broken and can't match the one from built-ins 13 // preference panels as everything needed is internal/private (at the time of writing this 14 // comment) 15 16#if UNITY_EDITOR 17 using UnityEditor; 18 19 public static class CoreRenderPipelinePreferences 20 { 21 // We do not want that GC frees the preferences that have been added, used to store their references 22 static readonly ConcurrentStack<object> s_ColorPref = new ConcurrentStack<object>(); 23 24 #region Volumes Gizmo Color 25 26 [Obsolete("Use VolumePreferences", false)] 27 public static Color volumeGizmoColor { get; } = new Color(0.2f, 0.8f, 0.1f, 0.125f); 28 29 #endregion 30 31 #region Preview Camera Background Color 32 33 static readonly Color kPreviewCameraBackgroundColorDefault = new Color(82f / 255.0f, 82f / 255.0f, 82.0f / 255.0f, 0.0f); 34 public static Color previewBackgroundColor => kPreviewCameraBackgroundColorDefault; 35 36 #endregion 37 38 /// <summary> 39 /// Adds a <see cref="PrefColor"/> into the **Preferences > Colors** panel./> 40 /// </summary> 41 /// <param name="name">The name the color has in the **Colors** panel. This is in the format of 'group/name'.</param> 42 /// <param name="defaultColor">The initial color to use for the new entry in the **Colors** panel. This is also the value Unity uses when it resets the colors to their defaults.</param> 43 public static Func<Color> RegisterPreferenceColor(string name, Color defaultColor) 44 { 45 if (string.IsNullOrEmpty(name)) 46 throw new ArgumentException("You must give a valid name for the color property", nameof(name)); 47 48 // PrefColor is the type to use to have a Color that is customizable inside the Preference/Colors panel. 49 // Sadly it is internal so we must create it and grab color from it by reflection. 50 Type prefColorType = typeof(Editor).Assembly.GetType("UnityEditor.PrefColor"); 51 PropertyInfo colorInfo = prefColorType.GetProperty("Color"); 52 53 var colorPref = Activator.CreateInstance(prefColorType, name, defaultColor.r, defaultColor.g, defaultColor.b, defaultColor.a); 54 s_ColorPref.Push(colorPref); 55 56 MemberExpression colorProperty = Expression.Property(Expression.Constant(colorPref, prefColorType), colorInfo); 57 58 // Make sure that the new preference color is being loaded into the Preference/Colors panel 59 MethodInfo loadMethod = prefColorType.GetMethod("Load"); 60 loadMethod.Invoke(colorPref, null); 61 62 // Return the method to obtain the color 63 return Expression.Lambda<Func<Color>>(colorProperty).Compile(); 64 } 65 } 66#endif 67}