Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Last active January 11, 2018 16:06
Show Gist options
  • Save kirillrybin/d971b94320c5b4aeab80 to your computer and use it in GitHub Desktop.
Save kirillrybin/d971b94320c5b4aeab80 to your computer and use it in GitHub Desktop.
TouchScriptNGUI from http://goo.gl/bamSBk for TouchScript (v.8 and below)
using System;
using System.Collections.Generic;
using UnityEngine;
// Download the appropriate TouchScript package here:
// https://github.com/DynaBots/Stitch/tree/master/Assets/TouchScript/Packages
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", 0) == 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 ()
{
string operatingSystem = SystemInfo.operatingSystem;
if (operatingSystem.StartsWith("Windows 8"))
{
mActive = true;
NGUITools.AddMissingComponent<TouchManager>(gameObject);
NGUITools.AddMissingComponent<Win8TouchInput>(gameObject);
#if UNITY_EDITOR
Debug.Log("Windows 8 Multi-touch active", this);
#endif
}
else if (operatingSystem.StartsWith("Windows 7"))
{
mActive = true;
NGUITools.AddMissingComponent<TouchManager>(gameObject);
NGUITools.AddMissingComponent<Win7TouchInput>(gameObject);
#if UNITY_EDITOR
Debug.Log("Windows 7 Multi-touch active", this);
#endif
}
if (mActive)
{
UICamera.GetInputTouchCount = OnGetTouchCount;
UICamera.GetInputTouch = OnGetTouch;
ITouchManager instance = TouchManager.Instance;
instance.TouchesBegan += OnTouchBegan;
instance.TouchesEnded += OnTouchEnded;
instance.TouchesMoved += OnTouchMove;
instance.TouchesCancelled += OnTouchCancel;
}
else enabled = false;
}
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 (ITouch 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 (ITouch 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 (ITouch 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;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment