Unity Tip - VERY USEFUL MinMax Range Slider Attribute

  Рет қаралды 22,342

GucioDevs

GucioDevs

Күн бұрын

Here's a really useful tip for Unity. Often when using Random.Range() I plug in the x and y values of a Vector2. Instead of typing in the values of the Vector2, it is much nicer to use this min max slider to slide the values in.
Here's the github page:
github.com/Guc...
COPY & PASTE THIS LINK INTO UNITY PACKAGE MANAGER:
github.com/Guc...
If you don't have Unity 2019.3 or greater, you can install the package using a good ol' .unitypackage file. Download it from here and open it up to import the package into unity:
github.com/Guc...
(click the download button beside the history button)
Top-down fake height effect tutorial:
• 2D Top-Down Height Ill...
#UnityTips #Unity #Gamedev

Пікірлер: 46
@DenisLabrecque
@DenisLabrecque 3 жыл бұрын
In 2021 version, this prevented the build. Something about the namespace being unable to reference.
@dappy3338
@dappy3338 2 ай бұрын
its a little late, but heres the fix: replace the code in MinMaxSliderDrawer with this: #if UNITY_EDITOR using UnityEngine; using UnityEditor; using GD.MinMaxSlider; [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"); //PrefixLabel returns the rect of the right part of the control. It leaves out the label section. We don't have to worry about it. Nice! 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; //F2 limits the float to two decimal places (0.00). minVal = EditorGUI.FloatField(splittedRect[0], float.Parse(minVal.ToString("F2"))); maxVal = EditorGUI.FloatField(splittedRect[2], float.Parse(maxVal.ToString("F2"))); EditorGUI.MinMaxSlider(splittedRect[1], 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; } } #endif
@GuillemPoy
@GuillemPoy 4 жыл бұрын
Could you do a tutorial about how to do a package the way you did? I think it is super convenient to share tools that way!
@PhotoMakers
@PhotoMakers 4 жыл бұрын
This is amazing! I was looking for this solution, but I got a problem: I can't export. It'll list some errors like this: Library/PackageCache/com.guciodevs.simple-min-max-slider@46ff35d229/Scripts/Editor/MinMaxSliderDrawer.cs(6,35): error CS0246: The type or namespace name 'PropertyDrawer' could not be found (are you missing a using directive or an assembly reference?) Any idea on how to solve this?
@super207room
@super207room 2 жыл бұрын
the same bullshit
@sevazakharenko8176
@sevazakharenko8176 2 ай бұрын
I didn't know Captain America made Unity tutorials, thank you for this! I honestly thought this was already built into Unity since they showed a feature like this at one of their conventions.
@traptown6418
@traptown6418 4 жыл бұрын
GucioDevs
@GucioDevs
@GucioDevs 4 жыл бұрын
that's me
@IceGoldDev
@IceGoldDev 4 жыл бұрын
@@GucioDevs Is it
@RabbleRousy
@RabbleRousy Жыл бұрын
This is great, thanks dude!
@beaverjoe9171
@beaverjoe9171 4 жыл бұрын
Nice demo but I follow your step and used on my Unity. It's not work. My version is 2019.3.0f3 and the Only variable name show on the inspector. No Slider and value even I declare as public. WAIT. ITS WORK FOR VECTOR2 Struct. THX. I KNEW
@marino1805
@marino1805 4 жыл бұрын
This does look very nice, might pick it up one day, when ill work on a solo project.
@CodeWithKarl
@CodeWithKarl 4 жыл бұрын
i have error :( when i build my game
@PhotoMakers
@PhotoMakers 4 жыл бұрын
Me too. I can't Build. :(
@codersexpo1580
@codersexpo1580 4 жыл бұрын
Same. I imported the asset and can see it under packages as Simple MinMax Slider. When I add "using GD.MinMaxSlider;" in my class it says it is not recognized. However, if I expand Assembly-CSharp => "References", I see there is a yield symbol on this resource. I just clicked on it and it fixed that issue and now seems to recognize this. Strange but it is working now...but...the text box display for min max values does not display, its just a thin field (like no width).
@whitedeath5057
@whitedeath5057 8 ай бұрын
Git link doesn't work for me :/
@zendraw3468
@zendraw3468 Жыл бұрын
when i try to build with IL2CPP backend, it throws me an error about missing stuff or something. pls fix it.
@dappy3338
@dappy3338 2 ай бұрын
its a little late, but heres the fix: replace the code in MinMaxSliderDrawer with this: #if UNITY_EDITOR using UnityEngine; using UnityEditor; using GD.MinMaxSlider; [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"); //PrefixLabel returns the rect of the right part of the control. It leaves out the label section. We don't have to worry about it. Nice! 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; //F2 limits the float to two decimal places (0.00). minVal = EditorGUI.FloatField(splittedRect[0], float.Parse(minVal.ToString("F2"))); maxVal = EditorGUI.FloatField(splittedRect[2], float.Parse(maxVal.ToString("F2"))); EditorGUI.MinMaxSlider(splittedRect[1], 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; } } #endif
@drstuarteve
@drstuarteve 4 жыл бұрын
This IS very useful, and so easy to install as well. Awesome, thanks!
@gustavrgild22
@gustavrgild22 4 жыл бұрын
Nice vid! Very useful!
@thatomogale9803
@thatomogale9803 3 жыл бұрын
Hey, I've done everything in this video, but when I test it, all I see is a blank space where the slider is supposed to be. The tooltip does work though. Please help!
@diliupg
@diliupg 3 жыл бұрын
Thank you very much. The link didn't work for me in Unity 2020.2. So I downloaded the package. Helps a lot with the app being developed. Subscribed to your channel. Thanks Again. May you be well!
@johannesottoboldingkruse3148
@johannesottoboldingkruse3148 4 жыл бұрын
VERY USEFUL 😘
@jiazq
@jiazq 3 жыл бұрын
can it be or adjust furture to use in runtime , I mean NOT JUST in editor Extension?
@danielraaschou-pedersen2961
@danielraaschou-pedersen2961 4 жыл бұрын
Interesting!
@hardworkerstudio
@hardworkerstudio 3 жыл бұрын
Very good But the git link is not work You should publish it on Unity Asset store if it have some cost or not free is still ok this tool is very importance
@SylvainSangla
@SylvainSangla 4 жыл бұрын
Thanks, that's very useful and works great, congrats ! Cheers from France.
@holdengreene9717
@holdengreene9717 4 жыл бұрын
THANK YOU!!!! I SPENT LIKE an hour looking for something exactly like this. You helped me more than Brackeys today.... I just thought you should know that.
@Ankhtepot
@Ankhtepot 3 жыл бұрын
Thanks, that's super useful bit.
@PreslavKolev
@PreslavKolev 4 жыл бұрын
How did you do your own "using" thing?
@knt5429
@knt5429 4 жыл бұрын
R u the kid in the 5yr old videos
@horsethi3f
@horsethi3f 3 жыл бұрын
Any built in solution?
@mythsheep
@mythsheep 4 жыл бұрын
Any idea on how to create a min max "UI" slider? that user can manipulate in game?
@alexanderbjerking8508
@alexanderbjerking8508 Жыл бұрын
I know that this is 2 years later but it might help somebody else. A very simple but maybe not the best way of doing it is just using 2 sliders, and then flip one, remove the background on one of them and have the background be the fill color and the the fill be the background color. Just be aware that you would also need to create a simple script that checks if the min value is above or equal to the max value and vice versa and then just set the value to one before/after the other handle, also be aware that if you have one from say 0-20 then the flipped one would go the wrong way (right side being 0 and left side being 20) but it can be "ignored" if you just say "20-value" instead of just "value".
@IOSoraOI
@IOSoraOI 4 жыл бұрын
Thanks for being great
@asalongino3588
@asalongino3588 4 жыл бұрын
im honestly so glad youre back man, your content is one of the best out there and i love your style
@michalski9141
@michalski9141 2 жыл бұрын
What happened to [Range()] ?
@cannotfigureoutaname
@cannotfigureoutaname 2 жыл бұрын
Only works with int and float
@ahmedmahmoudahmed6888
@ahmedmahmoudahmed6888 Жыл бұрын
What a handy dandy brandy randy little tool you have, you majestic programming creature! I bestow my sub upon you!
@wanderingzanzey2126
@wanderingzanzey2126 2 жыл бұрын
*cough* [Range(0, 300)] *cough*
@3dbob891
@3dbob891 Жыл бұрын
yeah sure if you want one explicit value. The thing is magic happens when you introduce randomness and want a slghtly varying outcome suddenly range becomes as good as just typing values.
SCRIPTABLE OBJECTS in Unity
8:57
Brackeys
Рет қаралды 1 МЛН
How Strong is Tin Foil? 💪
00:26
Preston
Рет қаралды 139 МЛН
From Small To Giant Pop Corn #katebrush #funny #shorts
00:17
Kate Brush
Рет қаралды 71 МЛН
Giving Personality to Procedural Animations using Math
15:30
t3ssel8r
Рет қаралды 2,5 МЛН
The GIGA JUKE is dead.
28:02
Mend It Mark
Рет қаралды 69 М.
Better Jumping in Unity With Four Lines of Code
12:47
Board To Bits Games
Рет қаралды 822 М.
The Power of Scriptable Objects as Middle-Men
17:41
samyam
Рет қаралды 126 М.
How to make 2D GLOW in Unity!
15:56
Brackeys
Рет қаралды 598 М.
Why you should use code to animate your UI in Unity.
5:02
Game Dev Guide
Рет қаралды 332 М.
Helper functions I can't live without in Unity
6:03
Tarodev
Рет қаралды 32 М.