Skip to content

Commit a1ad48b

Browse files
committed
Added Initial Files
1 parent a8887c0 commit a1ad48b

33 files changed

+522
-0
lines changed

Editor/AssetImporter.Extensions.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#region Header
2+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
3+
// ** Github Profile: https://github.com/LTMX
4+
// ** Repository : https://github.com/LTMX/Unity.Athena
5+
#endregion
6+
7+
#if UNITY_EDITOR
8+
9+
using UnityEditor;
10+
using UnityEngine;
11+
12+
public static class AssetImporterExtensions
13+
{
14+
// Asset Importer Stuff
15+
public static void Import(this Texture2D t) => AssetDatabase.ImportAsset(t.GetPath());
16+
public static void Import<T>(this T t) where T : Object => AssetDatabase.ImportAsset(t.GetPath());
17+
public static void Import(this AssetImporter t) => AssetDatabase.ImportAsset(t.assetPath);
18+
public static void ImportAndRefresh(this AssetImporter t) {
19+
AssetDatabase.ImportAsset(t.assetPath);
20+
AssetDatabase.Refresh();
21+
}
22+
}
23+
24+
#endif

Editor/AssetImporter.Extensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#region Header
2+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
3+
// ** Github Profile: https://github.com/LTMX
4+
// ** Repository : https://github.com/LTMX/Unity.Athena
5+
#endregion
6+
7+
#if UNITY_EDITOR
8+
9+
using UnityEditor;
10+
using UnityEditor.AssetImporters;
11+
12+
public static class ScriptedImporterEditorExtensions
13+
{
14+
public static SerializedProperty FindProperty(this ScriptedImporterEditor s, string name)
15+
{
16+
return s.serializedObject.FindProperty(name);
17+
}
18+
}
19+
20+
21+
#endif

Editor/ScriptedImporterEditor.Extensions.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.

Runtime/AssetManagement.Extensions.cs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#region Header
2+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
3+
// ** Github Profile: https://github.com/LTMX
4+
// ** Repository : https://github.com/LTMX/Unity.Athena
5+
#endregion
6+
7+
using System;
8+
using System.IO;
9+
using System.Text;
10+
using Unity.Mathematics;
11+
using UnityEditor;
12+
using UnityEngine;
13+
using Object = UnityEngine.Object;
14+
15+
public static class AssetManagementExtensions
16+
{
17+
public static string ApplicationRootPath => Application.dataPath.Remove(Application.dataPath.Length - 6);
18+
19+
#if UNITY_EDITOR
20+
21+
/// <inheritdoc cref="AssetDatabase.GetAssetPath(UnityEngine.Object)" />
22+
public static string GetPath<T>(this T o) where T : Object => AssetDatabase.GetAssetPath(o);
23+
/// <inheritdoc cref="AssetDatabase.LoadAssetAtPath(string, System.Type)" />
24+
public static T LoadAtPath<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
25+
/// Loads an object from a path relative to the Assets folder
26+
public static Object LoadAtPath(string path) => AssetDatabase.LoadAssetAtPath<Object>(path);
27+
28+
public static string GetFullPath(this Object asset) => ApplicationRootPath.Combine(AssetDatabase.GetAssetPath(asset));
29+
public static string GetAssetPathWithoutAssetName(this Object asset) => AssetDatabase.GetAssetPath(asset).RemoveAssetNameFromPath(asset);
30+
31+
/// Replaces an asset at the desired path. If nothing can be found, the asset it simply created
32+
public static T CreateOrReplaceAsset<T>(this T asset, string path) where T : Object => CreateAssetIfNull(asset, path).CopySerialized(asset);
33+
34+
/// <inheritdoc cref="AssetDatabase.CreateAsset" />
35+
public static T CreateAsset<T>(this T asset, string path) where T : Object {
36+
AssetDatabase.CreateAsset(asset, path);
37+
return asset;
38+
}
39+
40+
/// <inheritdoc cref="EditorUtility.CopySerialized" />
41+
public static T CopySerialized<T>(this T destination, T source) where T : Object {
42+
EditorUtility.CopySerialized(source, destination);
43+
return destination;
44+
}
45+
46+
47+
/// Creates an asset if no asset is found at the desired path
48+
public static T CreateAssetIfNull<T>(this T asset, string path) where T : Object {
49+
var existingAsset = LoadAtPath<T>(path);
50+
return existingAsset != null ? existingAsset : asset.CreateAsset(path);
51+
}
52+
53+
#endif
54+
55+
56+
57+
58+
59+
private static string CreateUniqueFileName(this object obj) => "Script_" + GetFormattedDate() + "_" + math.abs(obj.GetHashCode());
60+
61+
private static string GetFormattedDate() => DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour;
62+
63+
/// Saves a byte array to a file at the desired path
64+
public static void SaveToFile(this byte[] bytes, string path, string fileName, string extension, bool ping = true)
65+
{
66+
path.CreateDirectoryIfVoid();
67+
68+
fileName.IsNullOrEmpty().IfTrue(() => fileName = bytes.CreateUniqueFileName());
69+
path = Path.Combine(path, fileName) + extension;
70+
File.WriteAllBytes(path, bytes);
71+
#if UNITY_EDITOR
72+
AssetDatabase.Refresh();
73+
Debug.Log($"Saved script to {path}");
74+
path.PingPath(ping);
75+
#endif
76+
}
77+
78+
/// Saves a string to a file at the desired path
79+
public static void SaveToFile(this string text, string directory, string fileName, string extension, bool ping = true) => SaveToFile(Encoding.UTF8.GetBytes(text), directory, fileName, extension, ping);
80+
81+
/// Saves a texture to a PNG file at the desired path
82+
public static void SaveToPNGFile<T>(this T texture, string directory, string fileName, bool ping = true) where T : Texture=> SaveToFile((texture as Texture2D).EncodeToPNG(), directory, fileName, ".png", ping);
83+
84+
/// Saves a texture to a JPG file at the desired path
85+
public static void SaveToJPGFile<T>(this T texture, string directory, string fileName, bool ping = true) where T : Texture=> SaveToFile((texture as Texture2D).EncodeToJPG(), directory, fileName, ".jpg", ping);
86+
87+
88+
89+
public static Texture2D LoadTexture2D(string path) => LoadResource<Texture2D>(path);
90+
91+
#if UNITY_EDITOR
92+
public static T LoadAsset<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
93+
#endif
94+
95+
public static T LoadResource<T>(string path) where T : Object => Resources.Load<T>(path);
96+
}

Runtime/AssetManagement.Extensions.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.

Runtime/Athena.Extensions.asmdef

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "Athena.Extensions",
3+
"rootNamespace": "",
4+
"references": [
5+
"GUID:d8b63aba1907145bea998dd612889d6b",
6+
"GUID:343deaaf83e0cee4ca978e7df0b80d21",
7+
"GUID:2bafac87e7f4b9b418d9448d219b01ab"
8+
],
9+
"includePlatforms": [],
10+
"excludePlatforms": [],
11+
"allowUnsafeCode": true,
12+
"overrideReferences": false,
13+
"precompiledReferences": [],
14+
"autoReferenced": true,
15+
"defineConstraints": [],
16+
"versionDefines": [],
17+
"noEngineReferences": false
18+
}

Runtime/Athena.Extensions.asmdef.meta

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

Runtime/Color.Extensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#region Header
2+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
3+
// ** Github Profile: https://github.com/LTMX
4+
// ** Repository : https://github.com/LTMX/Unity.Athena
5+
#endregion
6+
7+
using Unity.Mathematics;
8+
using UnityEngine;
9+
10+
public static class ColorExtensions
11+
{
12+
public static Color unlerp(this Color In, float2 MinMax) => ((In.AsFloat4() - MinMax.x) / (MinMax.y - MinMax.x)).asColor();
13+
public static float4 AsFloat4(this Color c) => new(c.r, c.g, c.b, c.a);
14+
public static Color asColor(this float4 c) => new(c.x, c.y, c.z, c.w);
15+
}

Runtime/Color.Extensions.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.

Runtime/Condition.Extensions.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#region Header
2+
3+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
4+
// ** Github Profile: https://github.com/LTMX
5+
// ** Repository : https://github.com/LTMX/Unity.Athena
6+
7+
#endregion
8+
9+
using System;
10+
11+
public static class ConditionExtensions {
12+
13+
public static bool And(this bool a, bool b) => a && b;
14+
public static bool AndNot(this bool a, bool b) => a && !b;
15+
public static bool Nand(this bool a, bool b) => !a && !b;
16+
public static bool Or(this bool a, bool b) => a || b;
17+
public static bool OrNot(this bool a, bool b) => a || b;
18+
public static bool Nor(this bool a, bool b) => !a || !b;
19+
20+
public static void IfTrue(this bool condition, Action action) {
21+
if (condition) action();
22+
}
23+
public static void IfTrue<T>(this bool condition, Func<object> action){
24+
if (condition) action();
25+
}
26+
27+
public static void IfFalse(this bool condition, Action action) {
28+
if (!condition) action();
29+
}
30+
31+
public static void Select(this bool condition, Action onTrue, Action onFalse) {
32+
if (condition) onTrue();
33+
else onFalse();
34+
}
35+
36+
public static void Select<T>(this bool condition, Action<T> onTrue, Action<T> onFalse, T arg) {
37+
if (condition) onTrue(arg);
38+
else onFalse(arg);
39+
}
40+
41+
public static U IfTrue<T, U>(this bool condition, Func<T, U> action, T arg) => condition ? action(arg) : default;
42+
43+
public static U IfFalse<T, U>(this bool condition, Func<T, U> action, T arg) => !condition ? action(arg) : default;
44+
45+
public static void IfTrue<T>(this bool condition, Action<T> action, T arg) {
46+
if (condition) action(arg);
47+
}
48+
public static void IfFalse<T>(this bool condition, Action<T> action, T arg) {
49+
if (!condition) action(arg);
50+
}
51+
52+
public static void flip(this ref bool condition) => condition = !condition;
53+
54+
}

Runtime/Condition.Extensions.cs.meta

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

Runtime/Directory.Extensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#region Header
2+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
3+
// ** Github Profile: https://github.com/LTMX
4+
// ** Repository : https://github.com/LTMX/Unity.Athena
5+
#endregion
6+
7+
using System.IO;
8+
9+
public static class DirectoryExtensions
10+
{
11+
/// <summary> Creates a directory if it doesn't exist. </summary>
12+
public static void CreateDirectoryIfVoid(this string path) => Directory.Exists(path).IfFalse(() => Directory.CreateDirectory(path));
13+
}

Runtime/Directory.Extensions.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.

Runtime/Editor.meta

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

Runtime/Encoding.Extensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#region Header
2+
3+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
4+
// ** Github Profile: https://github.com/LTMX
5+
// ** Repository : https://github.com/LTMX/Unity.Athena
6+
7+
#endregion
8+
9+
using System.Text;
10+
11+
public static class EncodingExtensions
12+
{
13+
public static byte[] ToUTF8Bytes(this string text) => Encoding.UTF8.GetBytes(text);
14+
public static string ToUTF8String(this byte[] bytes) => Encoding.UTF8.GetString(bytes);
15+
}

Runtime/Encoding.Extensions.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.

Runtime/IEnumerable.Extensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#region Header
2+
3+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
4+
// ** Github Profile: https://github.com/LTMX
5+
// ** Repository : https://github.com/LTMX/Unity.Athena
6+
7+
#endregion
8+
9+
using System;
10+
using System.Collections.Generic;
11+
12+
public static class IEnumerableExtensions
13+
{
14+
public static void ForEach<T>(this IEnumerable<T> array, Action<T> action) {
15+
foreach (var item in array) {
16+
action(item);
17+
}
18+
}
19+
}

Runtime/IEnumerable.Extensions.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#region Header
2+
3+
// ** Copyright (C) 2023 Nicolas Reinhard, @LTMX. All rights reserved.
4+
// ** Github Profile: https://github.com/LTMX
5+
// ** Repository : https://github.com/LTMX/Unity.Athena
6+
7+
#endregion
8+
9+
using System.Text.Json;
10+
using System.Text.Json.Serialization;
11+
12+
public static class JsonSerializerOptionsExtensions
13+
{
14+
public static JsonSerializerOptions AddConverter<T>(this JsonSerializerOptions options) where T : JsonConverter, new()
15+
{
16+
options.Converters.Add(new T());
17+
return options;
18+
}
19+
}

Runtime/JsonSerializerOptions.Extensions.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)