TouchCreator.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using UnityEngine;
  2. using System.Reflection;
  3. using System.Collections.Generic;
  4. /// <summary>
  5. /// Touch creator.
  6. /// BASED ON: http://answers.unity3d.com/answers/801637/view.html
  7. /// </summary>
  8. public class TouchCreator
  9. {
  10. static BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
  11. static Dictionary<string, FieldInfo> fields;
  12. object touch;
  13. public float deltaTime { get { return ((Touch)touch).deltaTime; } set { fields["m_TimeDelta"].SetValue(touch, value); } }
  14. public int tapCount { get { return ((Touch)touch).tapCount; } set { fields["m_TapCount"].SetValue(touch, value); } }
  15. public TouchPhase phase { get { return ((Touch)touch).phase; } set { fields["m_Phase"].SetValue(touch, value); } }
  16. public Vector2 deltaPosition { get { return ((Touch)touch).deltaPosition; } set { fields["m_PositionDelta"].SetValue(touch, value); } }
  17. public int fingerId { get { return ((Touch)touch).fingerId; } set { fields["m_FingerId"].SetValue(touch, value); } }
  18. public Vector2 position { get { return ((Touch)touch).position; } set { fields["m_Position"].SetValue(touch, value); } }
  19. public Vector2 rawPosition { get { return ((Touch)touch).rawPosition; } set { fields["m_RawPosition"].SetValue(touch, value); } }
  20. public Touch Create()
  21. {
  22. return (Touch)touch;
  23. }
  24. public TouchCreator()
  25. {
  26. touch = new Touch();
  27. }
  28. static TouchCreator()
  29. {
  30. fields = new Dictionary<string, FieldInfo>();
  31. foreach (var f in typeof(Touch).GetFields(flags))
  32. {
  33. fields.Add(f.Name, f);
  34. //Debug.Log("name: " + f.Name); // Use this to find the names of hidden private fields
  35. }
  36. }
  37. }