How to add an Event object to Unity's EventSystem - unity3d

If I have an event from Event.current and I want to 'replay' it by making the event system process it again, how do I do that? Can I access the scripts responsible for raising events?

You might be able to track down how events are created using IL Spy then use reflection to invoke that, but unfortunately there is no public API for creating events manually.

I found a neat C# library which simulates input called Windows Input Simulator which does a good job of raising all the keyboard events I needed. Looks like it also works for things like mouse input!

Related

Event Types In Unity Scripting

i just wanna start to codding more clean and dynamic codes and i start to learn "events" but im so confused about events because all videos show different things 0.0 did i understand wrong or there are three types of events ? i mean
c# event
unity events
game events
if i understand true this all different event systems right ? and i can use which i want, and i should spesific.
i hope i understand right can someone explain shortly ? if i understand right just can say yes :)
There are UnityEvents. Usually AddListener(YourFunction)
There are Unity Events (meta, haha!)
There is an official Unity Tutorial on C# Events. Usually with the += operator: .OnClicked += YourFunction;
There is an Event System for the "Unity UI 1.0.0" Package, but there is also still the old Event System for Ui/Canvas interaction (until Unity 2018?).
There is the Event System for UI inside of Unity, if you want to build an Editor Tool, to paint prefabs in the Scene for example.
The "old" Input System (Input.GetKeyDown("Space")) is also an Event System.
But there is also a still optional, new Input System Package that handles local Coop (multiple Controllers) a bit better. You can only use one at a time. (You have to switch form one to the other in Settings)
As you can see, there a a lot of Event Systems. You can write your own, if that is required for a Task. Event Systems are a general way to handle changes without polling and comparing values every frame, but listening to changes and occurances.

Unity3d onSomeEvent() vs update() performance

I've been using Unity3D lately, and soon I discovered thorugh multiple topics online that using OnMouseUp event is much slower than checking for mouse clicks on the update() function. Can someone explain why is that?
Is that also valid for other on functions like OnTriggerExit2D and others? As a design pattern, should I abandon the on() functions completely and only catch events on Update()?
I prefer neither of them but use the 'new' (since 4.6) Unity Event system when possible. In short you have to implement the appropriate handler interface like IPointerClickHandler.
I recommend to have a look at the Events tutorials, for example UI Events and Event Triggers. Note that although this tutorial is focussed on 2D, you can use the event system in 3D as well. You just need to add a Raycaster to your camera.
Agreed with the previous answer but as a reason to WHY the "On" functions are so much more inefficient lies in how they function.
How does unity know that an "OnMouseUp" event has fired? The mouse object would need to have an event handler attached to it that knows to fire the OnMouseUp event. Beyond that something would need to listen every single frame for that event and then run the required code. Also the OnEvent functions tend to get very dispersed because you could in theory have the same event in multiple different functions/classes.
I think the update method in general is a more efficient way to check, because there is a lot less overhead involved when you handle these things yourself.
tldr: There's more overhead involved in using the "On" events instead of in "Update"

Scripting structure for unity with airconsole

I've looked at the examples that come with the airconsole plugin, and in both of the examples the "meat" of the code is in one script.
I am interested in trying to make a multiplayer game, is it possible to split the control of each character into 4 scripts, which each are applied on a different player object rather than having one script control all 4 objects from one single script?
If so, how do you go about doing it, just by including the libraries in each control script? like so:
using NDream.AirConsole;
or do i have to do some kind of special structure?
It's possible to have various different scripts that use the NDream.AirConsole namespace, yes.
Be aware however that if you have 4 scripts in a scene that all listen for the OnMessage event, for instance, you will likely end up using a lot more performance than if you just have one "AirConsole script" in a scene, where you receive and filter the messages and then relay to the right player.
Better to have one AirConsole script that listens for the event, check for device id/player number in the OnMessage (or whichever) event and then send the relevant information to the player script in question.
That's how we do it for our multiplayer games in Unity.

Why a form is needed for a SetWinEventHook callback?

Currently, I'm using the powerful SetWinEventHook() function to catch some user-interface's actions like minimizing and maximizing from other window runned by programs on the computer.
So I inspired myself by using the code provided by BrendanMcK on this post and it does work (I mean: the callback function is called when an event occurs) until the line
MessageBox.Show("Something")
is present. But I don't want to use any form or window for this program..
After some research, I figured out this kind of hook needs a message loop to allow the redirection of messages from other window handles. Apparently, calling the thread using Application.Run() should do the trick, but I would prefer something cleaner, in the C# object itself.
So my question is: is it possible to create a message loop inside an object's method?
http://bytes.com/topic/c-sharp/answers/557342-thread-message-loop-c
No, the function doesn't require a window handle so no "form" is needed. But the MSDN docs for the function is quite explicit:
The client thread that calls SetWinEventHook must have a message loop in order to receive events.
A message loop is universal in any program that want to receive notifications that are generated externally by other processes or threads. It is the common solution to the producer-consumer problem. Clearly any GUI app has a need for such a solution, Windows messages are generated by the operating system. It isn't different for SetWinEventHook(), the accessibility events originate in other programs. There is no clean mechanism to "interrupt" a thread and make it run other code, the re-entrancy problems that causes are extremely difficult to deal with. The thread has to co-operate, it must be idle and ready to receive a notification to safely process it. A message loop solves that problem.
Pumping a message loop (calling Application.Run) in a method is certainly possible. But do keep in mind that the method won't return until you explicitly stop the loop with Application.ExitThread. There is therefore usually only one good place for that call, the Main() method of your program.
Starting your project with a Winforms or WPF project template is a very good way to get this right. You have no need to actually create a window, call Application.Run() without an argument, after pinvoking SetWinEventHook.

How create a custom event handler for ctcall center? iphone

I am working on an application in which i have a requirement of detect call log info.
Means when ever a call coming and going and then after attending call that will disconnect. So i have detect and send a notification when call disconnect.
For this requirement i have done so much r&d and get some results but when i go through Apple's doc for core telephony framework then there is class "CTCallCenter". This class provide a event handler which will invoke app whenever a call state change.
Now problem is that when i go through document of that class then i get som texts which is shown below
To handle such call events, define a handler block in your application and assign it to this property. You must implement the handler block to support being invoked from any context.
link of apple doc for core telephony framework
In above text write down that u have to create a event handler and assign to property then it will handle call events.
So problem is that how I create a custom event handler and how make a property and assign to my custom event handler?
Thanks in advance...
Take a look at CoreTelephonyDemo. You will find your answer in there.
The event handler is a block. You can also look at Blocks Programming Topics