1
votes

I'm developing a multimedia app using C# and Unity.

In this app, user can move an object using mouse wheel; so I wrote

private void MoveObjectUpDown(float value)
{
     MyObject.transform.position = MyObject.transform.position + new Vector3(0, value, 0); 
     StartCoroutine(UpdateObjectPositionToServer(MyObject.transform.position, MyObject.MyObjectID);            

}

MyObjectUpAndDown is called from Update checking

Input.GetAxis("Mouse ScrollWheel");

UpdateObjectPositionToServer is a call to a web service; in this web service I update a MySql Table via Query.

As you can imagine, if user move whell mouse to move my object, there will be a lot of call to UpdateObjectPositionToServer; my question is:

Which approach Can I use not to call UpdateObjectPositionToServer after each user movement, but just one time , at the end of user movement (maybe, 1 or 2 seconds user don't move that object ) ?

1

1 Answers

1
votes

One way to approach this would be to create a wrapper coroutine that waits for some period of time before starting the UpdateObjectPositionToServer coroutine, and stop any already previously existing instance of the wrapper coroutine.

private IEnumerator currentWaitCoroutine = null;

private void MoveObjectUpDown(float value)
{
     MyObject.transform.position = MyObject.transform.position + new Vector3(0, value, 0); 

     if (currentWaitCoroutine != null)
     {
         StopCoroutine(currentWaitCoroutine);
     }

     currentWaitCoroutine = WaitAndCall(1.5f);
     StartCoroutine(currentWaitCoroutine);
}

private IEnumerator WaitAndCall(float seconds) {
    yield return new WaitForSeconds(seconds);

    StartCoroutine(UpdateObjectPositionToServer(MyObject.transform.position, MyObject.MyObjectID);   
}