|
| 1 | +using UnityEditor; |
| 2 | +using UnityEngine; |
| 3 | +using System.Linq; |
| 4 | +using System; |
| 5 | +using Object = UnityEngine.Object; |
| 6 | +#if TMP_ENABLE |
| 7 | +using TMPro; |
| 8 | +#endif |
| 9 | + |
| 10 | +namespace Coffee.UIEffects.Editors |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// UIEffect editor. |
| 14 | + /// </summary> |
| 15 | + [CustomEditor(typeof(UIEffect), true)] |
| 16 | + [CanEditMultipleObjects] |
| 17 | + public class UIEffect2Editor : Editor |
| 18 | + { |
| 19 | + private SerializedProperty _toneFilter; |
| 20 | + private SerializedProperty _toneIntensity; |
| 21 | + |
| 22 | + private SerializedProperty _colorFilter; |
| 23 | + private SerializedProperty _color; |
| 24 | + private SerializedProperty _colorIntensity; |
| 25 | + |
| 26 | + private SerializedProperty _samplingFilter; |
| 27 | + private SerializedProperty _samplingIntensity; |
| 28 | + |
| 29 | + private SerializedProperty _transitionFilter; |
| 30 | + private SerializedProperty _transitionRate; |
| 31 | + private SerializedProperty _transitionTex; |
| 32 | + private SerializedProperty _transitionTexScale; |
| 33 | + private SerializedProperty _transitionTexOffset; |
| 34 | + private SerializedProperty _transitionReverse; |
| 35 | + private SerializedProperty _transitionRotation; |
| 36 | + private SerializedProperty _transitionKeepAspectRatio; |
| 37 | + private SerializedProperty _transitionWidth; |
| 38 | + private SerializedProperty _transitionSoftness; |
| 39 | + private SerializedProperty _transitionColorFilter; |
| 40 | + private SerializedProperty _transitionColor; |
| 41 | + |
| 42 | + private SerializedProperty _targetMode; |
| 43 | + private SerializedProperty _targetColor; |
| 44 | + private SerializedProperty _targetRange; |
| 45 | + private SerializedProperty _targetSoftness; |
| 46 | + |
| 47 | + private SerializedProperty _blendType; |
| 48 | + private SerializedProperty _srcBlendMode; |
| 49 | + private SerializedProperty _dstBlendMode; |
| 50 | + |
| 51 | + private SerializedProperty _shadowMode; |
| 52 | + private SerializedProperty _shadowDistance; |
| 53 | + private SerializedProperty _shadowDistanceX; |
| 54 | + private SerializedProperty _shadowDistanceY; |
| 55 | + private SerializedProperty _shadowIteration; |
| 56 | + private SerializedProperty _shadowFade; |
| 57 | + private SerializedProperty _shadowEffectOnOrigin; |
| 58 | + private SerializedProperty _shadowMirrorScale; |
| 59 | + |
| 60 | + private void OnEnable() |
| 61 | + { |
| 62 | + if (target == null) return; |
| 63 | + |
| 64 | + _toneFilter = serializedObject.FindProperty("m_ToneFilter"); |
| 65 | + _toneIntensity = serializedObject.FindProperty("m_ToneIntensity"); |
| 66 | + var toneParams = serializedObject.FindProperty("m_ToneParams"); |
| 67 | + |
| 68 | + _colorFilter = serializedObject.FindProperty("m_ColorFilter"); |
| 69 | + _color = serializedObject.FindProperty("m_Color"); |
| 70 | + _colorIntensity = serializedObject.FindProperty("m_ColorIntensity"); |
| 71 | + |
| 72 | + _samplingFilter = serializedObject.FindProperty("m_SamplingFilter"); |
| 73 | + _samplingIntensity = serializedObject.FindProperty("m_SamplingIntensity"); |
| 74 | + |
| 75 | + _transitionFilter = serializedObject.FindProperty("m_TransitionFilter"); |
| 76 | + _transitionRate = serializedObject.FindProperty("m_TransitionRate"); |
| 77 | + _transitionTex = serializedObject.FindProperty("m_TransitionTex"); |
| 78 | + _transitionTexScale = serializedObject.FindProperty("m_TransitionTexScale"); |
| 79 | + _transitionTexOffset = serializedObject.FindProperty("m_TransitionTexOffset"); |
| 80 | + _transitionReverse = serializedObject.FindProperty("m_TransitionReverse"); |
| 81 | + _transitionRotation = serializedObject.FindProperty("m_TransitionRotation"); |
| 82 | + _transitionKeepAspectRatio = serializedObject.FindProperty("m_TransitionKeepAspectRatio"); |
| 83 | + _transitionWidth = serializedObject.FindProperty("m_TransitionWidth"); |
| 84 | + _transitionSoftness = serializedObject.FindProperty("m_TransitionSoftness"); |
| 85 | + _transitionColorFilter = serializedObject.FindProperty("m_TransitionColorFilter"); |
| 86 | + _transitionColor = serializedObject.FindProperty("m_TransitionColor"); |
| 87 | + |
| 88 | + _targetMode = serializedObject.FindProperty("m_TargetMode"); |
| 89 | + _targetColor = serializedObject.FindProperty("m_TargetColor"); |
| 90 | + _targetRange = serializedObject.FindProperty("m_TargetRange"); |
| 91 | + _targetSoftness = serializedObject.FindProperty("m_TargetSoftness"); |
| 92 | + |
| 93 | + _blendType = serializedObject.FindProperty("m_BlendType"); |
| 94 | + _srcBlendMode = serializedObject.FindProperty("m_SrcBlendMode"); |
| 95 | + _dstBlendMode = serializedObject.FindProperty("m_DstBlendMode"); |
| 96 | + |
| 97 | + _shadowMode = serializedObject.FindProperty("m_ShadowMode"); |
| 98 | + _shadowDistance = serializedObject.FindProperty("m_ShadowDistance"); |
| 99 | + _shadowDistanceX = _shadowDistance.FindPropertyRelative("x"); |
| 100 | + _shadowDistanceY = _shadowDistance.FindPropertyRelative("y"); |
| 101 | + _shadowIteration = serializedObject.FindProperty("m_ShadowIteration"); |
| 102 | + _shadowFade = serializedObject.FindProperty("m_ShadowFade"); |
| 103 | + _shadowEffectOnOrigin = serializedObject.FindProperty("m_ShadowEffectOnOrigin"); |
| 104 | + _shadowMirrorScale = serializedObject.FindProperty("m_ShadowMirrorScale"); |
| 105 | + } |
| 106 | + |
| 107 | + public override void OnInspectorGUI() |
| 108 | + { |
| 109 | + serializedObject.Update(); |
| 110 | + DrawPresetMenu(targets); |
| 111 | + DrawSeparator(); |
| 112 | + DrawProperties(); |
| 113 | + serializedObject.ApplyModifiedProperties(); |
| 114 | + } |
| 115 | + |
| 116 | + public void DrawProperties() |
| 117 | + { |
| 118 | + serializedObject.Update(); |
| 119 | + |
| 120 | + // Tone filter |
| 121 | + if (DrawHeaderPopup(_toneFilter)) |
| 122 | + { |
| 123 | + EditorGUI.indentLevel++; |
| 124 | + EditorGUILayout.PropertyField(_toneIntensity); |
| 125 | + |
| 126 | + EditorGUI.indentLevel--; |
| 127 | + } |
| 128 | + |
| 129 | + // Color filter |
| 130 | + DrawSeparator(); |
| 131 | + var prevColorFilter = (ColorFilter)_colorFilter.intValue; |
| 132 | + if (DrawHeaderPopup(_colorFilter)) |
| 133 | + { |
| 134 | + EditorGUI.indentLevel++; |
| 135 | + EditorGUILayout.PropertyField(_colorIntensity); |
| 136 | + DrawColor(_colorFilter, _color, prevColorFilter); |
| 137 | + EditorGUI.indentLevel--; |
| 138 | + } |
| 139 | + |
| 140 | + EditorGUI.EndChangeCheck(); |
| 141 | + |
| 142 | + // Sampling filter |
| 143 | + DrawSeparator(); |
| 144 | + if (DrawHeaderPopup(_samplingFilter)) |
| 145 | + { |
| 146 | + EditorGUI.indentLevel++; |
| 147 | + EditorGUILayout.PropertyField(_samplingIntensity); |
| 148 | + EditorGUI.indentLevel--; |
| 149 | + } |
| 150 | + |
| 151 | + // Transition filter |
| 152 | + DrawSeparator(); |
| 153 | + if (DrawHeaderPopup(_transitionFilter)) |
| 154 | + { |
| 155 | + EditorGUI.indentLevel++; |
| 156 | + EditorGUILayout.PropertyField(_transitionRate); |
| 157 | + EditorGUILayout.PropertyField(_transitionTex); |
| 158 | + EditorGUI.indentLevel++; |
| 159 | + EditorGUILayout.PropertyField(_transitionTexScale, EditorGUIUtility.TrTempContent("Scale")); |
| 160 | + EditorGUILayout.PropertyField(_transitionTexOffset, EditorGUIUtility.TrTempContent("Offset")); |
| 161 | + EditorGUILayout.PropertyField(_transitionRotation, EditorGUIUtility.TrTempContent("Rotation")); |
| 162 | + EditorGUILayout.PropertyField(_transitionKeepAspectRatio, |
| 163 | + EditorGUIUtility.TrTempContent("Keep Aspect Ratio")); |
| 164 | + EditorGUILayout.PropertyField(_transitionReverse, EditorGUIUtility.TrTempContent("Reverse")); |
| 165 | + EditorGUI.indentLevel--; |
| 166 | + |
| 167 | + if (2 < _transitionFilter.intValue) |
| 168 | + { |
| 169 | + EditorGUILayout.PropertyField(_transitionWidth); |
| 170 | + EditorGUILayout.PropertyField(_transitionSoftness); |
| 171 | + prevColorFilter = (ColorFilter)_transitionColorFilter.intValue; |
| 172 | + EditorGUILayout.PropertyField(_transitionColorFilter); |
| 173 | + DrawColor(_transitionColorFilter, _transitionColor, prevColorFilter); |
| 174 | + } |
| 175 | + |
| 176 | + EditorGUI.indentLevel--; |
| 177 | + } |
| 178 | + |
| 179 | + // Target |
| 180 | + DrawSeparator(); |
| 181 | + if (DrawHeaderPopup(_targetMode)) |
| 182 | + { |
| 183 | + EditorGUI.indentLevel++; |
| 184 | + EditorGUILayout.PropertyField(_targetColor); |
| 185 | + EditorGUILayout.PropertyField(_targetRange); |
| 186 | + EditorGUILayout.PropertyField(_targetSoftness); |
| 187 | + EditorGUI.indentLevel--; |
| 188 | + } |
| 189 | + |
| 190 | + // Blending |
| 191 | + DrawSeparator(); |
| 192 | + if (!DrawHeaderPopup(_blendType)) |
| 193 | + { |
| 194 | + EditorGUI.indentLevel++; |
| 195 | + |
| 196 | + EditorGUILayout.PropertyField(_srcBlendMode); |
| 197 | + EditorGUILayout.PropertyField(_dstBlendMode); |
| 198 | + EditorGUI.indentLevel--; |
| 199 | + } |
| 200 | + |
| 201 | + // Shadow |
| 202 | + DrawSeparator(); |
| 203 | + var prevShadowMode = (ShadowMode)_shadowMode.intValue; |
| 204 | + if (DrawHeaderPopup(_shadowMode)) |
| 205 | + { |
| 206 | + EditorGUI.indentLevel++; |
| 207 | + if ((ShadowMode)_shadowMode.intValue == ShadowMode.Mirror) |
| 208 | + { |
| 209 | + if (prevShadowMode != ShadowMode.Mirror) |
| 210 | + { |
| 211 | + _shadowDistanceX.floatValue = 0.5f; |
| 212 | + _shadowDistanceY.floatValue = 0f; |
| 213 | + } |
| 214 | + |
| 215 | + EditorGUILayout.Slider(_shadowDistanceX, 0, 1, "Reflection"); |
| 216 | + EditorGUILayout.PropertyField(_shadowDistanceY, EditorGUIUtility.TrTempContent("Offset")); |
| 217 | + EditorGUILayout.PropertyField(_shadowMirrorScale); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + if (prevShadowMode == ShadowMode.Mirror) |
| 222 | + { |
| 223 | + _shadowDistanceX.floatValue = 1f; |
| 224 | + _shadowDistanceY.floatValue = -1f; |
| 225 | + } |
| 226 | + |
| 227 | + EditorGUILayout.PropertyField(_shadowDistance); |
| 228 | + EditorGUILayout.PropertyField(_shadowIteration); |
| 229 | + } |
| 230 | + |
| 231 | + EditorGUILayout.PropertyField(_shadowFade); |
| 232 | + EditorGUILayout.PropertyField(_shadowEffectOnOrigin); |
| 233 | + EditorGUI.indentLevel--; |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + private static void DrawColor(SerializedProperty filter, SerializedProperty color, ColorFilter prevFilter) |
| 238 | + { |
| 239 | + if (filter.intValue == (int)ColorFilter.None) |
| 240 | + { |
| 241 | + } |
| 242 | + else if (filter.intValue == (int)ColorFilter.HsvModifier) |
| 243 | + { |
| 244 | + if (prevFilter != ColorFilter.HsvModifier) |
| 245 | + { |
| 246 | + color.colorValue = Color.black; |
| 247 | + } |
| 248 | + |
| 249 | + EditorGUILayout.Slider(color.FindPropertyRelative("r"), -0.5f, 0.5f, "Hue"); |
| 250 | + EditorGUILayout.Slider(color.FindPropertyRelative("g"), -1f, 1f, "Saturation"); |
| 251 | + EditorGUILayout.Slider(color.FindPropertyRelative("b"), -1f, 1f, "Value"); |
| 252 | + EditorGUILayout.Slider(color.FindPropertyRelative("a"), 0, 1, "Alpha"); |
| 253 | + } |
| 254 | + else if (filter.intValue == (int)ColorFilter.Contrast) |
| 255 | + { |
| 256 | + if (prevFilter != ColorFilter.Contrast) |
| 257 | + { |
| 258 | + color.colorValue = Color.black; |
| 259 | + } |
| 260 | + |
| 261 | + EditorGUILayout.Slider(color.FindPropertyRelative("r"), -1f, 1f, "Contrast"); |
| 262 | + EditorGUILayout.Slider(color.FindPropertyRelative("g"), -1f, 1f, "Brightness"); |
| 263 | + EditorGUILayout.Slider(color.FindPropertyRelative("a"), 0, 1, "Alpha"); |
| 264 | + } |
| 265 | + else |
| 266 | + { |
| 267 | + if (prevFilter == ColorFilter.HsvModifier || prevFilter == ColorFilter.Contrast) |
| 268 | + { |
| 269 | + color.colorValue = Color.white; |
| 270 | + } |
| 271 | + |
| 272 | + EditorGUILayout.PropertyField(color); |
| 273 | + } |
| 274 | + } |
| 275 | + |
| 276 | + private static bool DrawHeaderPopup(SerializedProperty sp) |
| 277 | + { |
| 278 | + var r = EditorGUILayout.GetControlRect(); |
| 279 | + var pos = EditorGUI.PrefixLabel(r, EditorGUIUtility.TrTempContent(sp.displayName), EditorStyles.boldLabel); |
| 280 | + EditorGUI.PropertyField(pos, sp, GUIContent.none); |
| 281 | + return 0 < sp.intValue; |
| 282 | + } |
| 283 | + |
| 284 | + private static void DrawSeparator() |
| 285 | + { |
| 286 | + EditorGUILayout.Space(4); |
| 287 | + GUILayout.Label(GUIContent.none, "sv_iconselector_sep", GUILayout.ExpandWidth(true)); |
| 288 | + } |
| 289 | + |
| 290 | + private static void DrawPresetMenu(Object[] targets) |
| 291 | + { |
| 292 | + var r = EditorGUILayout.GetControlRect(); |
| 293 | + r.width /= 2; |
| 294 | + if (GUI.Button(r, EditorGUIUtility.TrTempContent("Load"), "MiniPopup")) |
| 295 | + { |
| 296 | + var menu = new GenericMenu(); |
| 297 | + foreach (var preset in UIEffectProjectSettings.LoadEditorPresets()) |
| 298 | + { |
| 299 | + var path = UIEffectProjectSettings.GetPresetPath(preset); |
| 300 | + menu.AddItem(new GUIContent(path), false, () => |
| 301 | + { |
| 302 | + Array.ForEach(targets.OfType<UIEffect>().ToArray(), t => |
| 303 | + { |
| 304 | + t.LoadPreset(preset); |
| 305 | + }); |
| 306 | + }); |
| 307 | + } |
| 308 | + |
| 309 | + menu.DropDown(r); |
| 310 | + } |
| 311 | + |
| 312 | + r.x += r.width; |
| 313 | + if (GUI.Button(r, EditorGUIUtility.TrTempContent("Save As New"), "MiniButton")) |
| 314 | + { |
| 315 | + UIEffectProjectSettings.SaveAsNewPreset(targets.OfType<UIEffect>().FirstOrDefault()); |
| 316 | + } |
| 317 | + } |
| 318 | + } |
| 319 | +} |
0 commit comments