|
| 1 | +# Constrained Rect |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="#"> |
| 5 | + <img alt="GitHub package.json version" src ="https://img.shields.io/github/package-json/v/Thundernerd/Unity3D-ConstrainedRect" /> |
| 6 | + </a> |
| 7 | + <a href="#"> |
| 8 | + <img alt="GitHub issues" src ="https://img.shields.io/github/issues/Thundernerd/Unity3D-ConstrainedRect" /> |
| 9 | + </a> |
| 10 | + <a href="#"> |
| 11 | + <img alt="GitHub pull requests" src ="https://img.shields.io/github/issues-pr/Thundernerd/Unity3D-ConstrainedRect" /> |
| 12 | + </a> |
| 13 | + <a href="#"> |
| 14 | + <img alt="GitHub license" src ="https://img.shields.io/github/license/Thundernerd/Unity3D-ConstrainedRect" /> |
| 15 | + </a> |
| 16 | + <a href="#"> |
| 17 | + <img alt="GitHub last commit" src ="https://img.shields.io/github/last-commit/Thundernerd/Unity3D-ConstrainedRect" /> |
| 18 | + </a> |
| 19 | +</p> |
| 20 | + |
| 21 | +Constrained Rect is a small helper that aims to make it easier to create Rect's based on existing ones. |
| 22 | + |
| 23 | +## Usage |
| 24 | +Using constrained rects is easy. You simply call `Constrain.To(...)` and pass it either a `Rect` or an `EditorWindow`. |
| 25 | + |
| 26 | + |
| 27 | +Here's an example using a simple Rect |
| 28 | +```csharp |
| 29 | +private void Foo() |
| 30 | +{ |
| 31 | + Rect rect = new Rect(16, 16, 128, 128); |
| 32 | + |
| 33 | + Rect constrainedRect = Constrain.To(rect) |
| 34 | + .Left.Relative(8) |
| 35 | + .Top.Relative(16) |
| 36 | + .Right.Relative(24) |
| 37 | + .Bottom.Relative(32) |
| 38 | + .ToRect(); |
| 39 | + |
| 40 | + Debug.Log(constrainedRect.xMin); // Logs 24 |
| 41 | + Debug.Log(constrainedRect.yMin); // Logs 32 |
| 42 | + Debug.Log(constrainedRect.xMax); // Logs 104 |
| 43 | + Debug.Log(constrainedRect.yMax); // Logs 96 |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Aside from `Left`, `Top`, `Right`, and `Bottom`, you can also use `Width` and `Height`. These will **override** `Right` and `Bottom` respectively. |
| 48 | + |
| 49 | +Other modifiers are `Absolute` and `Percentage`. |
| 50 | +Absolute is what it suggests: instead of taking into account it's constraints it just returns the value given. |
| 51 | + |
| 52 | +Percentage expects a float value between 0 and 1 (inclusive) and multiplies that value with the constrained property. |
| 53 | +```csharp |
| 54 | +private void Foo() |
| 55 | +{ |
| 56 | + Rect rect = new Rect(16, 16, 128, 128); |
| 57 | + |
| 58 | + Rect constrainedRect = Constrain.To(rect) |
| 59 | + .Width.Percentage(0.5f) |
| 60 | + .ToRect(); |
| 61 | + |
| 62 | + Debug.Log(constrainedRect.width); // Logs 64 |
| 63 | +} |
| 64 | +``` |
| 65 | + |
| 66 | +## Contributing |
| 67 | +Pull requests are welcomed. Please feel free to fix any issues you find, or add new features. |
| 68 | + |
| 69 | +## Installing |
| 70 | +Installing Constrained Rect into your Unity3D project is done through the [Package Manager](https://docs.unity3d.com/Manual/Packages.html). |
| 71 | + |
| 72 | +You can either add the package manually to the [manifest.json](https://docs.unity3d.com/Manual/upm-dependencies.html) file: |
| 73 | +```json |
| 74 | +{ |
| 75 | + "dependencies": { |
| 76 | + "net.tnrd.constrainedrect": "https://github.com/thundernerd/unity3d-constrainedrect" |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +Or add it through the UI by selecting the **+ button** in the top left of the Package Manager, selecting _Add package from git URL..._, and pasting https://github.com/Thundernerd/Unity3D-ConstrainedRect.git in the input field. |
| 82 | + |
0 commit comments