├── UnityApiExtensions.asmdef
├── CHANGELOG.md.meta
├── LICENSE.md.meta
├── README.md.meta
├── package.json.meta
├── Scripts.meta
├── Scripts
├── Editor.meta
├── Editor
│ ├── ExtensionsEditor.asmdef.meta
│ ├── MissingScriptsRemover.cs.meta
│ ├── ExtensionsEditor.asmdef
│ └── MissingScriptsRemover.cs
├── RayExtensions.cs.meta
├── CameraExtensions.cs.meta
├── Color32Extensions.cs.meta
├── ColorExtensions.cs.meta
├── DoubleExtensions.cs.meta
├── FloatExtensions.cs.meta
├── GraphicExtensions.cs.meta
├── IListExtensions.cs.meta
├── ObjectExtensions.cs.meta
├── RectExtensions.cs.meta
├── SceneExtensions.cs.meta
├── SceneReference.cs.meta
├── Vector2Extensions.cs.meta
├── Vector3Extensions.cs.meta
├── Vector4Extensions.cs.meta
├── CanvasScalerExtensions.cs.meta
├── ComponentExtensions.cs.meta
├── GameObjectExtensions.cs.meta
├── IComparableExtensions.cs.meta
├── IEnumerableExtensions.cs.meta
├── IEquatableExtensions.cs.meta
├── QuaternionExtensions.cs.meta
├── RectTransformExtensions.cs.meta
├── RenderTextureExtensions.cs.meta
├── TransformExtensions.cs.meta
├── Vector2IntExtensions.cs.meta
├── Vector3IntExtensions.cs.meta
├── CanvasScalerExtensions.cs
├── IComparableExtensions.cs
├── ObjectExtensions.cs
├── IEquatableExtensions.cs
├── FloatExtensions.cs
├── DoubleExtensions.cs
├── ComponentExtensions.cs
├── SceneExtensions.cs
├── RenderTextureExtensions.cs
├── RayExtensions.cs
├── GameObjectExtensions.cs
├── SceneReference.cs
├── RectExtensions.cs
├── IListExtensions.cs
├── GraphicExtensions.cs
├── ColorExtensions.cs
├── Color32Extensions.cs
├── Vector2IntExtensions.cs
├── IEnumerableExtensions.cs
├── Vector2Extensions.cs
├── RectTransformExtensions.cs
├── QuaternionExtensions.cs
├── Vector3IntExtensions.cs
└── CameraExtensions.cs
├── UnityApiExtensions.asmdef.meta
├── package.json
├── CHANGELOG.md
├── .gitignore
├── README.md
└── LICENSE.md
/UnityApiExtensions.asmdef:
--------------------------------------------------------------------------------
1 | {
2 | "name": "UnityApiExtensions"
3 | }
4 |
--------------------------------------------------------------------------------
/CHANGELOG.md.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 59b952c0c94bf48f6aa85fcefb18a9ee
3 | TextScriptImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/LICENSE.md.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 01d121959599145239331405a9e0213e
3 | TextScriptImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/README.md.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 9632a9f444840a54ba10a65fe32f53d4
3 | TextScriptImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/package.json.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 8bbc4d4b5c0a045ca93b9bcb3c859492
3 | PackageManifestImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/Scripts.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: f664c25ea3212406aaf11218628e9deb
3 | folderAsset: yes
4 | DefaultImporter:
5 | externalObjects: {}
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Scripts/Editor.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: d742424b27c00be46acef63aecb2bca0
3 | folderAsset: yes
4 | DefaultImporter:
5 | externalObjects: {}
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/UnityApiExtensions.asmdef.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: deb967d224d3144cea6d67fbcbcf76f1
3 | AssemblyDefinitionImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/Scripts/Editor/ExtensionsEditor.asmdef.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: cd2ef4a1fbbebca45bd1fc8a08112282
3 | AssemblyDefinitionImporter:
4 | externalObjects: {}
5 | userData:
6 | assetBundleName:
7 | assetBundleVariant:
8 |
--------------------------------------------------------------------------------
/Scripts/RayExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 806de8bab26686340b1c1d5ab89e6d16
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/CameraExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 011c57431a922f94b9c4c7ec1228865f
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Color32Extensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: e9c3dd36d0d04234d9552bc03d712005
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/ColorExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 46af64433c4ccc24ca86f372a1016839
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/DoubleExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 17da1c7c6dda4ed4089383010e1772bf
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/FloatExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 30859a8f19fdb8c4fa5688034ba0610b
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/GraphicExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: cf5e11ae54ae07e41af44898e1fcf1a3
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/IListExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: f82871def1670ca418c5fb2b5fecfa3d
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/ObjectExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: bbb53fa295c8a0642946e97049c5ec09
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/RectExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 9dfdc041ba0f3854583f5647b697919b
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/SceneExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: a598fe46620e6c048a416c673069442b
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/SceneReference.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 43096ef4197f5334ca1912ce59037963
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Vector2Extensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: febd20201d83e6544b100e80b55095cc
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Vector3Extensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 40fb2d2ed19fcc74eb3156df03336846
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Vector4Extensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 51bdad0bd776d1941bf91a499f2ccf28
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/CanvasScalerExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 218fc879fd65fc44ead13258606df00d
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/ComponentExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: ca8764fd58b995f43a113e4be8d79d28
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/GameObjectExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 309420a6293e10c4a8287f7e4c448ac7
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/IComparableExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: c7adbebcfa259ff40b1c501abdbc0f23
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/IEnumerableExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: d2aec9d7292ca844e86d0a91cb62a2b0
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/IEquatableExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: ba66dc7433551374f8b3e29a4e1c437c
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/QuaternionExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: c51c5ae7b71fcc7458fc3cc11199cb09
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/RectTransformExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: b2b0702da89a03c41a1c575e512e700f
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/RenderTextureExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 90d11e24769720740ac5eb51349bde1a
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/TransformExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: cebf6ef0a90a05d48b301e1ed031f0ec
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Vector2IntExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 4dc2aec85a834354c8b5796859ec0685
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Vector3IntExtensions.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: c17a3e6254e91bd4a91c1b6ec21a6558
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/Scripts/Editor/MissingScriptsRemover.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: baf71069a799ec344b67fc06f9b21182
3 | MonoImporter:
4 | externalObjects: {}
5 | serializedVersion: 2
6 | defaultReferences: []
7 | executionOrder: 0
8 | icon: {instanceID: 0}
9 | userData:
10 | assetBundleName:
11 | assetBundleVariant:
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "com.codomaster.extensions",
3 | "version": "1.1.1",
4 | "displayName": "Unity API Extensions",
5 | "description": "This package contains a lot of extension methods for unity, which make developing more convenient.",
6 | "unity": "2021.1",
7 | "author": {
8 | "name": "codomaster ",
9 | "email": "codomaster.me@gmail.com",
10 | "url": "https://t.me/codomaster"
11 | }
12 | }
--------------------------------------------------------------------------------
/Scripts/Editor/ExtensionsEditor.asmdef:
--------------------------------------------------------------------------------
1 | {
2 | "name": "ExtensionsEditor",
3 | "rootNamespace": "",
4 | "references": [
5 | "GUID:b52c6daf0f9f3974fb04e400a06c9369"
6 | ],
7 | "includePlatforms": [
8 | "Editor"
9 | ],
10 | "excludePlatforms": [],
11 | "allowUnsafeCode": false,
12 | "overrideReferences": false,
13 | "precompiledReferences": [],
14 | "autoReferenced": true,
15 | "defineConstraints": [],
16 | "versionDefines": [],
17 | "noEngineReferences": false
18 | }
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # v1.1.0
2 | ### Fixes and new methods.
3 | - `GetRandomElement` methods renamed to `GetRandom`.
4 | - `Except` and `Shuffled` methods now return list.
5 | - Added `Shuffle` and `Swap` methods to `IList`.
6 | - Added `AsRect` method to `Transform`. Added `Rotate` method to `Vector2`.
7 |
8 | # v1.0.6
9 | ### fixes and updates:
10 | - Added support for `Approx` and `WithRandomSign` methods to `double`.
11 | - Remove `MaxComponentIndex` and `MinComponentIndex` methods, now just use `Max/MinComponent` instead.
12 | - Small fixes.
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /[Ll]ibrary/
2 | /[Tt]emp/
3 | /[Oo]bj/
4 | /[Bb]uild/
5 | /[Bb]uilds/
6 | /[Uu][Ww][Pp]/
7 | /[Ll]ogs/
8 | /[Aa]ssets/[Aa]ssetStoreTools*
9 | /[Uu]serSettings
10 |
11 | # Visual Studio 2015 cache directory and files
12 | /.vs/
13 | /.vscode/
14 | /.idea/
15 | /.vsconfig
16 |
17 | # Autogenerated solution and project files
18 | ExportedObj/
19 | .consulo/
20 | *.csproj
21 | *.unityproj
22 | *.sln
23 | *.suo
24 | *.tmp
25 | *.user
26 | *.userprefs
27 | *.pidb
28 | *.booproj
29 | *.svd
30 | *.pdb
31 |
32 |
33 | # Unity3D generated meta files
34 | *.pidb.meta
35 |
36 | # Unity3D Generated File On Crash Reports
37 | sysinfo.txt
38 |
39 | # Builds
40 | *.apk
41 | *.unitypackage
--------------------------------------------------------------------------------
/Scripts/CanvasScalerExtensions.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using UnityEngine.UI;
3 |
4 | namespace Codomaster.Extensions
5 | {
6 | public static class CanvasScalerExtensions
7 | {
8 | ///
9 | /// Get scale factor which canvas scaler calculated when work in mode.
10 | ///
11 | /// The canvas scaler.
12 | /// Calculated scale factor.
13 | public static float GetScaleFactor(this CanvasScaler scaler)
14 | {
15 | return Mathf.Lerp(Screen.width / scaler.referenceResolution.x, Screen.height / scaler.referenceResolution.y, scaler.matchWidthOrHeight);
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/Scripts/IComparableExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Codomaster.Extensions
4 | {
5 | public static class IComparableExtensions
6 | {
7 | ///
8 | /// Checks if the object is on the specified interval.
9 | ///
10 | /// Value type.
11 | /// Target value.
12 | /// Interval's start value.
13 | /// Interval's end value.
14 | /// Is the beginning of the interval included?
15 | /// Is the end of the interval included?
16 | /// if the is between and .
17 | public static bool IsBetween(this T value, T a, T b, bool aInclusive = true, bool bInclusive = true) where T: IComparable
18 | {
19 | if (a.CompareTo(b) == 1)
20 | {
21 | (a, b) = (b, a);
22 | (aInclusive, bInclusive) = (bInclusive, aInclusive);
23 | }
24 |
25 | return (aInclusive ? value.CompareTo(a).EqualsToAny(0, 1) : value.CompareTo(a) == 1) && (bInclusive ? value.CompareTo(b).EqualsToAny(-1, 0) : value.CompareTo(b) == -1);
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/Scripts/ObjectExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 |
3 | namespace Codomaster.Extensions
4 | {
5 | public static class ObjectExtensions
6 | {
7 | ///
8 | /// Checks if the equals to all elements of array.
9 | ///
10 | /// Source type.
11 | /// Object to compare.
12 | /// Array with objects to compare.
13 | /// if the all element of are equals to
14 | public static bool EqualsToAll(this object obj, params object[] objects) => objects.All(o => o.Equals(obj));
15 |
16 | ///
17 | /// Checks if the equals to at least one of elements of array.
18 | ///
19 | /// Source type.
20 | /// Object to compare.
21 | /// Array with objects to compare.
22 | /// if at least one of all elements of are equals to .
23 | public static bool EqualsToAny(this object obj, params object[] objects) => objects.Any(o => o.Equals(obj));
24 | }
25 | }
--------------------------------------------------------------------------------
/Scripts/IEquatableExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Linq;
3 |
4 | namespace Codomaster.Extensions
5 | {
6 | public static class IEquatableExtensions
7 | {
8 | ///
9 | /// Checks if the equals to all elements of array.
10 | ///
11 | /// Source type.
12 | /// Object to compare.
13 | /// Array with objects to compare.
14 | /// if the all element of are equals to .
15 | public static bool EqualsToAll(this T value, params T[] values) where T : IEquatable => values.All(o => o.Equals(value));
16 |
17 | ///
18 | /// Checks if the equals to at least one of elements of array.
19 | ///
20 | /// Source type.
21 | /// Object to compare.
22 | /// Array with objects to compare.
23 | /// if at least one of all elements of are equals to .
24 | public static bool EqualsToAny(this T value, params T[] values) where T : IEquatable => values.Any(o => o.Equals(value));
25 | }
26 | }
--------------------------------------------------------------------------------
/Scripts/FloatExtensions.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 |
3 | namespace Codomaster.Extensions
4 | {
5 | public static class FloatExtensions
6 | {
7 | ///
8 | /// Remap a value from one range to another.
9 | ///
10 | /// Source value.
11 | /// Source min.
12 | /// Source max.
13 | /// Target min.
14 | /// Target max
15 | /// Remaped value.
16 | public static float Remap(this float value, float min1, float max1, float min2, float max2)
17 | {
18 | return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
19 | }
20 |
21 | ///
22 | /// Checks for approximate equality with .
23 | ///
24 | /// First operand.
25 | /// Second operand.
26 | /// if the variables are the same.
27 | public static bool Approx(this float value, float other) => Mathf.Approximately(value, other);
28 |
29 | ///
30 | /// Randomly changes the sign (+ or -) of .
31 | ///
32 | /// Target value.
33 | /// Value with randomed sign.
34 | public static float WithRandomSign(this float value) => value * (Random.Range(0, 2) * 2 - 1);
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/Scripts/DoubleExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using UnityEngine;
3 | using Random = UnityEngine.Random;
4 |
5 | namespace Codomaster.Extensions
6 | {
7 | public static class DoubleExtensions
8 | {
9 | ///
10 | /// Remap a value from one range to another.
11 | ///
12 | /// Source value.
13 | /// Source min.
14 | /// Source max.
15 | /// Target min.
16 | /// Target max
17 | /// Remaped value.
18 | public static double Remap(this double value, double min1, double max1, double min2, double max2)
19 | {
20 | return min2 + (value - min1) * (max2 - min2) / (max1 - min1);
21 | }
22 |
23 | ///
24 | /// Checks for approximate equality with and .
25 | ///
26 | /// First operand.
27 | /// Second operand.
28 | /// If difference between and is smaller than this, then result is true.
29 | /// if the variables are the same.
30 | public static bool Approx(this double value, double other, double minDifference = 1E-17) => Math.Abs(value - other) <= minDifference;
31 |
32 | ///
33 | /// Randomly changes the sign (+ or -) of .
34 | ///
35 | /// Target value.
36 | /// Value with randomed sign.
37 | public static double WithRandomSign(this double value) => value * (Random.Range(0, 2) * 2 - 1);
38 | }
39 | }
--------------------------------------------------------------------------------
/Scripts/ComponentExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 | using UnityEngine;
4 |
5 | namespace Codomaster.Extensions
6 | {
7 | public static class ComponentExtensions
8 | {
9 | ///
10 | /// Gets component from 's game object. If it no exist, new one will be created.
11 | ///
12 | /// Component type.
13 | /// The component.
14 | /// Game object's component.
15 | public static T GetOrAddComponent(this Component component) where T : Component
16 | {
17 | return component.gameObject.GetOrAddComponent();
18 | }
19 |
20 | ///
21 | /// Trying to get component in this or any it's children.
22 | ///
23 | /// Component type.
24 | /// The component.
25 | /// Target component.
26 | /// Should we find component on inactive game objects?
27 | /// if component was found.
28 | public static bool TryGetComponentInChildren(this Component sourceComponent, out T component, bool includeInactive = false) where T : Component
29 | {
30 | return component = sourceComponent.gameObject.GetComponentInChildren(includeInactive);
31 | }
32 |
33 | ///
34 | /// Trying to get component in this or any it's parent.
35 | ///
36 | /// Component type.
37 | /// The component.
38 | /// Target component.
39 | /// Should we find component on inactive game objects?
40 | /// if component was found.
41 | public static bool TryGetComponentInParent(this Component sourceComponent, out T component, bool includeInactive = false) where T : Component
42 | {
43 | return component = sourceComponent.gameObject.GetComponentInParent(includeInactive);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/Scripts/SceneExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 | using UnityEngine;
3 | using UnityEngine.SceneManagement;
4 |
5 | namespace Codomaster.Extensions
6 | {
7 | public static class SceneExtensions
8 | {
9 | ///
10 | /// Finds game object in scene with component.
11 | ///
12 | /// Component type.
13 | /// Target scene.
14 | /// Should we find inactive game objects?
15 | /// Founded game object.
16 | public static T FindObjectOfType(this Scene scene, bool includeInactive = false) where T : Component
17 | {
18 | foreach (var root in scene.GetRootGameObjects().Where(r => r.activeSelf || includeInactive))
19 | {
20 | if (root.TryGetComponentInChildren(out T component, includeInactive))
21 | return component;
22 | }
23 |
24 | return null;
25 | }
26 |
27 | ///
28 | /// Finds game objects in scene with component.
29 | ///
30 | /// Component type.
31 | /// Target scene.
32 | /// Should we find inactive game objects?
33 | /// Founded game objects.
34 | public static T[] FindObjectsOfType(this Scene scene, bool includeInactive = false)
35 | {
36 | var filtered = scene.GetRootGameObjects().Where(r => r.activeSelf || includeInactive);
37 | return filtered.SelectMany(go => go.GetComponentsInChildren(includeInactive)).ToArray();
38 | }
39 |
40 | ///
41 | /// Counts the whole number of game objects in scene.
42 | ///
43 | /// Target scene.
44 | /// Should we count inactive game objects?
45 | ///
46 | public static int ObjectsCount(this Scene scene, bool includeInactive = false)
47 | {
48 | var filtered = scene.GetRootGameObjects().Where(r => r.activeSelf || includeInactive);
49 | return filtered.SelectMany(r => r.GetComponentsInChildren(includeInactive)).Count();
50 | }
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Scripts/RenderTextureExtensions.cs:
--------------------------------------------------------------------------------
1 | using System.Collections;
2 | using System.Collections.Generic;
3 | using UnityEngine;
4 |
5 | namespace Codomaster.Extensions
6 | {
7 | public static class RenderTextureExtensions
8 | {
9 | ///
10 | /// Create texture and write to it.
11 | ///
12 | /// The render texture.
13 | /// Created texture.
14 | public static Texture2D ToTexture2D(this RenderTexture renderTexture, TextureFormat format)
15 | {
16 | var texture = new Texture2D(renderTexture.width, renderTexture.height, format, false);
17 | renderTexture.WriteToTexture2D(texture);
18 |
19 | return texture;
20 | }
21 |
22 | ///
23 | /// Write to .
24 | ///
25 | /// The render texture.
26 | /// Texture to write render texture.
27 | public static void WriteToTexture2D(this RenderTexture renderTexture, Texture2D texture)
28 | {
29 | var oldRenderTexture = RenderTexture.active;
30 | RenderTexture.active = renderTexture;
31 |
32 | texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0);
33 | texture.Apply();
34 |
35 | RenderTexture.active = oldRenderTexture;
36 | }
37 |
38 | ///
39 | /// Create sprite and write to it.
40 | ///
41 | /// The render texture.
42 | /// Created sprite.
43 | public static Sprite ToSprite(this RenderTexture renderTexture, TextureFormat format)
44 | {
45 | var texture = renderTexture.ToTexture2D(format);
46 | return Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), new Vector2(0.5f, 0.5f));
47 | }
48 |
49 | ///
50 | /// Write to .
51 | ///
52 | /// The render texture.
53 | /// Sprite to write render texture.
54 | public static void WriteToSprite(this RenderTexture renderTexture, Sprite sprite)
55 | {
56 | renderTexture.WriteToTexture2D(sprite.texture);
57 | }
58 | }
59 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Extensions is a package that contains many extension methods for popular classes in Unity that you can use to write more expressive and concise code.
2 |
3 | For example:
4 | ```C#
5 | // Setting a new global position along the desired axis.
6 | transform.SetPositionX(1f);
7 |
8 | // Setting local rotation through Euler angles along the desired axex.
9 | transform.SetLocalEulerAnglesXZ(45f, 90f);
10 |
11 | // Reset the transformation state to default values (position and rotation to zero, scale to one).
12 | transform.Reset();
13 |
14 | // Moves one position down in the hierarchy.
15 | transform.SetToNextSibling();
16 |
17 | // Returns all child objects.
18 | var childs = transform.GetChilds();
19 |
20 | // Will delete all child objects based on condition.
21 | transform.DestroyChildsWhere(c => c.name == "Enemy");
22 |
23 | // Assign a position with a zeroed value along the Y axis.
24 | transform.position = otherTransform.position.WithY(0f);
25 |
26 | // Create and return a new vector from components [z, x, y]
27 | transform.position = vector.GetZXY();
28 |
29 | // Inserts the value 1f along the Y axis. From [x, y] You get [x, 1f, y].
30 | transform.position = vector2.InsertY(1f);
31 |
32 | // Partially inverts a vector:
33 | var partialNegated = transform.position.WithNegateXZ();
34 |
35 | // Will check if the vector is homogeneous:
36 | var isUniform = transform.localScale.IsUniform();
37 |
38 | // Returns the point closest to the vector (used as the position in space) from the specified list.
39 | var closest = transform.position.GetClosestPoint(pointsList);
40 | print($"Point: {closest.point}\nIndex: {closest.index}");
41 |
42 | // We get a return beam, the beginning of which begins at 10 meters along the original beam.
43 | var reversed = ray.Reversed(10f);
44 |
45 | // Returns a ray deviated from its original direction by a random angle.
46 | var deflected = ray.RandomDeflected(45f);
47 |
48 | // Returns a copy with the color channel value changed.
49 | image.color = someColor.WithGA(1f, 0.5f);
50 |
51 | // Will reassign a value in one range to a value in another.
52 | var x = 0.25f;
53 | var remapped = x.Remap(0f, 1f, 0f, 2f);
54 |
55 | // Returns a random element.
56 | var element = list.GetRandom();
57 | var elements = list.GetRandoms(3);
58 |
59 | // Will return a random element according to their probabilities (in this example: hello = 35%, bye = 50%, ok = 25%).
60 | var words = new string[] {"hello", "bye", "ok"};
61 | var random = words.GetRandomWithProbability(35f, 50f, 25f);
62 |
63 | // Returns a shuffled list.
64 | var shuffled = list.Shuffled();
65 |
66 | // Converts any collection to a string in the format "[1, 2, 3]".
67 | var array = new int[] { 4, -7, 12, 1, 0 };
68 | print(array.AsString()); // Prints to the console -> [4, -7, 12, 1, 0]
69 |
70 | // Retrieves an element from a list. You can retrieve multiple items at once.
71 | var popped = list.Pop(4); // Retrieved the element at index 4.
72 |
73 | // Retrieves random elements from a list.
74 | var popped = list.PopRandoms(2); // Extracted 2 random elements.
75 | ```
76 |
77 | And many other extensions! Read more on the wiki page
78 |
--------------------------------------------------------------------------------
/Scripts/Editor/MissingScriptsRemover.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 | using System.Linq;
3 | using UnityEngine;
4 | using UnityEditor;
5 | public static class FindMissingScriptsRecursively
6 | {
7 | private static float _lastCallTime = 0f;
8 |
9 | [MenuItem("Assets/Remove Missing Scripts")]
10 | [MenuItem("GameObject/Remove Missing Scripts")]
11 | private static void FindAndRemoveMissingInSelected()
12 | {
13 | if (Mathf.Approximately(Time.unscaledTime, _lastCallTime))
14 | return;
15 |
16 | var selected = Selection.gameObjects.SelectMany(go => go.GetComponentsInChildren(true)).Select(t => t.gameObject);
17 | var prefabs = new HashSet