Constrained Rect is a small helper that aims to make it easier to create Rect's based on existing ones.
- The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.constrainedrect
- Installing through a Unity Package created by the Package Installer Creator from Needle
- You can also install via git url by adding these entries in your manifest.json
"net.tnrd.constrainedrect": "https://github.com/Thundernerd/Unity3D-ConstrainedRect.git"
Using constrained rects is easy. You simply call Constrain.To(...)
and pass it either a Rect
or an EditorWindow
.
Here's an example using a simple Rect
private void Foo()
{
Rect rect = new Rect(16, 16, 128, 128);
Rect constrainedRect = Constrain.To(rect)
.Left.Relative(8)
.Top.Relative(16)
.Right.Relative(24)
.Bottom.Relative(32)
.ToRect();
Debug.Log(constrainedRect.xMin); // Logs 24
Debug.Log(constrainedRect.yMin); // Logs 32
Debug.Log(constrainedRect.xMax); // Logs 104
Debug.Log(constrainedRect.yMax); // Logs 96
}
Aside from Left
, Top
, Right
, and Bottom
, you can also use Width
and Height
. If you want to use width
and height
you will have to omit either left
or right
and top
or bottom
respectively.
Other modifiers are Absolute
and Percentage
.
Absolute is what it suggests: instead of taking into account it's constraints it just returns the value given.
Percentage expects a float value between 0 and 1 (inclusive) and multiplies that value with the constrained property.
private void Foo()
{
Rect rect = new Rect(16, 16, 128, 128);
Rect constrainedRect = Constrain.To(rect)
.Width.Percentage(0.5f)
.ToRect();
Debug.Log(constrainedRect.width); // Logs 64
}
Due to the nature of Unity's editor architecture it is common to use Constrained Rects in high volume. In an attempt to prevent creating and collecting too much garbage as a result of using Constrained Rects they are now being pooled.
After you've finalized your Constrained Rect by calling .ToRect()
the Constrained Rect will be returned to the pool and free to use for other instances.
While all of this happens under the hood it is important to understand that from the moment that you call .ToRect()
the Constrained Rect will throw an exception if it is not being used. This also means that if it is being used then the properties and variables might be different from what you might expect.
Constrained Rect is a small and open-source utility that I hope helps other people. It is by no means necessary but if you feel generous you can support me by donating.
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.