1
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
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
5
#endregion
6
6
7
7
using System ;
8
8
using System . Diagnostics ;
9
9
using System . IO ;
10
10
using System . Text ;
11
- using Unity . Mathematics ;
12
- using UnityEditor ;
13
11
using UnityEngine ;
14
12
using Debug = UnityEngine . Debug ;
15
13
using Object = UnityEngine . Object ;
16
14
15
+ #if UNITY_EDITOR
16
+ using UnityEditor ;
17
+ #endif
18
+
17
19
public static class AssetManagementExtensions
18
20
{
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
71
84
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
+
114
130
}
0 commit comments