Unity Asset Store:
Extensions & Helpers
Description
A collection of extensions and helper tools for any project.
Features
- Adjustments: Quick Vector adjustments: Set|Increase|Decrease[X|Y|Z]
- Conversions: From/to float arrays
- Distance: InRange, FlatDistance, Closest, Farthest
- FileHandler: Save/Load savegames to binary files
- Ground: PlaceOnGround, GetGroundPosition
- MapValue: Clamped and Unclamped mapping of floats
- RandomPosition: Random point around a Vector3 in a given radius
- Rounding: Round(2.5f==2) + RoundExact(2.5f==3)
- SnapToGrid: Snap a float/Vector2 or Vector3 to a given gridSize
- String: StartsWith, EndsWith, TrimStart, TrimEnd (extending C# for easy string usage)
- Some MonoBehaviour scripts for quick setups without coding
Demos + UnitTests
You don't have to import the Demos + Tests folders, but there are some examples in there how to use the methods (for example the 'MapValue' to calculate percentages or amount of stars).
Installation
1. Download the latest package
2. Import the files (optional: you don't really need the 'Demos' and 'Scripts/Tests' folders, but could be useful to find examples)
3. If you are using an Assembly Definition file: go to your Assembly Definition and add the 'MysticEraGames' definition to the 'Assembly Definition References', and click 'Apply' at the bottom
4. Add this to the top of your script: using MysticEraGames.Extensions;
5. Try it out: bool inRange = Vector3.zero.InRange(Vector3.forward, 5f);
In the top Menu you can toggle all the Scene Gizmo 3D Icons at once for all 'MEG*' scripts (just note: directly after importing the package, the icons are not 'visible' yet to the script, just toggle the Gizmo view once and try again)
For more information and documentation:
https://www.mysticeragames.com/assets/extensions-helpers
Troubleshooting
For any issues or questions, please mail to [email protected]
( Please use something like 'Unity Asset Store - QUESTION' in the subject title )
Release Notes
v1.2.0 - Jul 21, 2023
Added Scripts to add directly to MonoBehaviour, the triggers can be used to quickly setup a Demo Scene without adding any other scripts (see Demo of Ground Position).
- Added MEGGizmoLabel (Show a label in the editor when Gizmos are activated)
- Added MEGMaterialInstanceColor (to quickly setup Demos, without adding a bunch of Materials)
- Added MEGPlaceOnGround (Place an object on the ground)
- Added MEGTriggerOnAwake
- Added MEGTriggerOnDestroy
- Added MEGTriggerOnDisable
- Added MEGTriggerOnEnable
- Added MEGTriggerOnFixedUpdate
- Added MEGTriggerOnLateUpdate
- Added MEGTriggerOnStart
- Added MEGTriggerOnUpdate
- Replaced some Demo scripts with Event Triggers
- Added logo for MonoBehaviour scripts
- Added Menu entry: Tools > MEG | Mystic ERA games (to quickly disable the icons in the Scene view) (only for 2022.1 or newer)
- Added GuiStyleLabel, to quickly create a label for the Gizmo view
- Include the Menu ability to disable/enable all Gizmo 3D Icons also for Unity versions lower then 2022 (with Reflection)
- Make sure the project can be build (added UNITY_EDITOR checks)
- Bugfix: BoxColliders magically grow twice the size when importing the package in Unity version lower then 2022 (where it was build in), added a script in the Demo Scene to fix that.
v1.1.0 - Jul 19, 2023
- Added 2 Demo Scenes
- Performance improvement for Closest/Farthest/InRange checks (using sqrMagnitude)
- Added Binary FileHandler for savegames
- Added common serialization for FileHandler (Vector2/Vector3/Quaternion)
- Added extension methods: Ground Raycast (+PlaceOnGround)
- Added extension methods: Random Position
- Added extension methods: Conversions
- Added extension methods: Adjustments
- Added extension methods: Round(2.5f==2) + RoundExact(2.5f==3)
- Renamed 'RemapValue' to 'MapValue'
- Using stricter namespace (using MysticEraGames.Extensions)
- Added UnitTests
v1.0.0 - Jun 17, 2023
- First release
Helper tools
FileHandlerBinary
Usage:
[Serializable]
public class MySaveGameClass
{
public Vector3 PlayerPosition;
public Quaternion PlayerRotation;
}
// Save
string path = ... // your savegame path
IFileHandler fileHandler = new FileHandlerBinary();
bool isSaved = fileHandler.SaveToFile(MySaveGameClass, path, out Exception exception);
// Load
string path = ... // your savegame path
IFileHandler fileHandler = new FileHandlerBinary();
bool isLoaded = fileHandler.LoadFromFile(path, out MySaveGameClass loadedSaveGame, out Exception loadException);
MonoBehaviour Scripts
MEGGizmoLabel - Show a label in the editor when Gizmos are activated
MEGMaterialInstanceColor - to quickly setup Demos, without adding a bunch of Materials
MEGPlaceOnGround - Place an object on the ground
MEGTriggerOnAwake - Trigger a Unity Event on Awake (no code needed, useful for quick demos)
MEGTriggerOnDestroy - Trigger a Unity Event on Destroy (no code needed, useful for quick demos)
MEGTriggerOnDisable - Trigger a Unity Event on Disable (no code needed, useful for quick demos)
MEGTriggerOnEnable - Trigger a Unity Event on Enable (no code needed, useful for quick demos)
MEGTriggerOnFixedUpdate - Trigger a Unity Event on FixedUpdate (no code needed, useful for quick demos)
MEGTriggerOnLateUpdate - Trigger a Unity Event on LateUpdate (no code needed, useful for quick demos)
MEGTriggerOnStart - Trigger a Unity Event on Start (no code needed, useful for quick demos)
MEGTriggerOnUpdate - Trigger a Unity Event on Update (no code needed, useful for quick demos)
Extensions
Extensions: Distance
public static float Distance(this float source, float target) - Calculate the distance to the target
public static float Distance(this Vector2 source, Vector2 target) - Calculate the distance to the target, Alias for: Vector2.Distance(source, target)
public static bool InRange(this Vector2 source, Vector2 target, float range) - Is the target in range?
public static float Distance(this Vector3 source, Vector3 target) - Calculate the distance to the target, Alias for: Vector3.Distance(source, target)
public static float FlatDistance(this Vector3 source, Vector3 target) - Calculate the distance to the target without the height (y)
public static bool InRange(this Vector3 source, Vector3 target, float range) - Is the target in range?
public static bool InFlatRange(this Vector3 source, Vector3 target, float range) - Is the target in flat range? (ignore the height)
public static Transform ClosestTransform(this Vector3 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the closest Transform
public static Component ClosestComponent(this Vector3 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the closest Component
public static Transform ClosestTransformFlat(this Vector3 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the closest Transform in 2D (ignoring height)
public static Transform ClosestTransform(this Vector2 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the closest Transform in 2D
public static Component ClosestComponentFlat(this Vector3 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the closest Component in 2D (ignoring height)
public static Component ClosestComponent(this Vector2 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the closest Component in 2D
public static Transform FarthestTransform(this Vector3 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the farthest Transform
public static Component FarthestComponent(this Vector3 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the farthest Component
public static Transform FarthestTransformFlat(this Vector3 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the farthest Transform in 2D (ignoring height)
public static Transform FarthestTransform(this Vector2 source, Transform[] elements, float maxRange = Mathf.Infinity) - Find the farthest Transform in 2D
public static Component FarthestComponentFlat(this Vector3 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the farthest Component in 2D (ignoring height)
public static Component FarthestComponent(this Vector2 source, Component[] elements, float maxRange = Mathf.Infinity) - Find the farthest Component in 2D
Extensions: Ground
public static void PlaceOnGround(this Transform source, LayerMask layerMask, float offsetHeight = 0f, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity, bool skipSelfCheck = true) - Place the transform on the ground using Raycast
public static void PlaceOnGround(this Transform source, float offsetHeight = 0f, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity, bool skipSelfCheck = true) - Place the transform on the ground using Raycast (use Default layerMask as ground)
public static bool GetGroundPosition(this Vector3 source, out Vector3 groundPosition, LayerMask layerMask, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity) - Get the ground position using Raycast
public static bool GetGroundPosition(this Vector3 source, out Vector3 groundPosition, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity) - Get the ground position using Raycast (use Default layerMask as ground)
public static bool GetGroundPositionWithoutSelf(this Transform source, out Vector3 groundPosition, LayerMask layerMask, float rayStartOffset = 0, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity) - Get the ground position without itself using Raycast
public static bool GetGroundPositionWithoutSelf(this Transform source, out Vector3 groundPosition, float rayStartOffset = 0, float nextCheckFromOffset = 100f, float maxRayDistance = Mathf.Infinity) - Get the ground position without itself using Raycast (use Default layerMask as ground)
public static bool GetRaycastHit(this Vector3 source, out RaycastHit hit, LayerMask layerMask, float maxRayDistance = Mathf.Infinity) - Cast a Ray from a Vector3
public static bool GetRaycastHitWithoutSelf(this Transform source, out RaycastHit hit, LayerMask layerMask, float rayStartOffset = 0, float maxRayDistance = Mathf.Infinity) - Cast a Ray from a Transform return the first hit (without hitting itself)
Extensions: MapValue
public static float MapValue(this float value, float originalMinValue, float originalMaxValue, float newMinValue, float newMaxValue) - Map a value to a new value (Clamp between newMinValue and newMaxValue)
public static float MapValueUnClamped(this float value, float originalMinValue, float originalMaxValue, float newMinValue, float newMaxValue) - Map a value to a new value
Extensions: Adjustments
public static Vector2 SetX(this Vector2 position, float x)
public static Vector2 SetY(this Vector2 position, float y)
public static Vector2 IncreaseX(this Vector2 position, float x)
public static Vector2 IncreaseY(this Vector2 position, float y)
public static Vector2 DecreaseX(this Vector2 position, float x)
public static Vector2 DecreaseY(this Vector2 position, float y)
public static Vector3 SetX(this Vector3 position, float x)
public static Vector3 SetY(this Vector3 position, float y)
public static Vector3 SetZ(this Vector3 position, float z)
public static Vector3 IncreaseX(this Vector3 position, float x)
public static Vector3 IncreaseY(this Vector3 position, float y)
public static Vector3 IncreaseZ(this Vector3 position, float z)
public static Vector3 DecreaseX(this Vector3 position, float x)
public static Vector3 DecreaseY(this Vector3 position, float y)
public static Vector3 DecreaseZ(this Vector3 position, float z)
Extensions: Conversions
public static float[] ToFloat(this Vector2 position)
public static Vector2 ToVector2(this float[] position)
public static float[] ToFloat(this Vector3 position)
public static Vector3 ToVector3(this float[] position)
public static Vector2 ToVector2(this Vector3 position)
public static float[] ToFloat(this Quaternion rotation)
public static Quaternion ToQuaternion(this float[] rotation)
Extensions: Material
public static int[] GetInstanceIds(this Material[] materials) - Create an array with all the material instance ids
public static string GetInstanceIdsString(this Material[] materials, string separator = "_") - Create a string with all the material instance ids (seperated by an underscore by default)
Extensions: RandomPosition
public static Vector3 GetRandomPosition(this Vector3 origin, float minRadius, float maxRadius = 0)
Extensions: Round
return Mathf.Round(value); - Round a value, alias for: Mathf.Round (note: 2.5f.Round() == 2 && 2.5f.RoundStrict() == 3)
public static float RoundStrict(this float value) - Round a value with MidpointRounding.AwayFromZero (note: 2.5f.Round() == 2 && 2.5f.RoundStrict() == 3)
Extensions: SnapToGrid
public static float SnapToGrid(this float value, float gridSize = .5f) - Snap a float to a given grid size (returns a new float)
public static Vector2 SnapToGrid(this Vector2 position, float gridSize = .5f) - Snap a Vector2 to a given grid size (returns a new Vector2)
public static Vector3 SnapToGrid(this Vector3 position, float gridSize = .5f, bool height = true) - Snap a Vector3 to a given grid size (returns a new Vector3)
Extensions: String
public static bool StartsWith(this string source, string[] values) - Determines whether the beginning of this string instance matches any of the specified string (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.startswith but with an array of strings))
public static bool EndsWith(this string source, string[] values) - Determines whether the end of this string instance matches any of the specified strings. (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.endswith but with an array of strings)
public static string TrimStart(this string source, string trimString) - Removes all the leading occurrences of a specified string from the current string. (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.trimstart but with a string)
public static string TrimStart(this string source, string[] trimStrings) - Removes all the leading occurrences of a set of strings specified in an array from the current string. (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.trimstart but with an array of strings)
public static string TrimEnd(this string source, string trimString) - Removes all the trailing occurrences of a string from the current string. (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.trimend but with a string)
public static string TrimEnd(this string source, string[] trimStrings) - Removes all the trailing occurrences of a set of strings specified in an array from the current string. (Like https://learn.microsoft.com/en-us/dotnet/api/system.string.trimend but with an array of strings)