How to Use Co-routine For Taking Touch Event in Unity(Mouse Event And Keyboard KeyEvent), Also Explained How to call coroutine from other script
1. Co-routine for Taking Touch Event
Write Following in followpath.CS
Step 2 : Following is Co-Routine for taking touch event.
------------------------------------------------------------------------------
// This method accepts gameobject name parameter,
// Then in this method we get name of object on which user tap.
// If both equals then we end while loop.In this way we can take touch event and mouse click event.
public IEnumerator WaitForTouch_Player(string player1)
{
bool found = false;
Touch touch;
Ray screenRay;
RaycastHit hit;
string objname;
while (Input.touchCount == 0 || found == false)
{
if (Input.touchCount > 0) {
touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
screenRay = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (screenRay, out hit)) {
objname = hit.collider.gameObject.name;
if (objname.Equals (player1)) {
found = true;
print("object tap");
yield return null;
break;
}
}
}
}
yield return null;
}
yield return null;
}
Step 3 : How to call Co-Routine.
----------------------------------
StartCoroutine(WaitForTouch_Player("player_blue"));
2. Co-routine for Taking Touch Event from other Script.
Now if you want to use Co-Routine from other script it is explained as below.
--------------------------------------------------------------------
There is two files named followpath.cs and ReyCast.cs
Co-routine will be in ReyCast.cs and we call coroutine from followpath.cs
Both file attached to path gameobject in editor.
--------------------------------------------------------------------
followpath.cs
void Start () {
// First we find gameobject.
// Then we find component of ReyCasr script that will use to call co-routine from ReyCast script.
ReyCast other = (ReyCast) go.GetComponent(typeof(ReyCast));
// "player_blue" here is gameobject name.
other.StartCoroutine (other.WaitForTouch_Player ("player_blue"));
}
// This method will be called when objectname on which you tap and argument which you send while calling co-routine same.
public void callback() {
// call back called from coroutine.
}
-------------------------------------------------------
ReyCast.cs
// First we find gameobject.
// Then we find component of followpath script that will use to call callback method of followpath script.
void Start() {
GameObject go = GameObject.Find("path");
followpath pre = (followpath)go.GetComponent(typeof(followpath));
}
// This method accepts gameobject name parameter,
// Then in this method we get name of object on which user tap.
// If both equals then we end while loop and call method callback of followpath script.
public IEnumerator WaitForTouch_Player(string player1)
{
bool found = false;
Touch touch;
Ray screenRay;
RaycastHit hit;
string objname;
while (Input.touchCount == 0 || found == false)
{
if (Input.touchCount > 0) {
touch = Input.GetTouch (0);
if (touch.phase == TouchPhase.Began) {
screenRay = Camera.main.ScreenPointToRay (touch.position);
if (Physics.Raycast (screenRay, out hit)) {
objname = hit.collider.gameObject.name;
if (objname.Equals (player1)) {
found = true;
pre .callback ();
yield return null;
break;
}
}
}
}
yield return null;
}
yield return null;
}
}
Comments
Post a Comment