Skip to content

Commit caf9670

Browse files
authored
Merge pull request #21 from GieziJo/SOData
2 parents de075eb + ca8b520 commit caf9670

16 files changed

Lines changed: 603 additions & 435 deletions

Editor/SOContextMenu.cs

Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

Editor/SOVFixer.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using UnityEditor;
4+
using UnityEngine;
5+
6+
namespace Giezi.Tools
7+
{
8+
public class SOVFixer
9+
{
10+
[MenuItem("Tools/GieziTools/SOVariant/Fix SOVs")]
11+
public static void FixSOVs()
12+
{
13+
IEnumerable<ScriptableObject> scriptableObjects =
14+
AssetDatabase.GetAllAssetPaths()
15+
.Where(s => s.EndsWith(".asset") && s.StartsWith("Assets/"))
16+
.Select(AssetDatabase.LoadAssetAtPath<ScriptableObject>)
17+
.Where(o => o != null)
18+
.Where(o => o.GetType().IsDefined(typeof(SOVariantAttribute), true));
19+
20+
var _library = SOVariantDataAccessor.SoVariantDataLibrary;
21+
Dictionary<ScriptableObject, SOVariantData> _localLibrary = new();
22+
foreach (ScriptableObject scriptableObject in scriptableObjects)
23+
{
24+
_localLibrary.Add(scriptableObject, _library.GetSOVariantDataForTarget(scriptableObject));
25+
}
26+
27+
foreach (SOVariantData soVariantData in _localLibrary.Values)
28+
{
29+
soVariantData.Children = new List<ScriptableObject>();
30+
}
31+
32+
foreach (KeyValuePair<ScriptableObject,SOVariantData> keyValuePair in _localLibrary)
33+
{
34+
if (keyValuePair.Value.Parent != null)
35+
{
36+
_localLibrary[keyValuePair.Value.Parent].Children.Add(keyValuePair.Key);
37+
}
38+
}
39+
40+
foreach (KeyValuePair<ScriptableObject,SOVariantData> keyValuePair in _localLibrary)
41+
{
42+
_library.WriteToLibrary(keyValuePair.Key, keyValuePair.Value.Parent,
43+
keyValuePair.Value.Overridden, keyValuePair.Value.Children);
44+
}
45+
46+
foreach (KeyValuePair<ScriptableObject,SOVariantData> keyValuePair in _localLibrary)
47+
{
48+
if (keyValuePair.Value.Children is { Count: > 0 })
49+
{
50+
SOVariant<ScriptableObject> sov = new SOVariant<ScriptableObject>(keyValuePair.Key);
51+
sov.SaveData(keyValuePair.Value.Overridden);
52+
}
53+
}
54+
55+
EditorUtility.SetDirty(_library);
56+
AssetDatabase.SaveAssets();
57+
AssetDatabase.Refresh();
58+
}
59+
}
60+
}

Editor/SOVFixer.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)