using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace MoreMountains.Tools
{
///
/// Add this class to a canvas and it'll automatically reposition TouchPrefabs at the position of touches
/// You can set a higher TouchProvision if your game gets more than the default number (6) simultaneous touches
/// Disable/enable this mono for it to stop/work
///
public class MMDebugTouchDisplay : MonoBehaviour
{
[Header("Bindings")]
/// the canvas to display the TouchPrefabs on
public Canvas TargetCanvas;
[Header("Touches")]
/// the prefabs to instantiate to signify the position of the touches
public RectTransform TouchPrefab;
/// the amount of these prefabs to pool and provision
public int TouchProvision = 6;
protected List _touchDisplays;
///
/// On Start we initialize our pool
///
protected virtual void Start()
{
Initialization();
}
///
/// Creates the pool of prefabs
///
protected virtual void Initialization()
{
_touchDisplays = new List();
for (int i = 0; i < TouchProvision; i++)
{
RectTransform touchDisplay = Instantiate(TouchPrefab);
touchDisplay.transform.SetParent(TargetCanvas.transform);
touchDisplay.name = "MMDebugTouchDisplay_" + i;
touchDisplay.gameObject.SetActive(false);
_touchDisplays.Add(touchDisplay);
}
this.enabled = false;
}
///
/// On update we detect touches and move our prefabs at their position
///
protected virtual void Update()
{
DisableAllDisplays();
DetectTouches();
}
///
/// Acts on all touches
///
protected virtual void DetectTouches()
{
for (int i = 0; i < Input.touchCount; ++i)
{
_touchDisplays[i].gameObject.SetActive(true);
_touchDisplays[i].position = Input.GetTouch(i).position;
}
}
///
/// Disables all touch prefabs
///
protected virtual void DisableAllDisplays()
{
foreach(RectTransform display in _touchDisplays)
{
display.gameObject.SetActive(false);
}
}
///
/// When this mono gets disabled we turn all our prefabs off
///
protected virtual void OnDisable()
{
DisableAllDisplays();
}
}
}