Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Thundernerd/Unity3D-ConstrainedRect
A simple helper for constraining rects to windows and other rects
https://github.com/Thundernerd/Unity3D-ConstrainedRect
Last synced: 3 months ago
JSON representation
A simple helper for constraining rects to windows and other rects
- Host: GitHub
- URL: https://github.com/Thundernerd/Unity3D-ConstrainedRect
- Owner: Thundernerd
- License: mit
- Created: 2020-02-16T13:31:43.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-01-06T19:36:58.000Z (almost 4 years ago)
- Last Synced: 2024-04-24T16:42:09.804Z (7 months ago)
- Language: C#
- Homepage:
- Size: 47.9 KB
- Stars: 17
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README
# Constrained Rect
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](https://openupm.com). You can install it via [openupm-cli](https://github.com/openupm/openupm-cli).
```
openupm add net.tnrd.constrainedrect
```
2. Installing through a [Unity Package](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.constrainedrect?registry=https://package.openupm.com) created by the [Package Installer Creator](https://package-installer.glitch.me) from [Needle](https://needle.tools)[](http://package-installer.glitch.me/v1/installer/package.openupm.com/net.tnrd.constrainedrect?registry=https://package.openupm.com)
3. You can also install via git url by adding these entries in your **manifest.json**
```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
```csharp
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.
```csharp
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](https://www.ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/J3J11GEYY)
## Contributing
Pull requests are welcomed. Please feel free to fix any issues you find, or add new features.