Skip to content

Thundernerd/Unity3D-ConstrainedRect

Repository files navigation

Constrained Rect

GitHub package.json version GitHub issues GitHub pull requests GitHub license GitHub last commit

Constrained Rect is a small helper that aims to make it easier to create Rect's based on existing ones.

Installation

  1. The package is available on the openupm registry. You can install it via openupm-cli.
openupm add net.tnrd.constrainedrect
  1. Installing through a Unity Package created by the Package Installer Creator from Needle

  1. 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"

Usage

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
}

Important

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.

Support

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.

ko-fi

Contributing

Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.

About

A simple helper for constraining rects to windows and other rects

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages