using System;
using System.Collections.Generic;
using UnityEngine;
// Download the appropriate TouchScript 8.2 package here:
// https://github.com/TouchScript/TouchScript/releases/tag/8.2
using TouchScript;
using TouchScript.InputSources;
/// <summary>
/// Bridge script between TouchScript and NGUI. Simply attach this script to any game object.
/// </summary>
public class TouchScriptNGUI : MonoBehaviour
{
static public TouchScriptNGUI instance;
/// <summary>
/// Whether the multi-touch should be enabled or not.
/// </summary>
static public bool isEnabled
{
get
{
if (instance != null) return instance.enabled;
return PlayerPrefs.GetInt("Multitouch", 1) == 1;
}
set
{
PlayerPrefs.SetInt("Multitouch", value ? 1 : 0);
if (instance != null) instance.enabled = value;
}
}
[System.NonSerialized] bool mActive = false;
[System.NonSerialized] BetterList
<UICamera
.Touch> mTouches
= new BetterList
<UICamera
.Touch>();
void Awake ()
{
if (instance == null)
{
enabled = isEnabled;
instance = this;
}
else Destroy(gameObject);
}
void OnDestroy () { if (instance == this) instance = null; }
void OnEnable ()
{
mActive = true;
NGUITools.AddMissingComponent<TouchManager>(gameObject);
UICamera.GetInputTouchCount = OnGetTouchCount;
UICamera.GetInputTouch = OnGetTouch;
ITouchManager instance = TouchManager.Instance;
instance.TouchesBegan += OnTouchBegan;
instance.TouchesEnded += OnTouchEnded;
instance.TouchesMoved += OnTouchMove;
instance.TouchesCancelled += OnTouchCancel;
}
void OnDisable ()
{
if (mActive)
{
mActive = false;
UICamera.GetInputTouchCount = null;
UICamera.GetInputTouch = null;
ITouchManager instance = TouchManager.Instance;
if (instance != null)
{
instance.TouchesBegan -= OnTouchBegan;
instance.TouchesEnded -= OnTouchEnded;
instance.TouchesMoved -= OnTouchMove;
instance.TouchesCancelled -= OnTouchCancel;
}
mTouches.Clear();
}
}
int OnGetTouchCount () { return mTouches.size; }
UICamera.Touch OnGetTouch (int index) { return mTouches[index]; }
void OnTouchBegan (object sender, TouchEventArgs e)
{
foreach (TouchPoint touch in e.Touches)
{
if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
mTouches
.Add(new UICamera
.Touch() {
phase = TouchPhase.Began,
fingerId = touch.Id,
position = touch.Position,
tapCount = 1
});
}
}
void OnTouchEnded (object sender, TouchEventArgs e)
{
foreach (TouchPoint touch in e.Touches)
{
if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
for (int index = 0; index < mTouches.size; ++index)
{
UICamera.Touch t = mTouches[index];
if (t.fingerId == touch.Id)
{
t.phase = TouchPhase.Ended;
t.position = touch.Position;
break;
}
}
}
}
void OnTouchMove (object sender, TouchEventArgs e)
{
foreach (TouchPoint touch in e.Touches)
{
if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
for (int index = 0; index < mTouches.size; ++index)
{
UICamera.Touch t = mTouches[index];
if (t.fingerId == touch.Id)
{
t.position = touch.Position;
break;
}
}
}
}
void OnTouchCancel (object sender, TouchEventArgs e) { OnTouchEnded(sender, e); }
void LateUpdate ()
{
int index = 0;
while (index < mTouches.size)
{
UICamera.Touch touch = mTouches[index];
if (touch.phase == TouchPhase.Ended)
{
mTouches.RemoveAt(index);
}
else
{
touch.phase = TouchPhase.Moved;
++index;
}
}
}
}