Skip to content

Commit fbe337f

Browse files
committed
Added some extensions and helpers for the first version of the package
1 parent d9c1afa commit fbe337f

26 files changed

+521
-2
lines changed
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11
{
2-
"name": "OmegaLeo.Toolbox.Editor"
3-
}
2+
"name": "OmegaLeo.Toolbox.Editor",
3+
"rootNamespace": "",
4+
"references": [],
5+
"includePlatforms": [
6+
"Editor"
7+
],
8+
"excludePlatforms": [],
9+
"allowUnsafeCode": false,
10+
"overrideReferences": false,
11+
"precompiledReferences": [],
12+
"autoReferenced": true,
13+
"defineConstraints": [],
14+
"versionDefines": [],
15+
"noEngineReferences": false
16+
}

Runtime/Extensions.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using UnityEngine;
2+
3+
namespace OmegaLeo.Toolbox.Runtime.Extensions
4+
{
5+
public static class ColorExtensions
6+
{
7+
/// <summary>
8+
/// Converts a color to it's hexadecimal equivalent
9+
/// </summary>
10+
/// <param name="color"></param>
11+
/// <returns></returns>
12+
public static string ToHex(this Color color)
13+
{
14+
return "#" + ColorUtility.ToHtmlStringRGBA(color);
15+
}
16+
17+
/// <summary>
18+
/// Converts a hexadecimal color into a Color, if it can't parse correctly it'll return Black
19+
/// </summary>
20+
/// <param name="value"></param>
21+
/// <returns></returns>
22+
public static Color ColorFromHex(this string value)
23+
{
24+
if (ColorUtility.TryParseHtmlString(value, out var newCol))
25+
{
26+
return newCol;
27+
}
28+
29+
return Color.black;
30+
}
31+
}
32+
}

Runtime/Extensions/ColorExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using UnityEngine;
2+
3+
namespace OmegaLeo.Toolbox.Runtime.Extensions
4+
{
5+
public static class GameObjectExtensions
6+
{
7+
/// <summary>
8+
/// Checks whether a game object has a component of type T attached.
9+
/// </summary>
10+
/// <param name="gameObject">Game object.</param>
11+
/// <returns>True when component is attached.</returns>
12+
public static bool HasComponent<T>(this GameObject gameObject) where T : Component
13+
{
14+
return gameObject.GetComponent<T>() != null;
15+
}
16+
17+
/// <summary>
18+
/// Destroy all children of a GameObject
19+
/// </summary>
20+
/// <param name="parent"></param>
21+
public static void DestroyChildren(this GameObject parent)
22+
{
23+
Transform[] children = new Transform[parent.transform.childCount];
24+
25+
for (int i = 0; i < parent.transform.childCount; i++)
26+
{
27+
children[i] = parent.transform.GetChild(i);
28+
}
29+
30+
for (int i = 0; i < children.Length; i++)
31+
{
32+
Object.Destroy(children[i].gameObject);
33+
}
34+
}
35+
36+
/// <summary>
37+
/// Toggle the defined game object on and off
38+
/// </summary>
39+
/// <param name="gameObject"></param>
40+
public static void ToggleGameObject(this GameObject gameObject)
41+
{
42+
gameObject.SetActive(!gameObject.activeSelf);
43+
}
44+
45+
46+
}
47+
}

Runtime/Extensions/GameObjectExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace OmegaLeo.Toolbox.Runtime.Extensions
5+
{
6+
public static class MathExtensions
7+
{
8+
/// <summary>
9+
/// Calculate the average value based on a list of values
10+
/// </summary>
11+
/// <param name="list"></param>
12+
/// <returns></returns>
13+
public static int AverageWithNullValidation(this List<int> list)
14+
{
15+
return list.Count > 0 ? (int)list.Average() : 0;
16+
}
17+
18+
/// <summary>
19+
/// Calculate the average value based on a list of values
20+
/// </summary>
21+
/// <param name="list"></param>
22+
/// <returns></returns>
23+
public static double AverageWithNullValidation(this List<double> list)
24+
{
25+
return list.Count > 0 ? list.Average() : 0.0;
26+
}
27+
28+
/// <summary>
29+
/// Calculate the average value based on a list of values
30+
/// </summary>
31+
/// <param name="list"></param>
32+
/// <returns></returns>
33+
public static float AverageWithNullValidation(this List<float> list)
34+
{
35+
return list.Count > 0 ? list.Average() : 0.0f;
36+
}
37+
}
38+
}

Runtime/Extensions/MathExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace OmegaLeo.Toolbox.Runtime.Extensions
6+
{
7+
public static class SpriteExtensions
8+
{
9+
/// <summary>
10+
/// Split a Texture2D into multiple sprites of <paramref name="width"/> x <paramref name="height"/>
11+
/// </summary>
12+
/// <param name="texture">Original Texture</param>
13+
/// <param name="width">Width of the sprites</param>
14+
/// <param name="height">Height of the sprites</param>
15+
/// <param name="columns">How many columns of sprites(Texture width / <paramref name="width"/>)</param>
16+
/// <param name="rows">How many rows of sprites(Texture height / <paramref name="height"/>)</param>
17+
/// <returns></returns>
18+
public static List<Sprite> Split(this Texture2D texture, int width, int height, int columns, int rows)
19+
{
20+
return texture.ToSprite().Split(width, height, columns, rows);
21+
}
22+
23+
/// <summary>
24+
/// Split a Sprite into multiple sprites of <paramref name="width"/> x <paramref name="height"/>
25+
/// </summary>
26+
/// <param name="sprite">Original Sprite</param>
27+
/// <param name="width">Width of the sprites</param>
28+
/// <param name="height">Height of the sprites</param>
29+
/// <param name="columns">How many columns of sprites(<paramref name="sprite"/> width / <paramref name="width"/>)</param>
30+
/// <param name="rows">How many rows of sprites(<paramref name="sprite"/> height / <paramref name="height"/>)</param>
31+
/// <returns></returns>
32+
public static List<Sprite> Split(this Sprite sprite, int width, int height, int columns, int rows)
33+
{
34+
List<Sprite> sprites = new List<Sprite>();
35+
36+
var croppedTexture = sprite.texture;
37+
38+
for (int y = rows; y >= 0; y --)
39+
{
40+
for (int x = 0; x < columns; x ++)
41+
{
42+
try
43+
{
44+
Sprite newSprite = Sprite.Create(
45+
croppedTexture,
46+
new Rect(x * width, y * height, width, height),
47+
new Vector2(width / 2, height / 2));
48+
49+
sprites.Add(newSprite);
50+
}
51+
catch(Exception ex)
52+
{
53+
Debug.LogException(ex);
54+
}
55+
}
56+
}
57+
58+
return sprites;
59+
}
60+
}
61+
}

Runtime/Extensions/SpriteExtensions.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)