diff options
Diffstat (limited to 'SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute')
5 files changed, 163 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor.meta b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor.meta new file mode 100644 index 0000000..0fbfc05 --- /dev/null +++ b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: fbe331826f9610d4b8bfba6374b10098 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs new file mode 100644 index 0000000..74bfebb --- /dev/null +++ b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs @@ -0,0 +1,118 @@ +using UnityEngine; +using UnityEditor; + +[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))] +public class MinMaxSliderDrawer : PropertyDrawer +{ + public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) + { + var minMaxAttribute = (MinMaxSliderAttribute)attribute; + var propertyType = property.propertyType; + + label.tooltip = minMaxAttribute.min.ToString("F2") + " to " + minMaxAttribute.max.ToString("F2"); + + Rect controlRect = EditorGUI.PrefixLabel(position, label); + + Rect[] splittedRect = SplitRect(controlRect, 3); + + if (propertyType == SerializedPropertyType.Vector2) + { + + EditorGUI.BeginChangeCheck(); + + Vector2 vector = property.vector2Value; + float minVal = vector.x; + float maxVal = vector.y; + + minVal = float.Parse(GUI.TextField(splittedRect[0], minVal.ToString("F2"))); + maxVal = float.Parse(GUI.TextField(splittedRect[2], maxVal.ToString("F2"))); + + Rect sliderRect = new Rect(splittedRect[0].x + 10, splittedRect[0].y, splittedRect[2].x - splittedRect[0].x - 15, splittedRect[0].height); + + EditorGUI.MinMaxSlider(sliderRect, ref minVal, ref maxVal, + minMaxAttribute.min, minMaxAttribute.max); + + if (minVal < minMaxAttribute.min) + { + minVal = minMaxAttribute.min; + } + + if (maxVal > minMaxAttribute.max) + { + maxVal = minMaxAttribute.max; + } + + vector = new Vector2(minVal > maxVal ? maxVal : minVal, maxVal); + + if (EditorGUI.EndChangeCheck()) + { + property.vector2Value = vector; + } + + } + else if (propertyType == SerializedPropertyType.Vector2Int) + { + + EditorGUI.BeginChangeCheck(); + + Vector2Int vector = property.vector2IntValue; + float minVal = vector.x; + float maxVal = vector.y; + + minVal = EditorGUI.FloatField(splittedRect[0], minVal); + maxVal = EditorGUI.FloatField(splittedRect[2], maxVal); + + EditorGUI.MinMaxSlider(splittedRect[1], ref minVal, ref maxVal, + minMaxAttribute.min, minMaxAttribute.max); + + if (minVal < minMaxAttribute.min) + { + maxVal = minMaxAttribute.min; + } + + if (minVal > minMaxAttribute.max) + { + maxVal = minMaxAttribute.max; + } + + vector = new Vector2Int(Mathf.FloorToInt(minVal > maxVal ? maxVal : minVal), Mathf.FloorToInt(maxVal)); + + if (EditorGUI.EndChangeCheck()) + { + property.vector2IntValue = vector; + } + + } + + } + + Rect[] SplitRect(Rect rectToSplit, int n) + { + + + Rect[] rects = new Rect[n]; + + for (int i = 0; i < n; i++) + { + + rects[i] = new Rect(rectToSplit.position.x + (i * rectToSplit.width / n), rectToSplit.position.y, rectToSplit.width / n, rectToSplit.height); + + } + + int padding = (int)rects[0].width - 40; + int space = 5; + + rects[0].width -= padding + space; + rects[2].width -= padding + space; + + rects[1].x -= padding; + rects[1].width += padding * 2; + + rects[2].x += padding + space; + + + return rects; + + } + +} diff --git a/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs.meta b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs.meta new file mode 100644 index 0000000..7efaf04 --- /dev/null +++ b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2418136676909034b8638efdc91a215e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs new file mode 100644 index 0000000..c01fbe3 --- /dev/null +++ b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs @@ -0,0 +1,15 @@ +using UnityEngine; +using UnityEditor; + +public class MinMaxSliderAttribute : PropertyAttribute +{ + + public float min; + public float max; + + public MinMaxSliderAttribute(float min, float max) + { + this.min = min; + this.max = max; + } +}
\ No newline at end of file diff --git a/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs.meta b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs.meta new file mode 100644 index 0000000..ca58275 --- /dev/null +++ b/SurvivalTest/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/MinMaxSliderAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 293fbbe23cf64d94885e043ab4f2aa0d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |