Author Topic: NGUI & TouchScript  (Read 583 times)

znerolnoht

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 5
    • View Profile
NGUI & TouchScript
« on: March 26, 2016, 09:46:25 AM »
Starting a new topic based on this old one: http://www.tasharen.com/forum/index.php?topic=4984.0

ArenMook made a great Class, TouchScriptNGUI, that gets NGUI working with TouchScript.

I've updated the TouchScriptNGUI class to work with Windows 10 (below)
Also, The original post has a link to a a very old ver of TouchScript. After some digging, I found the most recent version of TouchScript that the TouchScriptNGUI class works with is TouchScript 6.10.

6.10 has a number of advantages over the version linked to in the original post.

Download TouchScript 6.10 here: https://github.com/TouchScript/TouchScript/releases/tag/6.10

I would have updated the class to work with newest version of TouchScript but I'm still new to C# so its out of the range of my abilities.


  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. // Download the appropriate TouchScript 6.10 package here:
  6. // https://github.com/TouchScript/TouchScript/releases/tag/6.10
  7.  
  8. using TouchScript;
  9. using TouchScript.InputSources;
  10.  
  11. /// <summary>
  12. /// Bridge script between TouchScript and NGUI. Simply attach this script to any game object.
  13. /// </summary>
  14.  
  15. public class TouchScriptNGUI : MonoBehaviour
  16. {
  17.         static public TouchScriptNGUI instance;
  18.  
  19.         /// <summary>
  20.         /// Whether the multi-touch should be enabled or not.
  21.         /// </summary>
  22.  
  23.         static public bool isEnabled
  24.         {
  25.                 get
  26.                 {
  27.                         if (instance != null) return instance.enabled;
  28.                         return PlayerPrefs.GetInt("Multitouch", 0) == 1;
  29.                 }
  30.                 set
  31.                 {
  32.                         PlayerPrefs.SetInt("Multitouch", value ? 1 : 0);
  33.                         if (instance != null) instance.enabled = value;
  34.                 }
  35.         }
  36.  
  37.         [System.NonSerialized] bool mActive = false;
  38.         [System.NonSerialized] BetterList<UICamera.Touch> mTouches = new BetterList<UICamera.Touch>();
  39.  
  40.         void Awake ()
  41.         {
  42.                 if (instance == null)
  43.                 {
  44.                         enabled = true;//isEnabled;
  45.                         instance = this;
  46.                 }
  47.                 else Destroy(gameObject);
  48.         }
  49.  
  50.         void OnDestroy () { if (instance == this) instance = null; }
  51.  
  52.         void OnEnable ()
  53.         {
  54.                 string operatingSystem = SystemInfo.operatingSystem;
  55.                
  56.                 if (operatingSystem.StartsWith("Windows 8") || operatingSystem.StartsWith("Windows 10"))
  57.                 {
  58.                         mActive = true;
  59.                         NGUITools.AddMissingComponent<TouchManager>(gameObject);
  60.                         NGUITools.AddMissingComponent<Win8TouchInput>(gameObject);
  61. #if UNITY_EDITOR
  62.                         Debug.Log("-> Windows 8 Multi-touch active", this);
  63. #endif
  64.                 }
  65.                 else if (operatingSystem.StartsWith("Windows 7"))
  66.                 {
  67.                         mActive = true;
  68.                         NGUITools.AddMissingComponent<TouchManager>(gameObject);
  69.                         NGUITools.AddMissingComponent<Win7TouchInput>(gameObject);
  70. #if UNITY_EDITOR
  71.                         Debug.Log("-> Windows 7 Multi-touch active", this);
  72. #endif
  73.                 }
  74.                
  75.                 if (mActive)
  76.                 {
  77.                         UICamera.GetInputTouchCount = OnGetTouchCount;
  78.                         UICamera.GetInputTouch = OnGetTouch;
  79.                        
  80.                         ITouchManager instance = TouchManager.Instance;
  81.                         instance.TouchesBegan += OnTouchBegan;
  82.                         instance.TouchesEnded += OnTouchEnded;
  83.                         instance.TouchesMoved += OnTouchMove;
  84.                         instance.TouchesCancelled += OnTouchCancel;
  85.                 }
  86.                 else enabled = false;
  87.         }
  88.  
  89.         void OnDisable ()
  90.         {
  91.                 if (mActive)
  92.                 {
  93.                         mActive = false;
  94.                         UICamera.GetInputTouchCount = null;
  95.                         UICamera.GetInputTouch = null;
  96.                         ITouchManager instance = TouchManager.Instance;
  97.                        
  98.                         if (instance != null)
  99.                         {
  100.                                 instance.TouchesBegan -= OnTouchBegan;
  101.                                 instance.TouchesEnded -= OnTouchEnded;
  102.                                 instance.TouchesMoved -= OnTouchMove;
  103.                                 instance.TouchesCancelled -= OnTouchCancel;
  104.                         }
  105.                         mTouches.Clear();
  106.                 }
  107.         }
  108.  
  109.         int OnGetTouchCount () { return mTouches.size; }
  110.         UICamera.Touch OnGetTouch (int index) { return mTouches[index]; }
  111.  
  112.         void OnTouchBegan (object sender, TouchEventArgs e)
  113.         {
  114.                 foreach (ITouch touch in e.Touches)
  115.                 {
  116.                         if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
  117.                        
  118.                         mTouches.Add(new UICamera.Touch()
  119.                         {
  120.                                 phase = TouchPhase.Began,
  121.                                 fingerId = touch.Id,
  122.                                 position = touch.Position,
  123.                                 tapCount = 1
  124.                         });
  125.                 }
  126.         }
  127.  
  128.         void OnTouchEnded (object sender, TouchEventArgs e)
  129.         {
  130.                 foreach (ITouch touch in e.Touches)
  131.                 {
  132.                         if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
  133.                        
  134.                         for (int index = 0; index < mTouches.size; ++index)
  135.                         {
  136.                                 UICamera.Touch t = mTouches[index];
  137.                                
  138.                                 if (t.fingerId == touch.Id)
  139.                                 {
  140.                                         t.phase = TouchPhase.Ended;
  141.                                         t.position = touch.Position;
  142.                                         break;
  143.                                 }
  144.                         }
  145.                 }
  146.         }
  147.  
  148.         void OnTouchMove (object sender, TouchEventArgs e)
  149.         {
  150.                 foreach (ITouch touch in e.Touches)
  151.                 {
  152.                         if (touch.Tags.HasTag(Tags.INPUT_MOUSE)) continue;
  153.                        
  154.                         for (int index = 0; index < mTouches.size; ++index)
  155.                         {
  156.                                 UICamera.Touch t = mTouches[index];
  157.                                
  158.                                 if (t.fingerId == touch.Id)
  159.                                 {
  160.                                         t.position = touch.Position;
  161.                                         break;
  162.                                 }
  163.                         }
  164.                 }
  165.         }
  166.  
  167.         void OnTouchCancel (object sender, TouchEventArgs e) { OnTouchEnded(sender, e); }
  168.  
  169.         void LateUpdate ()
  170.         {
  171.                 int index = 0;
  172.                
  173.                 while (index < mTouches.size)
  174.                 {
  175.                         UICamera.Touch touch = mTouches[index];
  176.                        
  177.                         if (touch.phase == TouchPhase.Ended)
  178.                         {
  179.                                 mTouches.RemoveAt(index);
  180.                         }
  181.                         else
  182.                         {
  183.                                 touch.phase = TouchPhase.Moved;
  184.                                 ++index;
  185.                         }
  186.                 }
  187.         }
  188. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUI & TouchScript
« Reply #1 on: March 27, 2016, 02:49:56 AM »
Unfortunately the forum flagged your post as "spam", which means no one but moderators can see it. Still, I've added your link to the original post and updated the windows 10 line as well. Thanks!