Beispiel #1
0
    private void Update()
    {
        if (EventSystem.current.IsPointerOverGameObject())      //return if over UI element
        {
            return;
        }

        if (Input.GetMouseButtonDown(0))                        //Called once
        {
            RaycastHit hit;
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100f, interactionLayer))
            {
                IInteractable inter = hit.collider.GetComponent <IInteractable>();
                if (inter != null && inter.IsCurrentlyInteractive())
                {
                    currentInteractable = inter;
                    inter.OnCompleted  += RemoveInteractable;                                 //We need to set currentInteractable to null when interaction has completed
                    progressUI.GetComponent <RectTransform>().position = Input.mousePosition; //Set progress UI position to where the touch is
                    progressUI.gameObject.SetActive(true);                                    //Show progress UI
                }
            }
        }

        if (currentInteractable == null)
        {
            return;
        }

        if (Input.GetMouseButtonUp(0))
        {
            currentInteractable.Cancel();
            currentInteractable = null;
            HideProgressBar();
        }

        if (currentInteractable == null)
        {
            return;
        }

        if (Input.GetMouseButton(0))
        {
            float progress = currentInteractable.UpdateProgress();
            progressUI.SetProgress(progress);           //Update progress UI
        }
    }