NGUI + TouchScript

Random discussions.

NGUI + TouchScript

Postby znerolnoht » Sat Mar 26, 2016 5:52 pm

For anyone that's interested this is an update of some code and a conversation started on NGUI's board:http://www.tasharen.com/forum/index.php?topic=4984.0

ArenMook made a great Class, TouchScriptNGUI, that gets NGUI working with TouchScript. It was a little out of date so I've made some changes and notes that should get the code as up to date as possible.

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. If anyone can update this to work with the newest version of TouchScript I'll buy you a Coke!


Code: Select all
using System;
using System.Collections.Generic;
using UnityEngine;
 
// Download the appropriate TouchScript 6.10 package here:
// https://github.com/TouchScript/TouchScript/releases/tag/6.10
 
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 = true;//isEnabled;
         instance = this;
      }
      else Destroy(gameObject);
   }
 
   void OnDestroy () { if (instance == this) instance = null; }
 
   void OnEnable ()
   {
      string operatingSystem = SystemInfo.operatingSystem;
      
      if (operatingSystem.StartsWith("Windows 8") || operatingSystem.StartsWith("Windows 10"))
      {
         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;
         }
      }
   }
}
znerolnoht
 
Posts: 1
Joined: Fri Nov 27, 2015 10:35 pm

Re: NGUI + TouchScript

Postby valyard » Wed Apr 13, 2016 3:49 pm

Thanks for the info. I wasn't aware of this script. I'll take a look what can be done to update it to the most recent version of TouchScript.
valyard
Site Admin
 
Posts: 435
Joined: Mon Sep 08, 2014 11:57 pm

Re: NGUI + TouchScript

Postby Aurigan » Sat Aug 20, 2016 8:25 am

Hi all, I'm also struggling to get TouchScript and NGUI to work. I posted my progress over on the Tasharen forum : http://www.tasharen.com/forum/index.php?topic=14812.0 along with an updated TouchScriptNGUI class. Any help much appreciated!
Aurigan
 
Posts: 1
Joined: Sat Aug 20, 2016 8:20 am


Return to General Discussion

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests