2

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

6
  • 1
    What is the relationship of PuzzleBase and IPuzzleMain ?It seems other ok. Commented Aug 8, 2018 at 12:06
  • I don't want it to inherit unused part from PuzzleBase, so i create IPuzzleMain interface for adding function like, GetPuzzleState,etc Commented Aug 9, 2018 at 4:04
  • So maybe you forgot create a new event class for your new Interface?because once the event has any parameter you need define for it. Commented Aug 9, 2018 at 4:09
  • @RonTang What do you mean by creating new event class for interface? because I already have interface definition, and the interface also has been implemented in a script. Commented Aug 9, 2018 at 6:15
  • I've tested in my unity.It worked for interface.I'm not sure why your unityevent can't. Commented Aug 9, 2018 at 6:53

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The problem is not setting up UnityEvent with interface as parameter, but rather than a public function with interface as parameter. On static parameter, Unity does not show function with interface as parameter.
1

Try to add button action with script

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.