Tengku Fathullah
Tengku Fathullah

Reputation: 1370

UnityEvent not accepting function with interface as parameter

I have a script with function that have interface as paremeter.

PuzzleABCondition.cs

public void Execute_IfPuzzleCompleted(IPuzzleMain mainScript);

but seems like the function not show up in UnityEvent.

enter image description here

Upvotes: 2

Views: 3150

Answers (2)

Ron Tang
Ron Tang

Reputation: 1622

My unity version is 2017.3 and running on Window10. Use follow code no problem.

In one of my code file: define the interface and event.

        public interface TestPara { }
        [System.Serializable]
        public class TestInterfaceEvent : UnityEvent<TestPara>
        {

        }

In another file: Use the TestInterfaceEvent and drag it to one object in scene.

        public class TestClass : MonoBehaviourpublic
        {
           TestInterfaceEvent OnTestEvent;
        } 

In OnEventEffect.cs define two functions.

        public void Test(TestPara para)
        {
        }
        public void Test()
        {
        }

enter image description here As the picture shows unityevent can select interface as parameter. but note that it mark dynamic. enter image description here

When configuring a UnityEvent in the Inspector there are two types of function calls that are supported:

Static. Static calls are preconfigured calls, with preconfigured values that are set in the UI. This means that when the callback is invoked, the target function is invoked with the argument that has been entered into the UI.

Dynamic. Dynamic calls are invoked using an argument that is sent from code, and this is bound to the type of UnityEvent that is being invoked. The UI filters the callbacks and only shows the dynamic calls that are valid for the UnityEvent.

--------------------------------UPDATE------------------------------------------

Update for the reason: I've try many special case such as struct, enum or more than one base type as parameter.They all don't show in static list. So I confused by the result.After hours of research,I found a good answer form JoshuaMcKenzie for these problems.

Unity's UnityEventBaseInspector class

//UnityEventBase has this field that it serializes
       PersistentCallGroup m_PersistentCalls;// holds a list of all the methods to call when the event is invoked

//and PersistentCallgroup has this
       List<PersistentCall> m_Calls;

//each PersistentCall has these fields
       UnityEventCallState m_CallState // an enum for off, editor and runtime, or runtime only
       PersistentListenerMode m_Mode // enum determining which internal delegate to call and which inputs to draw in inspector
       UnityEngine.Object m_Target // the instance which to call the method on
       string m_TypeName // the concrete type name of the target (needed for polymorphism)
       string m_MethodName // the name of the method to call in target

       ArgumentCache m_Arguments //container class which holds the arguments that are passed into the calling function, used for static calls

//ArgumentCache has the following variables
       UnityEngine.Object m_ObjectArgument
       string m_ObjectArgumentAssemblyTypeName // used to confirm if objectArgument is valid
       int m_IntArgument
       float m_FloatArgument
       string m_StringArgument
       bool m_BoolArgument

as you can see with the ArgumentCache class it really only has enough storage for one of each datatype and when you really get down into the editor scripting, there no clean way of showing multiple fields with the limited fields availible (can't show any function(int,int) calls for example since theres only one intArgument field per call) in the ArgumentCache class.

For custom class only support subclass of UnityEngine.Object.

Upvotes: 2

Ausl&#228;nder
Ausl&#228;nder

Reputation: 499

Try to add button action with script

public Button btn;
void Start()
{
    btn.onClick.AddListener(delegate{  
           Execute_IfPuzzleCompleted(YourObjectHere);})
}

Upvotes: 1

Related Questions