- Home /
How to calculate the width of the Rect, knowing current value and min and max values
Hello,
how can I calculate the width of the Rect, knowing current (in my case volume) value and min and max (in my case 0 and 1) values?
I am creating a custom GUI slider for my editor extension and I need to rescale the rect according on the current volume value.
I looked around and found the function Mathf.InverseLerp(); but was unable to figure out how to calculate the width still, can someone give some advice on this? It could be anything, I just want to understand on how to calculate these type of things? Thank you!
Rect class in Unity have a width property you can use. It you are referring to an UI object's RectTransform, you can access it with transform.rect.width. If you just want to do the math with the $$anonymous$$ and max, you can do max - $$anonymous$$.
Hey, thanks for reply! I work with EditorWindows so I don't use RectTransform. And doing this (max - $$anonymous$$) won't really fix my issue as I want to rescale the rect width depending on the current value which is between 0 and 1. At least in my case right now. So this would not work unfortunately. :/ I want to recreate in short something like a slider. Like UI Slider for example. And Rect would be my slider fill.
Sorry, I missed the part about it being an editor extension. Would you be able to use EditorGUIUtility.currentViewWidth to get the width of the editor window? Then the $$anonymous$$, max, and value all look to be percentages of that.
This doesn't help you much does it??
if (GUI.Button(new Rect(Screen.width / 2 - 540, Screen.height / 1.5f - 100, 270, 50), "$$anonymous$$ain menu", YourGUIStyle))
I build a lot of editor windows and Editor GUI can a see a snippet? aka a small block of code you are suck on.
Your answer
Follow this Question
Related Questions
Can I show a numeric variable from the slider on the GUI? 1 Answer
Using Gui button to alter Slider Value 1 Answer
Horizontal Slider issue 0 Answers
Changing light intensity with a slider 2 Answers