The simplest way is to give your prefab a suitable collider, and attach a script to the prefab that looks a bit like this:
public class TouchDetector : MonoBehaviour {
// Called when a collider attached to this game object is pressed.
void OnMouseDown() {
// Put the actions you want to perform to respond to the touch here.
Debug.LogFormat("Object {0} got touched!", gameObject.name);
}
}
When you build, Unity looks for any MonoBehaviour scripts that have OnMouseDown
or other mouse-related messages. If it finds them, then it automatically fires a ray into your scene every frame to see what collider it hits first, and then sends the corresponding up/down/enter/exit messages.
This also works for a single finger touch or stylus. I don't know if it handles multitouch in a useful way.
The downside is that this is a very generic solution, so it might be more heavyweight than you need (maybe you don't care about enter/exit/up messages at all, or only some of your objects need the pointer interaction, or you have gobs of objects and don't want to put a receiver script on every one of them...)
So, in many cases you can make this more efficient by firing the ray yourself, only when it's appropriate for your game rules.
Here we could have a single script that fires a ray for each touch and detects what it hits - without needing a receiving script on each prefab instance:
public class TouchManager : MonoBehaviour {
public LayerMask touchableLayers;
void Update() {
var camera = Camera.main;
int count = Input.touchCount;
for(int i = 0; i < count; i++) {
var touch = Input.GetTouch(i);
var ray = camera.ScreenPointToRay(touch.position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, Mathf.infinity, touchableLayers)) {
// Pur the actions you want to perform to respond to the touch here.
Debug.LogFormat("Object {0} got touched!", hit.collider.gameObject.name);
}
}
}
}