@@ -8,9 +8,7 @@ namespace Giezi.Tools
88{
99 public static class SOContextMenu
1010 {
11- private const string GieziToolsCreateSOvariantChildKey = "Giezi.Tools.CreateSoVariant.Child" ;
12- private const string GieziToolsCreateSOvariantParentKey = "Giezi.Tools.CreateSoVariant.Parent" ;
13-
11+
1412 [ MenuItem ( "Assets/Create/Create SO Variant" , false , 2000 ) ]
1513 static void CreateSOVariant ( )
1614 {
@@ -19,70 +17,38 @@ static void CreateSOVariant()
1917
2018 string assetPath = AssetDatabase . GetAssetPath ( activeObject ) ;
2119 string newAssetPath = GetNewAssetName ( assetPath ) ;
22-
20+
2321 ScriptableObject newAsset = ScriptableObject . CreateInstance ( soType ) ;
2422
2523 AssetDatabase . CreateAsset ( newAsset , newAssetPath ) ;
2624 EditorUtility . SetDirty ( newAsset ) ;
2725
28- PlayerPrefs . SetString ( GieziToolsCreateSOvariantChildKey , newAssetPath ) ;
29- PlayerPrefs . SetString ( GieziToolsCreateSOvariantParentKey , assetPath ) ;
26+ SOVariantHelper < ScriptableObject > . SetParent ( newAsset , ( ScriptableObject ) activeObject ) ;
3027
3128 AssetDatabase . SaveAssets ( ) ;
32-
33- AssetDatabase . Refresh ( ) ;
34- EditorUtility . RequestScriptReload ( ) ;
3529 }
36-
30+
3731 [ MenuItem ( "Assets/Create/Create SO Variant" , true ) ]
3832 static bool ValidateCreateSOVariant ( )
3933 {
4034 return ( Selection . activeObject is ScriptableObject &&
4135 Selection . activeObject . GetType ( ) . IsDefined ( typeof ( SOVariantAttribute ) , true ) ) ;
4236 }
43-
44- [ UnityEditor . Callbacks . DidReloadScripts ]
45- static void SetParentAfterReload ( )
46- {
47- if ( ! PlayerPrefs . HasKey ( GieziToolsCreateSOvariantChildKey ) )
48- return ;
49-
50- if ( AssetImporter . GetAtPath ( PlayerPrefs . GetString ( GieziToolsCreateSOvariantChildKey ) ) == null )
51- {
52- Debug . Log ( "<color=orange>SOVariant: </color>Asset Importer does not exist yet, waiting for Unity to reload." ) ;
53- return ;
54- }
55-
56- ScriptableObject parent =
57- AssetDatabase . LoadAssetAtPath < ScriptableObject > (
58- PlayerPrefs . GetString ( GieziToolsCreateSOvariantParentKey ) ) ;
59-
60- ScriptableObject child =
61- AssetDatabase . LoadAssetAtPath < ScriptableObject > (
62- PlayerPrefs . GetString ( GieziToolsCreateSOvariantChildKey ) ) ;
63-
64- SOVariantHelper < ScriptableObject > . SetParent ( child , parent ) ;
65-
66- PlayerPrefs . DeleteKey ( GieziToolsCreateSOvariantParentKey ) ;
67- PlayerPrefs . DeleteKey ( GieziToolsCreateSOvariantChildKey ) ;
68-
69- Debug . Log ( $ "<color=orange>SOVariant: </color>Created new SO Variant { child . name } with parent.") ;
70- }
71-
37+
7238 private static string GetNewAssetName ( string assetPath )
7339 {
7440 string newAssetPath = Path . Combine ( Path . GetDirectoryName ( assetPath ) ,
7541 Path . GetFileNameWithoutExtension ( assetPath ) + " Variant" + Path . GetExtension ( assetPath ) ) ;
76-
42+
7743 int counter = 0 ;
78-
44+
7945 while ( AssetDatabase . LoadAssetAtPath < ScriptableObject > ( newAssetPath ) != null )
8046 {
8147 counter ++ ;
8248 newAssetPath = Path . Combine ( Path . GetDirectoryName ( assetPath ) ,
8349 Path . GetFileNameWithoutExtension ( assetPath ) + " Variant_" + counter + Path . GetExtension ( assetPath ) ) ;
8450 }
85-
51+
8652 return newAssetPath ;
8753 }
8854 }
0 commit comments