Skip to content

Commit

Permalink
Wipe all proxy behaviours on upload because Unity isn't thorough enough
Browse files Browse the repository at this point in the history
- Unity can include some dependencies from proxies even when the components are marked to not be included in the build. So wipe them before upload.
  • Loading branch information
MerlinVR committed Oct 28, 2020
1 parent 037f5bb commit 2d534a3
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions Assets/UdonSharp/Editor/UdonSharpEditorManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,22 @@ internal class UdonSharpEditorManager
{
static UdonSharpEditorManager()
{
EditorSceneManager.sceneOpened += EditorSceneManager_sceneOpened;
EditorSceneManager.sceneOpened += OnSceneOpened;
EditorApplication.update += OnEditorUpdate;
EditorApplication.playModeStateChanged += OnChangePlayMode;
AssemblyReloadEvents.afterAssemblyReload += RunPostAssemblyBuildRefresh;
}

private static void EditorSceneManager_sceneOpened(Scene scene, OpenSceneMode mode)
static bool _skipSceneOpen = false;

private static void OnSceneOpened(Scene scene, OpenSceneMode mode)
{
List<UdonBehaviour> udonBehaviours = GetAllUdonBehaviours();
if (!_skipSceneOpen)
{
List<UdonBehaviour> udonBehaviours = GetAllUdonBehaviours();

RunAllUpdates(udonBehaviours);
RunAllUpdates(udonBehaviours);
}
}

internal static void RunPostBuildSceneFixup()
Expand Down Expand Up @@ -155,11 +160,11 @@ void InjectEvent(System.Type behaviourType, string eventName)
// Patch GUI object field drawer
MethodInfo doObjectFieldMethod = typeof(EditorGUI).GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault(e => e.Name == "DoObjectField" && e.GetParameters().Length == 9);

HarmonyMethod objectFieldProxy = new HarmonyMethod(typeof(InjectedMethods).GetMethod("DoObjectFieldProxy"));
HarmonyMethod objectFieldProxy = new HarmonyMethod(typeof(InjectedMethods).GetMethod(nameof(InjectedMethods.DoObjectFieldProxy)));
harmony.Patch(doObjectFieldMethod, objectFieldProxy);

System.Type validatorDelegateType = typeof(EditorGUI).GetNestedType("ObjectFieldValidator", BindingFlags.Static | BindingFlags.NonPublic);
InjectedMethods.validationDelegate = Delegate.CreateDelegate(validatorDelegateType, typeof(InjectedMethods).GetMethod("ValidateObjectReference"));
InjectedMethods.validationDelegate = Delegate.CreateDelegate(validatorDelegateType, typeof(InjectedMethods).GetMethod(nameof(InjectedMethods.ValidateObjectReference)));

InjectedMethods.objectValidatorMethod = typeof(EditorGUI).GetMethod("ValidateObjectReferenceValue", BindingFlags.NonPublic | BindingFlags.Static);

Expand All @@ -172,7 +177,10 @@ void InjectEvent(System.Type behaviourType, string eventName)
MethodInfo postBuildMethod = typeof(InjectedMethods).GetMethod(nameof(InjectedMethods.PostBuildAssetBundles), BindingFlags.Public | BindingFlags.Static);
HarmonyMethod postBuildHarmonyMethod = new HarmonyMethod(postBuildMethod);

harmony.Patch(buildAssetbundlesMethod, null, postBuildHarmonyMethod);
MethodInfo preBuildMethod = typeof(InjectedMethods).GetMethod(nameof(InjectedMethods.PreBuildAssetBundles), BindingFlags.Public | BindingFlags.Static);
HarmonyMethod preBuildHarmonyMethod = new HarmonyMethod(preBuildMethod);

harmony.Patch(buildAssetbundlesMethod, preBuildHarmonyMethod, postBuildHarmonyMethod);
}

static class InjectedMethods
Expand Down Expand Up @@ -293,9 +301,16 @@ public static bool DoObjectFieldProxy(ref System.Type objType, SerializedPropert
return true;
}

public static void PreBuildAssetBundles()
{
DestroyAllProxies();
_skipSceneOpen = true;
}

public static void PostBuildAssetBundles()
{
CreateProxyBehaviours(GetAllUdonBehaviours());
_skipSceneOpen = false;
}
}

Expand Down Expand Up @@ -824,9 +839,25 @@ static void CreateProxyBehaviours(List<UdonBehaviour> allBehaviours)
{
foreach (UdonBehaviour udonBehaviour in allBehaviours)
{
if (udonBehaviour.programSource != null && udonBehaviour.programSource is UdonSharpProgramAsset)
if (UdonSharpEditorUtility.IsUdonSharpBehaviour(udonBehaviour))
UdonSharpEditorUtility.GetProxyBehaviour(udonBehaviour, ProxySerializationPolicy.NoSerialization);
}
}

static void DestroyAllProxies()
{
var allBehaviours = GetAllUdonBehaviours();

foreach (UdonBehaviour behaviour in allBehaviours)
{
if (UdonSharpEditorUtility.IsUdonSharpBehaviour(behaviour))
{
UdonSharpBehaviour proxy = UdonSharpEditorUtility.FindProxyBehaviour(behaviour, ProxySerializationPolicy.NoSerialization);

if (proxy)
UnityEngine.Object.DestroyImmediate(proxy);
}
}
}
}
}

0 comments on commit 2d534a3

Please sign in to comment.