示例#1
0
        // Update is called once per frame
        void Update()
        {
            wasHeld = isHeld;
            isHeld  = false;

            //clear out any touches we have claimed that no longer exist
            if (onlyPressingTouches)
            {
                for (int k = 0; k < claimedTouches.Count; k++)
                {
                    if (!SinputTouchManager.TouchExists(claimedTouches[k]))
                    {
                        claimedTouches.RemoveAt(k);
                        k--;
                    }
                }
            }


            //detect touches
            bool canUseThisTouch = false;
            bool claimThisTouch  = false;

            for (int i = 0; i < Input.touchCount; i++)
            {
                canUseThisTouch = false;
                claimThisTouch  = false;
                if (!onlyPressingTouches)
                {
                    canUseThisTouch = true;
                }
                if (onlyPressingTouches)
                {
                    //if this touch is one we have already claimed, we can use it
                    for (int k = 0; k < claimedTouches.Count; k++)
                    {
                        if (Input.touches[i].fingerId == claimedTouches[k])
                        {
                            canUseThisTouch = true;
                        }
                    }

                    //if this touch is a press and has not yet been claimed we can use it
                    if (Input.touches[i].phase == TouchPhase.Began && !SinputTouchManager.IsClaimed(Input.touches[i].fingerId))
                    {
                        canUseThisTouch = true;
                        claimThisTouch  = true;
                    }
                }

                if (canUseThisTouch)
                {
                    //see if this touch, touches this button
                    Ray ray = Camera.main.ScreenPointToRay(Input.touches[i].position);

                    if (touchCollider)
                    {
                        //test against a collider
                        RaycastHit hit = new RaycastHit();
                        if (touchCollider.Raycast(ray, out hit, 9999f))
                        {
                            isHeld = true;

                            if (claimThisTouch)
                            {
                                SinputTouchManager.ClaimTouch(Input.touches[i].fingerId);
                                claimedTouches.Add(Input.touches[i].fingerId);
                            }
                        }
                    }
                    else
                    {
                        //test against button plane within a radius
                        float hitDistance = 0f;
                        buttonPlane.SetNormalAndPosition(transform.forward, transform.position);
                        if (buttonPlane.Raycast(ray, out hitDistance))
                        {
                            if (Vector3.Distance(transform.position, ray.origin + ray.direction.normalized * hitDistance) < collisionRadius)
                            {
                                isHeld = true;
                                if (claimThisTouch)
                                {
                                    SinputTouchManager.ClaimTouch(Input.touches[i].fingerId);
                                    claimedTouches.Add(Input.touches[i].fingerId);
                                }
                            }
                        }
                    }
                }
            }


            //make it work with mouse for debug
            if (debugMouse)
            {
                if (Input.GetKey(KeyCode.Mouse0))
                {
                    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                    if (touchCollider)
                    {
                        RaycastHit hit = new RaycastHit();
                        if (touchCollider.Raycast(ray, out hit, 9999f))
                        {
                            isHeld = true;
                        }
                    }
                    else
                    {
                        float hitDistance = 0f;
                        buttonPlane.SetNormalAndPosition(transform.forward, transform.position);
                        if (buttonPlane.Raycast(ray, out hitDistance))
                        {
                            if (Vector3.Distance(transform.position, ray.origin + ray.direction.normalized * hitDistance) < collisionRadius)
                            {
                                isHeld = true;
                            }
                        }
                    }
                }
            }

            //update virtual input
            SinputSystems.VirtualInputs.SetVirtualButton(virtualInputID, isHeld);

            //make the button pretty
            spriteRenderer.color = Color.Lerp(spriteRenderer.color, color, Time.deltaTime * 10f);
            if (wasHeld != isHeld)
            {
                //change
                if (isHeld)
                {
                    spriteRenderer.sprite        = heldSprite;
                    spriteRenderer.color         = pressColor;
                    labelContainer.localPosition = Vector3.zero;
                }
                else
                {
                    spriteRenderer.sprite = releasedSprite;
                    spriteRenderer.color  = pressColor;
                    //spriteRenderer.color = color;
                    labelContainer.localPosition = labelOffset;
                }
            }
        }
示例#2
0
        // Update is called once per frame
        void Update()
        {
            //if we have a touch, make sure it's still there
            if (claimedTouch != -1)
            {
                bool stillExists = false;
                if (SinputTouchManager.TouchExists(claimedTouch))
                {
                    stillExists = true;
                }

                /*for (int i = 0; i < Input.touchCount; i++) {
                 *      if (Input.touches[i].fingerId == claimedTouch) stillExists = true;
                 * }*/
                if (debugMouse && claimedTouch == -2 && Input.GetKey("mouse 0"))
                {
                    stillExists = true;
                }
                if (!stillExists)
                {
                    claimedTouch = -1;
                }
            }

            //lets find a touch press if we don't have a touch yet
            if (claimedTouch == -1)
            {
                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (claimedTouch == -1 && Input.touches[i].phase == TouchPhase.Began && !SinputTouchManager.IsClaimed(Input.touches[i].fingerId))
                    {
                        //this touch press is free, but does it touch the stick?

                        Vector3 hitPoint = Vector3.zero;
                        if (TouchCollision(Input.touches[i].position, out hitPoint, !followTouch))
                        {
                            if (followTouch)
                            {
                                transform.position = hitPoint;
                            }
                            claimedTouch = Input.touches[i].fingerId;
                            SinputTouchManager.ClaimTouch(claimedTouch);
                            touchOrigin = transform.InverseTransformPoint(hitPoint);
                        }
                    }
                }

                if (debugMouse && claimedTouch == -1 && Input.GetKeyDown("mouse 0"))
                {
                    Vector3 hitPoint = Vector3.zero;
                    if (TouchCollision(Input.mousePosition, out hitPoint, !followTouch))
                    {
                        if (followTouch)
                        {
                            transform.position = hitPoint;
                        }
                        claimedTouch = -2;
                        SinputTouchManager.ClaimTouch(claimedTouch);
                        touchOrigin = transform.InverseTransformPoint(hitPoint);
                    }
                }
            }



            if (claimedTouch != -1)
            {
                //still touching, work out distance from the origin (in local space) and that's our stick value
                Vector3 touchPosition = touchOrigin;

                for (int i = 0; i < Input.touchCount; i++)
                {
                    if (claimedTouch == Input.touches[i].fingerId)
                    {
                        //this is our touch
                        Vector3 hitPoint = Vector3.zero;
                        if (TouchCollision(Input.touches[i].position, out hitPoint, false))
                        {
                            touchPosition = hitPoint;
                        }
                    }
                }
                if (debugMouse && claimedTouch == -2)
                {
                    Vector3 hitPoint = Vector3.zero;
                    if (TouchCollision(Input.mousePosition, out hitPoint, false))
                    {
                        touchPosition = hitPoint;
                    }
                }

                stickInputVector  = transform.InverseTransformPoint(touchPosition) - touchOrigin;
                stickInputVector *= rangeFactor;

                if (stickInputVector.magnitude > 1f)
                {
                    stickInputVector.Normalize();

                    if (followTouch)
                    {
                        transform.position = touchPosition - transform.right * (stickInputVector.x * stickRange) - transform.up * (stickInputVector.y * stickRange);
                        touchOrigin        = Vector3.zero;
                    }
                }
            }
            else
            {
                stickInputVector = Vector3.zero;
            }

            //set virtual input values now
            if (virtualInputID_UP != "")
            {
                VirtualInputs.SetVirtualAxis(virtualInputID_UP, Mathf.Clamp(stickInputVector.y, 0f, 1f));
            }
            if (virtualInputID_DOWN != "")
            {
                VirtualInputs.SetVirtualAxis(virtualInputID_DOWN, Mathf.Clamp(stickInputVector.y * -1f, 0f, 1f));
            }
            if (virtualInputID_RIGHT != "")
            {
                VirtualInputs.SetVirtualAxis(virtualInputID_RIGHT, Mathf.Clamp(stickInputVector.x, 0f, 1f));
            }
            if (virtualInputID_LEFT != "")
            {
                VirtualInputs.SetVirtualAxis(virtualInputID_LEFT, Mathf.Clamp(stickInputVector.x * -1f, 0f, 1f));
            }

            //animate stick
            stickTopTransform.localPosition = stickInputVector * stickRange;
            if (claimedTouch == -1)
            {
                fade -= Time.deltaTime;
                if (followTouch)
                {
                    fade = Mathf.Clamp(fade, 0.05f, 1f);
                }
                else
                {
                    fade = Mathf.Clamp(fade, 0.5f, 1f);
                }
            }
            else
            {
                fade += Time.deltaTime;
                fade  = Mathf.Clamp(fade, 0f, 1f);
            }
            Color col = stickBase.color;

            col.a           = fade;
            stickBase.color = col;

            col            = stickTop.color;
            col.a          = fade;
            stickTop.color = col;
        }