I can, but I will need to strip them of all but basic functionality
For Delegates, I created an Input Manager.
This manager takes what was originally about 50 objects all checking MouseButtonDown(0) with a single Update function call that passes out functions to objects that register.
This is an incredibly stripped down version
using UnityEngine;
using System.Collections;
public delegate void TouchEvent(Vector3 position);
public class InputManager : MonoBehaviour {
public event TouchEvent OnTouchDown, OnTouchUp;
int prevTouchCount = 0;
private static InputManager instance;
public static InputManager Instance
{
get
{
if (instance == null)
{
instance = new GameObject ("InputManager").AddComponent(typeof(InputManager)) as InputManager;
}
return instance;
}
}
void Update()
{
int touchCount = Input.touchCount;
if(touchCount != prevTouchCount)
{
if(touchCount > prevTouchCount)
{
OnTouchDown(Input.mousePosition);
}
else if(touchCount < prevTouchCount)
{
OnTouchUp(Input.mousePosition);
}
}
prevTouchCount = touchCount;
}
}
and for event objects:
using UnityEngine;
using System.Collections;
public class Event: InputBehaviour {
public static bool isPlaceable = false;
void Start()
{
InputManager.Instance.OnTouchDown += OnPressed;
}
public override void OnPressed(Vector3 position)
{
//Perform Events
}
}
I have a singleton pattern for the InputManager and an abstract class and Interface for events, but it allows you to create an event system that reduces Update calls significantly on the iPhone. You register only for events you want to use, or use an array of delegates to create an order to your event operations. I found it sped up event handling significantly.
Note: The InputManager never has to be explicitly created. As soon as a single object attempts to register with it it will be created. That is what the Instance stuff is for. It only creates itself if it will be used.
Coroutines are more just understanding what exactly can be done with them. I have an easy example while also using the Event system.
using UnityEngine;
using System.Collections;
using System.IO;
public class BasicTest: MonoBehaviour, IInputBehaviour
{
bool isGrabbed;
void Start ()
{
isGrabbed = false;
InputManager.Instance.OnTouchDown += OnPressed;
InputManager.Instance.OnTouchUp += OnReleased;
}
public void OnPressed(Vector3 position)
{
if(isGrabbed == false)
{
isGrabbed = true;
StartCoroutine(Grabbed());
}
}
public void OnReleased(Vector3 position)
{
if( isGrabbed == true)
{
isGrabbed = false;
}
}
private IEnumerator Grabbed()
{
while(isGrabbed)
{
//Do Stuff
yield return new WaitForFixedUpdate();
}
}
}
I register with the previous InputManager and while having no update loop, I can still maintain an action while the inputs are held down.
This creates a temporary Update cycle through coroutines that will only exist when necessary. It operates through a faster system than reflection and is therefore faster than an Update function.
You can add parameters and checks to when it is initiated, and the cycle gets maintained by a boolean.
You can optimize even further with events that are not very time dependent by using WaitForSeconds in this situation and therefore only updating every half or full second if necessary.
Note: These optimizations are probably not nearly as crucial on desktop builds, as they have much faster hardware, you could be better off optimizing somewhere else if coding in this sort of event logic would not be as fluid for you.