Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

The type or namespace name 'Touch' could not be found

Discussion in 'Scripting' started by Zentify, Aug 16, 2019 at 11:14 AM.

  1. Zentify

    Zentify

    Joined:
    Jul 27, 2019
    Posts:
    10
    I was trying to render as WebGL and it said this was wrong

    C:\Program Files\Unity\Hub\Editor\2019.2.0f1\Editor\Data\Resources\PackageManager\BuiltInPackages\com.unity.ugui\Runtime\EventSystem\InputModules\PointerInputModule.cs(70,61): error CS0246: The type or namespace name 'Touch' could not be found (are you missing a using directive or an assembly reference?)

    Code (csharp):
    1.  
    2. using System.Collections.Generic;
    3. using System.Text;
    4. using UnityEngine.UI;
    5. namespace UnityEngine.EventSystems
    6. {
    7.     /// <summary>
    8.     /// A BaseInputModule for pointer input.
    9.     /// </summary>
    10.     public abstract class PointerInputModule : BaseInputModule
    11.     {
    12.         /// <summary>
    13.         /// Id of the cached left mouse pointer event.
    14.         /// </summary>
    15.         public const int kMouseLeftId = -1;
    16.         /// <summary>
    17.         /// Id of the cached right mouse pointer event.
    18.         /// </summary>
    19.         public const int kMouseRightId = -2;
    20.         /// <summary>
    21.         /// Id of the cached middle mouse pointer event.
    22.         /// </summary>
    23.         public const int kMouseMiddleId = -3;
    24.         /// <summary>
    25.         /// Touch id for when simulating touches on a non touch device.
    26.         /// </summary>
    27.         public const int kFakeTouchesId = -4;
    28.         protected Dictionary<int, PointerEventData> m_PointerData = new Dictionary<int, PointerEventData>();
    29.         /// <summary>
    30.         /// Search the cache for currently active pointers, return true if found.
    31.         /// </summary>
    32.         /// <param name="id">Touch ID</param>
    33.         /// <param name="data">Found data</param>
    34.         /// <param name="create">If not found should it be created</param>
    35.         /// <returns>True if pointer is found.</returns>
    36.         protected bool GetPointerData(int id, out PointerEventData data, bool create)
    37.         {
    38.             if (!m_PointerData.TryGetValue(id, out data) && create)
    39.             {
    40.                 data = new PointerEventData(eventSystem)
    41.                 {
    42.                     pointerId = id,
    43.                 };
    44.                 m_PointerData.Add(id, data);
    45.                 return true;
    46.             }
    47.             return false;
    48.         }
    49.         /// <summary>
    50.         /// Remove the PointerEventData from the cache.
    51.         /// </summary>
    52.         protected void RemovePointerData(PointerEventData data)
    53.         {
    54.             m_PointerData.Remove(data.pointerId);
    55.         }
    56.         /// <summary>
    57.         /// Given a touch populate the PointerEventData and return if we are pressed or released.
    58.         /// </summary>
    59.         /// <param name="input">Touch being processed</param>
    60.         /// <param name="pressed">Are we pressed this frame</param>
    61.         /// <param name="released">Are we released this frame</param>
    62.         /// <returns></returns>
    63.         protected PointerEventData GetTouchPointerEventData(Touch input, out bool pressed, out bool released)
    64.         {
    65.             PointerEventData pointerData;
    66.             var created = GetPointerData(input.fingerId, out pointerData, true);
    67.             pointerData.Reset();
    68.             pressed = created || (input.phase == TouchPhase.Began);
    69.             released = (input.phase == TouchPhase.Canceled) || (input.phase == TouchPhase.Ended);
    70.             if (created)
    71.                 pointerData.position = input.position;
    72.             if (pressed)
    73.                 pointerData.delta = Vector2.zero;
    74.             else
    75.                 pointerData.delta = input.position - pointerData.position;
    76.             pointerData.position = input.position;
    77.             pointerData.button = PointerEventData.InputButton.Left;
    78.             if (input.phase == TouchPhase.Canceled)
    79.             {
    80.                 pointerData.pointerCurrentRaycast = new RaycastResult();
    81.             }
    82.             else
    83.             {
    84.                 eventSystem.RaycastAll(pointerData, m_RaycastResultCache);
    85.                 var raycast = FindFirstRaycast(m_RaycastResultCache);
    86.                 pointerData.pointerCurrentRaycast = raycast;
    87.                 m_RaycastResultCache.Clear();
    88.             }
    89.             return pointerData;
    90.         }
    91.         /// <summary>
    92.         /// Copy one PointerEventData to another.
    93.         /// </summary>
    94.         protected void CopyFromTo(PointerEventData @from, PointerEventData @to)
    95.         {
    96.             @to.position = @from.position;
    97.             @to.delta = @from.delta;
    98.             @to.scrollDelta = @from.scrollDelta;
    99.             @to.pointerCurrentRaycast = @from.pointerCurrentRaycast;
    100.             @to.pointerEnter = @from.pointerEnter;
    101.         }
    102.         /// <summary>
    103.         /// Given a mouse button return the current state for the frame.
    104.         /// </summary>
    105.         /// <param name="buttonId">Mouse button ID</param>
    106.         protected PointerEventData.FramePressState StateForMouseButton(int buttonId)
    107.         {
    108.             var pressed = input.GetMouseButtonDown(buttonId);
    109.             var released = input.GetMouseButtonUp(buttonId);
    110.             if (pressed && released)
    111.                 return PointerEventData.FramePressState.PressedAndReleased;
    112.             if (pressed)
    113.                 return PointerEventData.FramePressState.Pressed;
    114.             if (released)
    115.                 return PointerEventData.FramePressState.Released;
    116.             return PointerEventData.FramePressState.NotChanged;
    117.         }
    118.         protected class ButtonState
    119.         {
    120.             private PointerEventData.InputButton m_Button = PointerEventData.InputButton.Left;
    121.             public MouseButtonEventData eventData
    122.             {
    123.                 get { return m_EventData; }
    124.                 set { m_EventData = value; }
    125.             }
    126.             public PointerEventData.InputButton button
    127.             {
    128.                 get { return m_Button; }
    129.                 set { m_Button = value; }
    130.             }
    131.             private MouseButtonEventData m_EventData;
    132.         }
    133.         protected class MouseState
    134.         {
    135.             private List<ButtonState> m_TrackedButtons = new List<ButtonState>();
    136.             public bool AnyPressesThisFrame()
    137.             {
    138.                 for (int i = 0; i < m_TrackedButtons.Count; i++)
    139.                 {
    140.                     if (m_TrackedButtons[i].eventData.PressedThisFrame())
    141.                         return true;
    142.                 }
    143.                 return false;
    144.             }
    145.             public bool AnyReleasesThisFrame()
    146.             {
    147.                 for (int i = 0; i < m_TrackedButtons.Count; i++)
    148.                 {
    149.                     if (m_TrackedButtons[i].eventData.ReleasedThisFrame())
    150.                         return true;
    151.                 }
    152.                 return false;
    153.             }
    154.             public ButtonState GetButtonState(PointerEventData.InputButton button)
    155.             {
    156.                 ButtonState tracked = null;
    157.                 for (int i = 0; i < m_TrackedButtons.Count; i++)
    158.                 {
    159.                     if (m_TrackedButtons[i].button == button)
    160.                     {
    161.                         tracked = m_TrackedButtons[i];
    162.                         break;
    163.                     }
    164.                 }
    165.                 if (tracked == null)
    166.                 {
    167.                     tracked = new ButtonState { button = button, eventData = new MouseButtonEventData() };
    168.                     m_TrackedButtons.Add(tracked);
    169.                 }
    170.                 return tracked;
    171.             }
    172.             public void SetButtonState(PointerEventData.InputButton button, PointerEventData.FramePressState stateForMouseButton, PointerEventData data)
    173.             {
    174.                 var toModify = GetButtonState(button);
    175.                 toModify.eventData.buttonState = stateForMouseButton;
    176.                 toModify.eventData.buttonData = data;
    177.             }
    178.         }
    179.         /// <summary>
    180.         /// Information about a mouse button event.
    181.         /// </summary>
    182.         public class MouseButtonEventData
    183.         {
    184.             /// <summary>
    185.             /// The state of the button this frame.
    186.             /// </summary>
    187.             public PointerEventData.FramePressState buttonState;
    188.             /// <summary>
    189.             /// Pointer data associated with the mouse event.
    190.             /// </summary>
    191.             public PointerEventData buttonData;
    192.             /// <summary>
    193.             /// Was the button pressed this frame?
    194.             /// </summary>
    195.             public bool PressedThisFrame()
    196.             {
    197.                 return buttonState == PointerEventData.FramePressState.Pressed || buttonState == PointerEventData.FramePressState.PressedAndReleased;
    198.             }
    199.             /// <summary>
    200.             /// Was the button released this frame?
    201.             /// </summary>
    202.             public bool ReleasedThisFrame()
    203.             {
    204.                 return buttonState == PointerEventData.FramePressState.Released || buttonState == PointerEventData.FramePressState.PressedAndReleased;
    205.             }
    206.         }
    207.         private readonly MouseState m_MouseState = new MouseState();
    208.         /// <summary>
    209.         /// Return the current MouseState. Using the default pointer.
    210.         /// </summary>
    211.         protected virtual MouseState GetMousePointerEventData()
    212.         {
    213.             return GetMousePointerEventData(0);
    214.         }
    215.         /// <summary>
    216.         /// Return the current MouseState.
    217.         /// </summary>
    218.         protected virtual MouseState GetMousePointerEventData(int id)
    219.         {
    220.             // Populate the left button...
    221.             PointerEventData leftData;
    222.             var created = GetPointerData(kMouseLeftId, out leftData, true);
    223.             leftData.Reset();
    224.             if (created)
    225.                 leftData.position = input.mousePosition;
    226.             Vector2 pos = input.mousePosition;
    227.             if (Cursor.lockState == CursorLockMode.Locked)
    228.             {
    229.                 // We don't want to do ANY cursor-based interaction when the mouse is locked
    230.                 leftData.position = new Vector2(-1.0f, -1.0f);
    231.                 leftData.delta = Vector2.zero;
    232.             }
    233.             else
    234.             {
    235.                 leftData.delta = pos - leftData.position;
    236.                 leftData.position = pos;
    237.             }
    238.             leftData.scrollDelta = input.mouseScrollDelta;
    239.             leftData.button = PointerEventData.InputButton.Left;
    240.             eventSystem.RaycastAll(leftData, m_RaycastResultCache);
    241.             var raycast = FindFirstRaycast(m_RaycastResultCache);
    242.             leftData.pointerCurrentRaycast = raycast;
    243.             m_RaycastResultCache.Clear();
    244.             // copy the apropriate data into right and middle slots
    245.             PointerEventData rightData;
    246.             GetPointerData(kMouseRightId, out rightData, true);
    247.             CopyFromTo(leftData, rightData);
    248.             rightData.button = PointerEventData.InputButton.Right;
    249.             PointerEventData middleData;
    250.             GetPointerData(kMouseMiddleId, out middleData, true);
    251.             CopyFromTo(leftData, middleData);
    252.             middleData.button = PointerEventData.InputButton.Middle;
    253.             m_MouseState.SetButtonState(PointerEventData.InputButton.Left, StateForMouseButton(0), leftData);
    254.             m_MouseState.SetButtonState(PointerEventData.InputButton.Right, StateForMouseButton(1), rightData);
    255.             m_MouseState.SetButtonState(PointerEventData.InputButton.Middle, StateForMouseButton(2), middleData);
    256.             return m_MouseState;
    257.         }
    258.         /// <summary>
    259.         /// Return the last PointerEventData for the given touch / mouse id.
    260.         /// </summary>
    261.         protected PointerEventData GetLastPointerEventData(int id)
    262.         {
    263.             PointerEventData data;
    264.             GetPointerData(id, out data, false);
    265.             return data;
    266.         }
    267.         private static bool ShouldStartDrag(Vector2 pressPos, Vector2 currentPos, float threshold, bool useDragThreshold)
    268.         {
    269.             if (!useDragThreshold)
    270.                 return true;
    271.             return (pressPos - currentPos).sqrMagnitude >= threshold * threshold;
    272.         }
    273.         /// <summary>
    274.         /// Process movement for the current frame with the given pointer event.
    275.         /// </summary>
    276.         protected virtual void ProcessMove(PointerEventData pointerEvent)
    277.         {
    278.             var targetGO = (Cursor.lockState == CursorLockMode.Locked ? null : pointerEvent.pointerCurrentRaycast.gameObject);
    279.             HandlePointerExitAndEnter(pointerEvent, targetGO);
    280.         }
    281.         /// <summary>
    282.         /// Process the drag for the current frame with the given pointer event.
    283.         /// </summary>
    284.         protected virtual void ProcessDrag(PointerEventData pointerEvent)
    285.         {
    286.             if (!pointerEvent.IsPointerMoving() ||
    287.                 Cursor.lockState == CursorLockMode.Locked ||
    288.                 pointerEvent.pointerDrag == null)
    289.                 return;
    290.             if (!pointerEvent.dragging
    291.                 && ShouldStartDrag(pointerEvent.pressPosition, pointerEvent.position, eventSystem.pixelDragThreshold, pointerEvent.useDragThreshold))
    292.             {
    293.                 ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.beginDragHandler);
    294.                 pointerEvent.dragging = true;
    295.             }
    296.             // Drag notification
    297.             if (pointerEvent.dragging)
    298.             {
    299.                 // Before doing drag we should cancel any pointer down state
    300.                 // And clear selection!
    301.                 if (pointerEvent.pointerPress != pointerEvent.pointerDrag)
    302.                 {
    303.                     ExecuteEvents.Execute(pointerEvent.pointerPress, pointerEvent, ExecuteEvents.pointerUpHandler);
    304.                     pointerEvent.eligibleForClick = false;
    305.                     pointerEvent.pointerPress = null;
    306.                     pointerEvent.rawPointerPress = null;
    307.                 }
    308.                 ExecuteEvents.Execute(pointerEvent.pointerDrag, pointerEvent, ExecuteEvents.dragHandler);
    309.             }
    310.         }
    311.         public override bool IsPointerOverGameObject(int pointerId)
    312.         {
    313.             var lastPointer = GetLastPointerEventData(pointerId);
    314.             if (lastPointer != null)
    315.                 return lastPointer.pointerEnter != null;
    316.             return false;
    317.         }
    318.         /// <summary>
    319.         /// Clear all pointers and deselect any selected objects in the EventSystem.
    320.         /// </summary>
    321.         protected void ClearSelection()
    322.         {
    323.             var baseEventData = GetBaseEventData();
    324.             foreach (var pointer in m_PointerData.Values)
    325.             {
    326.                 // clear all selection
    327.                 HandlePointerExitAndEnter(pointer, null);
    328.             }
    329.             m_PointerData.Clear();
    330.             eventSystem.SetSelectedGameObject(null, baseEventData);
    331.         }
    332.         public override string ToString()
    333.         {
    334.             var sb = new StringBuilder("<b>Pointer Input Module of type: </b>" + GetType());
    335.             sb.AppendLine();
    336.             foreach (var pointer in m_PointerData)
    337.             {
    338.                 if (pointer.Value == null)
    339.                     continue;
    340.                 sb.AppendLine("<B>Pointer:</b> " + pointer.Key);
    341.                 sb.AppendLine(pointer.Value.ToString());
    342.             }
    343.             return sb.ToString();
    344.         }
    345.         /// <summary>
    346.         /// Deselect the current selected GameObject if the currently pointed-at GameObject is different.
    347.         /// </summary>
    348.         /// <param name="currentOverGo">The GameObject the pointer is currently over.</param>
    349.         /// <param name="pointerEvent">Current event data.</param>
    350.         protected void DeselectIfSelectionChanged(GameObject currentOverGo, BaseEventData pointerEvent)
    351.         {
    352.             // Selection tracking
    353.             var selectHandlerGO = ExecuteEvents.GetEventHandler<ISelectHandler>(currentOverGo);
    354.             // if we have clicked something new, deselect the old thing
    355.             // leave 'selection handling' up to the press event though.
    356.             if (selectHandlerGO != eventSystem.currentSelectedGameObject)
    357.                 eventSystem.SetSelectedGameObject(null, pointerEvent);
    358.         }
    359.     }
    360. }
    361.  
     
  2. Dalton-Lima

    Dalton-Lima

    Joined:
    Dec 21, 2016
    Posts:
    5
    You can try resetting the packages to default values (go to Help -> Reset Package to Defaults)