From 7ddf44f52d6b7f15466a6f2bd3692de31954c5e1 Mon Sep 17 00:00:00 2001 From: Merlin <36685500+MerlinVR@users.noreply.github.com> Date: Thu, 5 Aug 2021 18:05:01 -0700 Subject: [PATCH] Workaround handling for Unity being dumb - Workaround handling for when Unity decides that a scriptable object is not the type it is. Which Unity claims to have fixed in 2019.4, but clearly hasn't and instead made it worse https://forum.unity.com/threads/assetdatabase-findassets-not-working.587062/ --- .../UdonSharp/Editor/UdonSharpProgramAsset.cs | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs index af20c9c6..4a1a469d 100644 --- a/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs +++ b/Assets/UdonSharp/Editor/UdonSharpProgramAsset.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Collections.Immutable; using System.IO; +using System.Linq; using System.Reflection; using UdonSharp.Compiler; using UdonSharpEditor; @@ -244,13 +245,39 @@ public static UdonSharpProgramAsset[] GetAllUdonSharpPrograms() { if (_programAssetCache == null) { - string[] udonSharpDataAssets = AssetDatabase.FindAssets($"t:{typeof(UdonSharpProgramAsset).Name}"); + string[] udonSharpDataAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}"); _programAssetCache = new UdonSharpProgramAsset[udonSharpDataAssets.Length]; for (int i = 0; i < _programAssetCache.Length; ++i) - { _programAssetCache[i] = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(udonSharpDataAssets[i])); + + bool neededFallback = false; + UdonSharpProgramAsset[] fallbackAssets = Resources.FindObjectsOfTypeAll(); + + foreach (UdonSharpProgramAsset fallbackAsset in fallbackAssets) + { + if (!_programAssetCache.Contains(fallbackAsset)) + { + Debug.LogWarning($"Repairing program asset {fallbackAsset} which Unity has broken"); + neededFallback = true; + + AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(fallbackAsset), ImportAssetOptions.ForceUpdate); + } + } + + if (neededFallback) + { + udonSharpDataAssets = AssetDatabase.FindAssets($"t:{nameof(UdonSharpProgramAsset)}"); + + _programAssetCache = new UdonSharpProgramAsset[udonSharpDataAssets.Length]; + + for (int i = 0; i < _programAssetCache.Length; ++i) + { + _programAssetCache[i] = + AssetDatabase.LoadAssetAtPath( + AssetDatabase.GUIDToAssetPath(udonSharpDataAssets[i])); + } } }