- Home /
Multiple events into one event
Hello , I made many delegates, (clickA,clickB,clickC... etc...) each touch will trigger an action "destroy()" i have 34 prefabs ( each for a letter in the arabic alphabet) i attached to each letter a script like so : using UnityEngine; using System.Collections;
public class touchA : MonoBehaviour {
void OnEnable()
{
Fly fly = GetComponent<Fly>();
Mainkeyboard.clickA += fly.destroy;
}
void OnDisable()
{
Fly fly = GetComponent<Fly>();
Mainkeyboard.clickA -= fly.destroy;
}
}
So the Question is instead of having to make 34 scripts and then attach each one to a prefab, Am i not able to make one script , that gets attached to prefab and then from a drop down menu(List) and pick which delegate i want?? thank you for your time. here is my main delegate handler.
using UnityEngine;
using System.Collections;
public class Mainkeyboard : MonoBehaviour
{
public delegate void ClickAction();
public static event ClickAction clickA;
public static event ClickAction clickS;
public static event ClickAction clickD;
public static event ClickAction clickF;
public static event ClickAction clickG;
public static event ClickAction clickH;
public static event ClickAction clickJ;
public static event ClickAction clickK;
public static event ClickAction clickL;
public static event ClickAction clickL2;
public static event ClickAction clickL3;
public static event ClickAction clickQ;
public static event ClickAction clickW;
public static event ClickAction clickE;
public static event ClickAction clickR;
public static event ClickAction clickT;
public static event ClickAction clickY;
public static event ClickAction clickU;
public static event ClickAction clickI;
public static event ClickAction clickO;
public static event ClickAction clickP;
public static event ClickAction clickP2;
public static event ClickAction clickP3;
public static event ClickAction clickQ2;
public static event ClickAction clickZ;
public static event ClickAction clickX;
public static event ClickAction clickC;
public static event ClickAction clickV;
public static event ClickAction clickB;
public static event ClickAction clickN;
public static event ClickAction clickM;
public static event ClickAction clickM2;
public static event ClickAction clickM3;
public static event ClickAction clickM4;
}
Why don't you have a single delegate that takes a parameter which is the button pressed?
Answer by fafase · Apr 04, 2014 at 02:31 PM
Considering you have all of your prefabs with the given letter as name then you could simplify by subscribing all of them to one event and the performing a generic check:
public class Mainkeyboard :MonoBehaviour{
public delegate void HandlerEvent(string key);
public static event HandlerEvent OnPress = new HandlerEvent((key)=>{});
void Update(){
// Check if pressed
// pass the press key to the event
if(Input.anyKeyDown)OnPress(Input.inputString);
}
}
public class Touch:MonoBehaviour{
private string givenName;
void Start()
{
givenName = gameObject.name;
Mainkeyboard.OnPress += CheckKey;
}
void CheckKey(string key){
if(key == givenName)// Do what it should
}
// Remember to remove the subscription on destroy
}
Now you may have to work that out a little since Input.inputString works with Ascii and I would think arabic letters are unicode.
Also read the doc as it seems to require a little parsing http://docs.unity3d.com/Documentation/ScriptReference/Input-inputString.html
dude you managed to solve two birds with one stone!!! :D thank you loads!!! , ins$$anonymous$$d of string and given name , i made it into $$anonymous$$eyCode so i can assign each prefab to its own keycode and it works like magic!! thanks again!!!!!!
Answer by Berenger · Apr 02, 2014 at 07:30 AM
Declare a public integer and use it to access the event array :
myEvents[eventIndex]();
By "around 30 prefabs", you mean 30 instances of the same prefab right ?
An array of event might not be a good idea actually, System.Action seems to be preferable.
no i have 30 diffrent prefabs ... (Game objects ) consider them as enemies! ( they are letters ) each letter needs its own keyboard input to kill . on a physical keyboard it managed that easy. but for the touch input i made around 30 buttons for each letter ( they are Arabic letters ) each GUI button gives out a OnClicked delegate event , which then destroys the prefab(the letter) so I made one Delegate handler ( $$anonymous$$ainkeyboard) which has the deffrient buttons and Onclicks I want to make one fly script that allows me to handle all the prefabs at once ... but setting which delegate effects which prefab. as i have it right now . i have to rewrite all of the script 30 times and set each delegate to each prefab ... i think having around 30 scripts for each prefab is a bit crazy!
do you have any idea of how i can do it? if it is possible.
Your answer
