Skip to content

Commit

Permalink
Prevent animator events from calling SendCustomEvent on proxies
Browse files Browse the repository at this point in the history
- Now check the calling method to see if it's a native call which indicates Unity is calling the code and block the call. Resolves #58
  • Loading branch information
MerlinVR committed Oct 5, 2020
1 parent a9b55f4 commit 7087ad4
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Assets/UdonSharp/Scripts/UdonSharpBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
using System.Reflection;
using UnityEngine;
using VRC.Udon.Common.Interfaces;

#if UNITY_EDITOR
using System.Diagnostics;
using VRC.Udon.Serialization.OdinSerializer;
#endif

namespace UdonSharp
{
Expand Down Expand Up @@ -35,6 +39,17 @@ public void SetProgramVariable(string name, object value)

public void SendCustomEvent(string eventName)
{
#if UNITY_EDITOR
if (_backingUdonBehaviour != null) // If this is a proxy, we need to check if this is a valid call to SendCustomEvent, since animation events can call it when they shouldn't
{
StackFrame frame = new StackFrame(1); // Get the frame of the calling method

// If the calling method is null, this has been called from native code which indicates it was called by Unity, which we don't want on proxies
if (frame.GetMethod() == null)
return;
}
#endif

MethodInfo eventmethod = GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(e => e.Name == eventName && e.GetParameters().Length == 0).FirstOrDefault();

if (eventmethod != null)
Expand Down

0 comments on commit 7087ad4

Please sign in to comment.