Skip to content

Commit c2e8de5

Browse files
committed
Moved some files to Editor Folder + updated header + cleanup
1 parent 383241b commit c2e8de5

22 files changed

+391
-203
lines changed

Editor/AssetImporter.Extensions.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
#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
2+
// ** Copyright (C) 2024 Nicolas Reinhard, @ltmx. All rights reserved.
3+
// ** Github Profile: https://github.com/ltmx
4+
// ** Repository : https://github.com/ltmx/Unity.Athena
55
#endregion
66

77
#if UNITY_EDITOR
88

99
using UnityEditor;
1010
using UnityEngine;
11+
using Object = UnityEngine.Object;
1112

1213
public static class AssetImporterExtensions
1314
{
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-
}
15+
// Asset Importer Stuff
16+
public static void Import(this Texture2D t) => AssetDatabase.ImportAsset(t.GetPath());
17+
public static void Import<T>(this T t) where T : Object => AssetDatabase.ImportAsset(t.GetPath());
18+
public static void Import(this AssetImporter t) => AssetDatabase.ImportAsset(t.assetPath);
19+
20+
public static void ImportAndRefresh(this AssetImporter t)
21+
{
22+
AssetDatabase.ImportAsset(t.assetPath);
23+
AssetDatabase.Refresh();
24+
}
25+
2226
}
2327

2428
#endif
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#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
2+
// ** Copyright (C) 2024 Nicolas Reinhard, @ltmx. All rights reserved.
3+
// ** Github Profile: https://github.com/ltmx
4+
// ** Repository : https://github.com/ltmx/Unity.Athena
55
#endregion
66

7-
#if UNITY_EDITOR
8-
97
using UnityEditor;
108
using UnityEditor.AssetImporters;
119

@@ -15,7 +13,4 @@ public static SerializedProperty FindProperty(this ScriptedImporterEditor s, str
1513
{
1614
return s.serializedObject.FindProperty(name);
1715
}
18-
}
19-
20-
21-
#endif
16+
}

Editor/TextureImporter.Extensions.cs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#region Header
2+
// ** Copyright (C) 2024 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 UnityEditor;
8+
using UnityEngine;
9+
10+
public static class TextureImporterExtensions
11+
{
12+
public static TextureImporter SetReadWrite(this TextureImporter t, bool state) {
13+
t.isReadable = state;
14+
return t;
15+
}
16+
17+
public static TextureImporter SetCrunchCompression(this TextureImporter t, bool state) {
18+
t.crunchedCompression = state;
19+
return t;
20+
}
21+
22+
public static TextureImporter SetAlphaIsTransparency(this TextureImporter t, bool state) {
23+
t.alphaIsTransparency = state;
24+
return t;
25+
}
26+
27+
public static TextureImporter SetAsNormalMap(this TextureImporter t) {
28+
t.textureType = TextureImporterType.NormalMap;
29+
return t;
30+
}
31+
32+
public static TextureImporter SetType(this TextureImporter t, TextureImporterType type) {
33+
t.textureType = type;
34+
return t;
35+
}
36+
37+
public static Texture2D SetAsNormalMap(this Texture2D texture) {
38+
texture?.GetImporter()?.SetType(TextureImporterType.NormalMap).SaveAndReimport();
39+
return texture;
40+
}
41+
42+
public static Texture2D SetEnableMips(this Texture2D texture, bool state) {
43+
texture.GetImporter().mipmapEnabled = state;
44+
return texture;
45+
}
46+
47+
public static Texture2D SetReadWriteAndRefresh(this Texture2D texture, bool state) {
48+
texture?.GetImporter()?.SetReadWrite(state).SaveAndReimport();
49+
return texture;
50+
}
51+
52+
public static Texture2D SetReadWrite(this Texture2D texture, bool state) {
53+
texture?.GetImporter()?.SetReadWrite(state);
54+
return texture;
55+
}
56+
57+
public static Texture2D SetCrunchCompression(this Texture2D texture, bool state) {
58+
texture?.GetImporter()?.SetCrunchCompression(state).SaveAndReimport();
59+
return texture;
60+
}
61+
62+
public static Texture2D SetAlphaIsTransparency(this Texture2D texture, bool state) {
63+
texture?.GetImporter()?.SetAlphaIsTransparency(state).SaveAndReimport();
64+
return texture;
65+
}
66+
67+
public static void SetImporterType(this Texture2D texture, TextureImporterType type) => texture?.GetImporter()?.SetType(type).SaveAndReimport();
68+
69+
public static TextureImporter GetImporter(this Texture2D t) => AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(t)) as TextureImporter;
70+
}

Editor/TextureImporter.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: 115 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,130 @@
11
#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
2+
// ** Copyright (C) 2024 Nicolas Reinhard, @ltmx. All rights reserved.
3+
// ** Github Profile: https://github.com/ltmx
4+
// ** Repository : https://github.com/ltmx/Unity.Athena
55
#endregion
66

77
using System;
88
using System.Diagnostics;
99
using System.IO;
1010
using System.Text;
11-
using Unity.Mathematics;
12-
using UnityEditor;
1311
using UnityEngine;
1412
using Debug = UnityEngine.Debug;
1513
using Object = UnityEngine.Object;
1614

15+
#if UNITY_EDITOR
16+
using UnityEditor;
17+
#endif
18+
1719
public static class AssetManagementExtensions
1820
{
19-
public static string ApplicationRootPath => Application.dataPath.Remove(Application.dataPath.Length - 6);
20-
21-
#if UNITY_EDITOR
22-
23-
/// <inheritdoc cref="AssetDatabase.GetAssetPath(UnityEngine.Object)" />
24-
public static string GetPath<T>(this T o) where T : Object => AssetDatabase.GetAssetPath(o);
25-
/// <inheritdoc cref="AssetDatabase.LoadAssetAtPath(string, System.Type)" />
26-
public static T LoadAtPath<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
27-
/// Loads an object from a path relative to the Assets folder
28-
public static Object LoadAtPath(string path) => AssetDatabase.LoadAssetAtPath<Object>(path);
29-
30-
public static string GetFullPath(this Object asset) => ApplicationRootPath.Combine(AssetDatabase.GetAssetPath(asset));
31-
public static string GetAssetPathWithoutAssetName(this Object asset) => AssetDatabase.GetAssetPath(asset).RemoveAssetNameFromPath(asset);
32-
33-
/// Replaces an asset at the desired path. If nothing can be found, the asset it simply created
34-
public static T CreateOrReplaceAsset<T>(this T asset, string path) where T : Object => CreateAssetIfNull(asset, path).CopySerialized(asset);
35-
36-
/// <inheritdoc cref="AssetDatabase.CreateAsset" />
37-
public static T CreateAsset<T>(this T asset, string path) where T : Object {
38-
AssetDatabase.CreateAsset(asset, path);
39-
return asset;
40-
}
41-
42-
/// <inheritdoc cref="EditorUtility.CopySerialized" />
43-
public static T CopySerialized<T>(this T destination, T source) where T : Object {
44-
EditorUtility.CopySerialized(source, destination);
45-
return destination;
46-
}
47-
48-
49-
/// Creates an asset if no asset is found at the desired path
50-
public static T CreateAssetIfNull<T>(this T asset, string path) where T : Object {
51-
var existingAsset = LoadAtPath<T>(path);
52-
return existingAsset != null ? existingAsset : asset.CreateAsset(path);
53-
}
54-
55-
#endif
56-
57-
58-
59-
60-
61-
private static string CreateUniqueFileName(this object obj) => "Script_" + GetFormattedDate() + "_" + math.abs(obj.GetHashCode());
62-
63-
private static string GetFormattedDate() => DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour;
64-
65-
/// Saves a byte array to a file at the desired path
66-
public async static void SaveToFile(this byte[] bytes, string path, string fileName, string extension, bool ping = true)
67-
{
68-
path.CreateDirectoryIfVoid();
69-
70-
#if !UNITY_EDITOR
21+
22+
public static string ApplicationRootPath => Application.dataPath.Remove(Application.dataPath.Length - 6);
23+
24+
#if UNITY_EDITOR
25+
26+
public static string GetAssetPathWithoutAssetFolderAndAssetName(this Object asset) =>
27+
AssetDatabase.GetAssetPath(asset).RemoveAssetFolderFromPath().RemoveAssetNameFromPath(asset);
28+
29+
public static string GetRelativeAssetPath(this Object asset) => Application.dataPath + GetAssetPathWithoutAssetFolderAndAssetName(asset);
30+
31+
/// <inheritdoc cref="AssetDatabase.GetAssetPath(UnityEngine.Object)" />
32+
public static string GetPath<T>(this T o) where T : Object => AssetDatabase.GetAssetPath(o);
33+
34+
/// <inheritdoc cref="AssetDatabase.LoadAssetAtPath(string, System.Type)" />
35+
public static T LoadAtPath<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
36+
37+
/// Loads an object from a path relative to the Assets folder
38+
public static Object LoadAtPath(string path) => AssetDatabase.LoadAssetAtPath<Object>(path);
39+
40+
public static string GetFullPath(this Object asset) => ApplicationRootPath.Combine(AssetDatabase.GetAssetPath(asset));
41+
public static string GetAssetPathWithoutAssetName(this Object asset) => AssetDatabase.GetAssetPath(asset).RemoveAssetNameFromPath(asset);
42+
43+
/// Replaces an asset at the desired path. If nothing can be found, the asset it simply created
44+
public static T CreateOrReplaceAsset<T>(this T asset, string path) where T : Object => CreateAssetIfNull(asset, path).CopySerialized(asset);
45+
46+
/// <inheritdoc cref="AssetDatabase.CreateAsset" />
47+
public static T CreateAsset<T>(this T asset, string path) where T : Object
48+
{
49+
AssetDatabase.CreateAsset(asset, path);
50+
51+
return asset;
52+
}
53+
54+
/// <inheritdoc cref="EditorUtility.CopySerialized" />
55+
public static T CopySerialized<T>(this T destination, T source) where T : Object
56+
{
57+
EditorUtility.CopySerialized(source, destination);
58+
59+
return destination;
60+
}
61+
62+
63+
/// Creates an asset if no asset is found at the desired path
64+
public static T CreateAssetIfNull<T>(this T asset, string path) where T : Object
65+
{
66+
var existingAsset = LoadAtPath<T>(path);
67+
68+
return existingAsset != null ? existingAsset : asset.CreateAsset(path);
69+
}
70+
71+
#endif
72+
73+
74+
private static string CreateUniqueFileName(this object obj) => "Script_" + GetFormattedDate() + "_" + Math.Abs(obj.GetHashCode());
75+
76+
private static string GetFormattedDate() => DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day + "-" + DateTime.Now.Hour;
77+
78+
/// Saves a byte array to a file at the desired path
79+
public static async void SaveToFile(this byte[] bytes, string path, string fileName, string extension, bool ping = true)
80+
{
81+
path.CreateDirectoryIfVoid();
82+
83+
#if !UNITY_EDITOR
7184
path.OpenInExplorer();
72-
#endif
73-
74-
fileName.IsNullOrEmpty().IfTrue(() => fileName = bytes.CreateUniqueFileName());
75-
path = Path.Combine(path, fileName) + extension;
76-
await File.WriteAllBytesAsync(path, bytes);
77-
#if UNITY_EDITOR
78-
AssetDatabase.Refresh();
79-
Debug.Log($"Saved script to {path}");
80-
path.PingPath(ping);
81-
#endif
82-
}
83-
84-
public static void OpenInExplorer(this string path)
85-
{
86-
if (Directory.Exists(path) == false)
87-
return;
88-
Process.Start(new ProcessStartInfo
89-
{
90-
FileName = path,
91-
UseShellExecute = true,
92-
Verb = "open"
93-
});
94-
}
95-
96-
/// Saves a string to a file at the desired path
97-
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);
98-
99-
/// Saves a texture to a PNG file at the desired path
100-
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);
101-
102-
/// Saves a texture to a JPG file at the desired path
103-
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);
104-
105-
106-
107-
public static Texture2D LoadTexture2D(string path) => LoadResource<Texture2D>(path);
108-
109-
#if UNITY_EDITOR
110-
public static T LoadAsset<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
111-
#endif
112-
113-
public static T LoadResource<T>(string path) where T : Object => Resources.Load<T>(path);
85+
#endif
86+
87+
fileName.IsNullOrEmpty().IfTrue(() => fileName = bytes.CreateUniqueFileName());
88+
path = Path.Combine(path, fileName) + extension;
89+
await File.WriteAllBytesAsync(path, bytes);
90+
#if UNITY_EDITOR
91+
AssetDatabase.Refresh();
92+
Debug.Log($"Saved script to {path}");
93+
path.PingPath(ping);
94+
#endif
95+
}
96+
97+
public static void OpenInExplorer(this string path)
98+
{
99+
if (Directory.Exists(path) == false)
100+
return;
101+
102+
Process.Start(new ProcessStartInfo
103+
{
104+
FileName = path,
105+
UseShellExecute = true,
106+
Verb = "open"
107+
});
108+
}
109+
110+
/// Saves a string to a file at the desired path
111+
public static void SaveToFile(this string text, string directory, string fileName, string extension, bool ping = true) =>
112+
SaveToFile(Encoding.UTF8.GetBytes(text), directory, fileName, extension, ping);
113+
114+
/// Saves a texture to a PNG file at the desired path
115+
public static void SaveToPNGFile<T>(this T texture, string directory, string fileName, bool ping = true) where T : Texture =>
116+
SaveToFile((texture as Texture2D).EncodeToPNG(), directory, fileName, ".png", ping);
117+
118+
/// Saves a texture to a JPG file at the desired path
119+
public static void SaveToJPGFile<T>(this T texture, string directory, string fileName, bool ping = true) where T : Texture =>
120+
SaveToFile((texture as Texture2D).EncodeToJPG(), directory, fileName, ".jpg", ping);
121+
122+
public static Texture2D LoadTexture2D(string path) => LoadResource<Texture2D>(path);
123+
124+
public static T LoadResource<T>(string path) where T : Object => Resources.Load<T>(path);
125+
126+
#if UNITY_EDITOR
127+
public static T LoadAsset<T>(string path) where T : Object => AssetDatabase.LoadAssetAtPath<T>(path);
128+
#endif
129+
114130
}

Runtime/AssetManagement.Extensions.cs.meta

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

0 commit comments

Comments
 (0)