Implementing the event manager based on Unity3d SendMessage

Source: Internet
Author: User
Tags event listener eventbase
Using System;
Using System.Collections;
Using System.Collections.Generic;
Using Unityengine;


Namespace Game

{

Event Listener manager Internal class EventListener
{
public string name;
public Gameobject Listener;
public string func;
}

Event Dispatcher
public class Eventdispatcher:monobehaviour
{
Private Hashtable _listeners = new Hashtable ();
public static Eventdispatcher Instance;
void Awake ()
{
Instance = this;
}
void OnDestroy ()
{
Instance = null;
_listeners. Clear ();
}



Check if the event type is added
private bool Checkforevent (string eventtype)
{
if (_listeners. ContainsKey (EventType))
{
return true;
}
return false;
}

Check the listener.
private bool Checkforlistener (string eventtype, Gameobject listener,string func)
{
if (!checkforevent (EventType))
{
RecordEvent (EventType);
}


ArrayList listenerlist = _listeners[eventtype] as ArrayList;
foreach (EventListener callback in ListenerList)
{
if (string. Equals (Callback.name, (listener). Getinstanceid (). ToString () + "_" + Func)))
return true;
}
return false;
}

Add Event
private bool RecordEvent (string eventtype)
{
if (!checkforevent (EventType))
{
_listeners. ADD (EventType, New ArrayList ());
}
return true;
}

Add Listener (save name as Listener _func)
private bool Recordlistener (string eventtype, Gameobject Listener, string func)
{
if (!checkforlistener (EventType, Listener, func))
{
ArrayList listenerlist = _listeners[eventtype] as ArrayList;
EventListener callback = new EventListener ();
Callback.name = listener. Getinstanceid (). ToString () + "_" + func;
Callback.listener = listener;
Callback.func = func;
Listenerlist.add (callback);
return true;
}
Else
{
return false;
}
}


public bool AddListener (string Eventtype,gameobject listener,string func)
{
if (listener = = NULL && EventType = = null)
{
return false;
}
Return Recordlistener (EventType, Listener, func);
}


public bool RemoveListener (string eventtype, Gameobject Listener, string func = null)
{
if (func = = null)
{
Return RemoveEventListener (EventType, listener);
}
Else
{
Return RemoveEventListener (EventType, Listener, func);
}
}


public void Removealllistener (Gameobject listener)
{
Removealleventlisteners (listener);
}


Send Event
public void sendevent (Eventbase ev)
{
string eventtype = Ev.type;
if (!checkforevent (EventType))
{
Return
}
ArrayList listenerlist = _listeners[eventtype] as ArrayList;
for (int i = 0; i < Listenerlist.count; ++i)
{
EventListener callback = Geteventlistener (EventType, i);
if (callback! = NULL)
{
if (Callback.listener && callback.listener.activeSelf)
{
Callback.listener.SendMessage (Callback.func, Ev, sendmessageoptions.dontrequirereceiver);
}
}
}
}
//
private bool RemoveEventListener (string eventtype, Gameobject listener)
{
if (!checkforevent (EventType)) return false;
ArrayList listenerlist = _listeners[eventtype] as ArrayList;
foreach (EventListener callback in ListenerList)
{
if (string. Equals (Callback.name,listener. Getinstanceid (). ToString ()))
{
Listenerlist.remove (callback);
return true;
}
}
return false;
}


Removing event listeners
private bool RemoveEventListener (string eventtype, Gameobject Listener, string func)
{
if (!checkforevent (EventType)) return false;


ArrayList listenerlist = _listeners[eventtype] as ArrayList;
foreach (EventListener callback in ListenerList)
{
if (string. Equals (Callback.name, (listener). Getinstanceid (). ToString () + "_" + Func)))
{
Listenerlist.remove (callback);
return true;
}
}
return false;
}
Remove all event Listeners
private void Removealleventlisteners (Gameobject listener)
{
foreach (EventListener callback in _listeners)
{
if (string. Equals (Callback.listener.GetInstanceID (). ToString (), listener. Getinstanceid (). ToString ()))
{
_listeners. Remove (callback);
}
}
}


Private EventListener Geteventlistener (string _eventtype,int _index)
{
ArrayList listenerlist = _listeners[_eventtype] as ArrayList;
for (int i = 0; i < Listenerlist.count; ++i)
{
if (i = = _index)
{
Return (EventListener) listenerlist[i];
}
}
return null;
}




Test
public void Update ()
{
if (Input.getmousebuttondown (1))
{
TestEvent e = new TestEvent ();
E.type = "Test";
E.test = "test99999";
Instance.sendevent (e);
Debug.Log ("test");
}
}


public void Start ()
{
Test ();
}
public void Test ()
{
Instance.addlistener ("Test", This.gameobject, "CallBack");
}


public void CallBack (Eventbase e)
{
Debug.Log ("CallBack1");
TestEvent t = (testevent) e;
Debug.Log ("CallBack1 =" + t.test);
Instance.removeeventlistener ("Test", This.gameobject, "CallBack2");
}
}

}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.