Exemplo n.º 1
0
    public override bool GetButton (string name, CrossPlatformInput.ButtonAction action) {

        bool containsName = virtualButtons.ContainsKey (name);
        if(containsName)
		{
			switch (action)
			{
				// virtual buttons are activated by touch or mouse click
			    case CrossPlatformInput.ButtonAction.GetButton:
			        return  virtualButtons[name].GetButton;
			    case CrossPlatformInput.ButtonAction.GetButtonDown:
					return  virtualButtons[name].GetButtonDown;
			    case CrossPlatformInput.ButtonAction.GetButtonUp:
					return virtualButtons[name].GetButtonUp;
			}
		} else {
			// no virtual button with this name, check "real" (input manager) buttons:
			switch (action)
			{
			case CrossPlatformInput.ButtonAction.GetButton:
				return Input.GetButton(name);
			case CrossPlatformInput.ButtonAction.GetButtonDown:
				return  Input.GetButtonDown(name);
			case CrossPlatformInput.ButtonAction.GetButtonUp:
				return Input.GetButtonUp(name);
			}
		}
		return false;
    }
Exemplo n.º 2
0
 public override bool GetButton (string name, CrossPlatformInput.ButtonAction action) {
     switch (action) {
         case CrossPlatformInput.ButtonAction.GetButton:
             return alwaysUseVirtual.Contains (name) ? virtualButtons[name].GetButton : Input.GetButton (name);
         case CrossPlatformInput.ButtonAction.GetButtonDown:
             return alwaysUseVirtual.Contains (name) ? virtualButtons[name].GetButtonDown : Input.GetButtonDown (name);
         case CrossPlatformInput.ButtonAction.GetButtonUp:
             return alwaysUseVirtual.Contains (name) ? virtualButtons[name].GetButtonUp : Input.GetButtonUp (name);
         default:
             throw new Exception("Invalid button action.");
     }
 }
Exemplo n.º 3
0
 public override bool GetButton(string name, CrossPlatformInput.ButtonAction action)
 {
     if (!this.virtualButtons.ContainsKey(name))
     {
         throw new Exception(" Button " + name + " does not exist");
     }
     switch (action)
     {
     case CrossPlatformInput.ButtonAction.GetButtonDown:
         return this.virtualButtons[name].GetButtonDown;
     case CrossPlatformInput.ButtonAction.GetButtonUp:
         return this.virtualButtons[name].GetButtonUp;
     case CrossPlatformInput.ButtonAction.GetButton:
         return this.virtualButtons[name].GetButton;
     default:
         throw new Exception("Invalid button action.");
     }
 }
Exemplo n.º 4
0
    public void RegisterVirtualAxis(CrossPlatformInput.VirtualAxis axis)
    {
        // check if we already have an axis with that name and log and error if we do
        if (virtualAxes.ContainsKey(axis.name))
        {
            //Debug.LogError("There is already a virtual axis named " + axis.name + " registered.");
        }
        else
        {

            // add any new axes
            virtualAxes.Add(axis.name, axis);

            // if we dont want to match with the input manager setting then revert to always using virtual
            if (!axis.matchWithInputManager)
            {
                alwaysUseVirtual.Add(axis.name);
            }
        }
    }
Exemplo n.º 5
0
    public void RegisterVirtualButton(CrossPlatformInput.VirtualButton button)
    {
        // check if already have a buttin with that name and log an error if we do
        if (virtualButtons.ContainsKey(button.name))
        {
            //Debug.LogError("There is already a virtual button named " + button.name + " registered.");
        }
        else
        {

            // add any new buttons
            virtualButtons.Add(button.name, button);

            // if we dont want to match to the input manager then always use a virtual axis
            if (!button.matchWithInputManager)
            {
                alwaysUseVirtual.Add(button.name);
            }

        }
    }
Exemplo n.º 6
0
 private static void RegisterVirtualAxis(CrossPlatformInput.VirtualAxis axis)
 {
     CrossPlatformInput.virtualInput.RegisterVirtualAxis(axis);
 }
Exemplo n.º 7
0
 public VirtualButton(string name, bool matchToInputSettings)
 {
     this.name = name;
     this.matchWithInputManager = matchToInputSettings;
     CrossPlatformInput.RegisterVirtualButton(this);
 }
Exemplo n.º 8
0
 public void Remove()
 {
     CrossPlatformInput.UnRegisterVirtualAxis(this.name);
 }
Exemplo n.º 9
0
    public void FixedUpdate()
    {
        float speed = runSpeed;

        // Read input
#if CROSS_PLATFORM_INPUT
        float h    = CrossPlatformInput.GetAxis("Horizontal");
        float v    = CrossPlatformInput.GetAxis("Vertical");
        bool  jump = CrossPlatformInput.GetButton("Jump");
#else
        float h    = Input.GetAxis("Horizontal");
        float v    = Input.GetAxis("Vertical");
        bool  jump = Input.GetButton("Jump");
#endif

#if !MOBILE_INPUT
        // On standalone builds, walk/run speed is modified by a key press.
        // We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
        bool walkOrRun = Input.GetKey(KeyCode.LeftShift);
        speed = walkByDefault ? (walkOrRun ? runSpeed : walkSpeed) : (walkOrRun ? walkSpeed : runSpeed);

        // On mobile, it's controlled in analogue fashion by the v input value, and therefore needs no special handling.
#endif

        input = new Vector2(h, v);

        // normalize input if it exceeds 1 in combined length:
        if (input.sqrMagnitude > 1)
        {
            input.Normalize();
        }

        // Get a vector which is desired move as a world-relative direction, including speeds
        Vector3 desiredMove = transform.forward * input.y * speed + transform.right * input.x * strafeSpeed;

        // preserving current y velocity (for falling, gravity)
        float yv = rigidbody.velocity.y;

        // add jump power
        if (grounded && jump)
        {
            yv      += jumpPower;
            grounded = false;
        }

        // Set the rigidbody's velocity according to the ground angle and desired move
        rigidbody.velocity = desiredMove + Vector3.up * yv;

        // Use low/high friction depending on whether we're moving or not
        if (desiredMove.magnitude > 0 || !grounded)
        {
            collider.material = advanced.zeroFrictionMaterial;
        }
        else
        {
            collider.material = advanced.highFrictionMaterial;
        }


        // Ground Check:

        // Create a ray that points down from the centre of the character.
        Ray ray = new Ray(transform.position, -transform.up);

        // Raycast slightly further than the capsule (as determined by jumpRayLength)
        RaycastHit[] hits = Physics.RaycastAll(ray, capsule.height * jumpRayLength);
        System.Array.Sort(hits, rayHitComparer);


        if (grounded || rigidbody.velocity.y < jumpPower * .5f)
        {
            // Default value if nothing is detected:
            grounded = false;
            // Check every collider hit by the ray
            for (int i = 0; i < hits.Length; i++)
            {
                // Check it's not a trigger
                if (!hits[i].collider.isTrigger)
                {
                    // The character is grounded, and we store the ground angle (calculated from the normal)
                    grounded = true;

                    // stick to surface - helps character stick to ground - specially when running down slopes
                    //if (rigidbody.velocity.y <= 0) {
                    rigidbody.position = Vector3.MoveTowards(rigidbody.position, hits[i].point + Vector3.up * capsule.height * .5f, Time.deltaTime * advanced.groundStickyEffect);
                    //}
                    rigidbody.velocity = new Vector3(rigidbody.velocity.x, 0, rigidbody.velocity.z);
                    break;
                }
            }
        }

        Debug.DrawRay(ray.origin, ray.direction * capsule.height * jumpRayLength, grounded ? Color.green : Color.red);


        // add extra gravity
        rigidbody.AddForce(Physics.gravity * (advanced.gravityMultiplier - 1));
    }
Exemplo n.º 10
0
 public abstract bool GetButton(string name, CrossPlatformInput.ButtonAction action);
Exemplo n.º 11
0
 private void Update()
 {
     this.move = new Vector3(CrossPlatformInput.GetAxis("Horizontal"), 0f, CrossPlatformInput.GetAxis("Vertical"));
     this.jump = CrossPlatformInput.GetButton("Jump");
 }
Exemplo n.º 12
0
 private static void AddAxes(string name)
 {
     // we have not registered this button yet so add it, happens in the constructor
     CrossPlatformInput.RegisterVirtualAxis(new CrossPlatformInput.VirtualAxis(name));
 }
Exemplo n.º 13
0
    private void Update()
    {
        this.demoCam.localPosition = Vector3.SmoothDamp(this.demoCam.localPosition, Vector3.forward * (float)(-(float)ParticleSceneControls.selected.camOffset), ref this.camOffsetVelocity, 1f);
        if (CrossPlatformInput.GetButtonDown("NextParticleSystem"))
        {
            ParticleSceneControls.selectedIndex++;
            if (ParticleSceneControls.selectedIndex == this.demoParticles.items.Length)
            {
                ParticleSceneControls.selectedIndex = 0;
            }
            this.Select(ParticleSceneControls.selectedIndex);
            return;
        }
        if (CrossPlatformInput.GetButtonDown("PreviousParticleSystem"))
        {
            ParticleSceneControls.selectedIndex--;
            if (ParticleSceneControls.selectedIndex == -1)
            {
                ParticleSceneControls.selectedIndex = this.demoParticles.items.Length - 1;
            }
            this.Select(ParticleSceneControls.selectedIndex);
            return;
        }
        if (ParticleSceneControls.selected.mode == ParticleSceneControls.Mode.Activate)
        {
            return;
        }
        bool flag  = Input.GetMouseButtonDown(0) && ParticleSceneControls.selected.mode == ParticleSceneControls.Mode.Instantiate;
        bool flag2 = Input.GetMouseButton(0) && ParticleSceneControls.selected.mode == ParticleSceneControls.Mode.Trail;

        if (flag || flag2)
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit raycastHit;
            if (Physics.Raycast(ray, out raycastHit))
            {
                Quaternion rotation = Quaternion.LookRotation(raycastHit.normal);
                if (ParticleSceneControls.selected.align == ParticleSceneControls.AlignMode.Up)
                {
                    rotation = Quaternion.identity;
                }
                Vector3 vector = raycastHit.point + raycastHit.normal * this.distFromSurface;
                if ((vector - this.lastPos).magnitude > ParticleSceneControls.selected.minDist)
                {
                    if (ParticleSceneControls.selected.mode != ParticleSceneControls.Mode.Trail || this.instance == null)
                    {
                        this.instance = UnityEngine.Object.Instantiate <Transform>(ParticleSceneControls.selected.transform, vector, rotation);
                        if (this.particleMultiplier != null)
                        {
                            this.instance.GetComponent <ParticleSystemMultiplier>().multiplier = this.multiply;
                        }
                        this.currentParticleList.Add(this.instance);
                        if (ParticleSceneControls.selected.maxCount > 0 && this.currentParticleList.Count > ParticleSceneControls.selected.maxCount)
                        {
                            if (this.currentParticleList[0] != null)
                            {
                                UnityEngine.Object.Destroy(this.currentParticleList[0].gameObject);
                            }
                            this.currentParticleList.RemoveAt(0);
                        }
                    }
                    else
                    {
                        this.instance.position = vector;
                        this.instance.rotation = rotation;
                    }
                    if (ParticleSceneControls.selected.mode == ParticleSceneControls.Mode.Trail)
                    {
                        this.instance.transform.GetComponent <ParticleSystem>().enableEmission = false;
                        this.instance.transform.GetComponent <ParticleSystem>().Emit(1);
                    }
                    this.instance.parent = raycastHit.transform;
                    this.lastPos         = vector;
                }
            }
        }
    }
Exemplo n.º 14
0
 public static bool GetButtonUp(string name)
 {
     return(CrossPlatformInput.GetButton(name, CrossPlatformInput.ButtonAction.GetButtonUp));
 }
Exemplo n.º 15
0
 public static float GetAxisRaw(string name)
 {
     return(CrossPlatformInput.GetAxis(name, true));
 }
Exemplo n.º 16
0
 private void OnEnable()
 {
     this.axis = (CrossPlatformInput.VirtualAxisReference(this.axisName) ?? new CrossPlatformInput.VirtualAxis(this.axisName));
     this.rect = base.GetComponent <GUIElement>().GetScreenRect();
     this.FindPairedButton();
 }
Exemplo n.º 17
0
 private static void RegisterVirtualButton(CrossPlatformInput.VirtualButton button)
 {
     CrossPlatformInput.virtualInput.RegisterVirtualButton(button);
 }
Exemplo n.º 18
0
 private static bool GetButton(string name, CrossPlatformInput.ButtonAction action)
 {
     return CrossPlatformInput.virtualInput.GetButton(name, action);
 }
Exemplo n.º 19
0
    // Update is called once per frame
    void Update()
    {
        // we make initial calculations from the original local rotation
        transform.localRotation = originalRotation;

        // read input from mouse or mobile controls
        float inputH = 0;
        float inputV = 0;

        if (relative)
        {
            inputH = CrossPlatformInput.GetAxis("Mouse X");
            inputV = CrossPlatformInput.GetAxis("Mouse Y");

            // wrap values to avoid springing quickly the wrong way from positive to negative
            if (targetAngles.y > 180)
            {
                targetAngles.y -= 360; followAngles.y -= 360;
            }
            if (targetAngles.x > 180)
            {
                targetAngles.x -= 360; followAngles.x -= 360;
            }
            if (targetAngles.y < -180)
            {
                targetAngles.y += 360; followAngles.y += 360;
            }
            if (targetAngles.x < -180)
            {
                targetAngles.x += 360; followAngles.x += 360;
            }

                        #if UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8
            // on mobile, sometimes we want input mapped directly to tilt value,
            // so it springs back automatically when the look input is released.
            if (autoZeroHorizontalOnMobile)
            {
                targetAngles.y = Mathf.Lerp(-rotationRange.y * 0.5f, rotationRange.y * 0.5f, inputH * .5f + .5f);
            }
            else
            {
                targetAngles.y += inputH * rotationSpeed;
            }
            if (autoZeroVerticalOnMobile)
            {
                targetAngles.x = Mathf.Lerp(-rotationRange.x * 0.5f, rotationRange.x * 0.5f, inputV * .5f + .5f);
            }
            else
            {
                targetAngles.x += inputV * rotationSpeed;
            }
                        #else
            // with mouse input, we have direct control with no springback required.
            targetAngles.y += inputH * rotationSpeed;
            targetAngles.x += inputV * rotationSpeed;
                        #endif

            // clamp values to allowed range
            targetAngles.y = Mathf.Clamp(targetAngles.y, -rotationRange.y * 0.5f, rotationRange.y * 0.5f);
            targetAngles.x = Mathf.Clamp(targetAngles.x, -rotationRange.x * 0.5f, rotationRange.x * 0.5f);
        }
        else
        {
            inputH = Input.mousePosition.x;
            inputV = Input.mousePosition.y;

            // set values to allowed range
            targetAngles.y = Mathf.Lerp(-rotationRange.y * 0.5f, rotationRange.y * 0.5f, inputH / Screen.width);
            targetAngles.x = Mathf.Lerp(-rotationRange.x * 0.5f, rotationRange.x * 0.5f, inputV / Screen.height);
        }



        // smoothly interpolate current values to target angles
        followAngles = Vector3.SmoothDamp(followAngles, targetAngles, ref followVelocity, dampingTime);

        // update the actual gameobject's rotation
        transform.localRotation = originalRotation * Quaternion.Euler(-followAngles.x, followAngles.y, 0);
    }
    void Update()
    {
        demoCam.localPosition = Vector3.SmoothDamp(demoCam.localPosition, Vector3.forward * -selected.camOffset, ref camOffsetVelocity, 1);

        if (CrossPlatformInput.GetButtonDown("NextParticleSystem"))
        {
            selectedIndex++;
            if (selectedIndex == demoParticles.items.Length)
            {
                selectedIndex = 0;
            }
            Select(selectedIndex);
            return;
        }
        if (CrossPlatformInput.GetButtonDown("PreviousParticleSystem"))
        {
            selectedIndex--;
            if (selectedIndex == -1)
            {
                selectedIndex = demoParticles.items.Length - 1;
            }
            Select(selectedIndex);
            return;
        }

        if (selected.mode == Mode.Activate)
        {
            // this is for a particle system that just needs activating, and needs no interaction (eg, duststorm)
            return;
        }


        bool oneShotClick = (Input.GetMouseButtonDown(0) && selected.mode == Mode.Instantiate);
        bool repeat       = (Input.GetMouseButton(0) && selected.mode == Mode.Trail);

        if (oneShotClick || repeat)
        {
            Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                var rot = Quaternion.LookRotation(hit.normal);

                if (selected.align == AlignMode.Up)
                {
                    rot = Quaternion.identity;
                }

                var pos = hit.point + hit.normal * distFromSurface;

                if ((pos - lastPos).magnitude > selected.minDist)
                {
                    if (selected.mode != Mode.Trail || instance == null)
                    {
                        instance = (Transform)Instantiate(selected.transform, pos, rot);

                        if (particleMultiplier != null)
                        {
                            instance.GetComponent <ParticleSystemMultiplier>().multiplier = multiply;
                        }

                        currentParticleList.Add(instance);

                        if (selected.maxCount > 0 && currentParticleList.Count > selected.maxCount)
                        {
                            if (currentParticleList[0] != null)
                            {
                                Destroy(currentParticleList[0].gameObject);
                            }
                            currentParticleList.RemoveAt(0);
                        }
                    }
                    else
                    {
                        instance.position = pos;
                        instance.rotation = rot;
                    }

                    if (selected.mode == Mode.Trail)
                    {
                        instance.transform.GetComponent <ParticleSystem>().enableEmission = false;
                        instance.transform.GetComponent <ParticleSystem>().Emit(1);
                    }

                    instance.parent = hit.transform;
                    lastPos         = pos;
                }
            }
        }
    }
Exemplo n.º 21
0
    public void FixedUpdate()
    {
        // Read input
        float h    = CrossPlatformInput.GetAxis("Horizontal");
        float v    = CrossPlatformInput.GetAxis("Vertical");
        bool  jump = CrossPlatformInput.GetButton("Jump");

        input = new Vector2(h, v);

        float speed = runSpeed;

                #if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_WP8)
        // On standalone builds, walk/run speed is modified by a key press.
        // We select appropriate speed based on whether we're walking by default, and whether the walk/run toggle button is pressed:
        bool walkOrRun = Input.GetKey(KeyCode.LeftShift);
        speed = walkByDefault ? (walkOrRun ? runSpeed : walkSpeed) : (walkOrRun ? walkSpeed : runSpeed);
        #endif

        // On mobile, it's controlled in analogue fashion by the v input value, and therefore needs no special handling.



        // Ground Check:

        // Create a ray that points down from the centre of the character.
        Ray ray = new Ray(transform.position, -transform.up);

        // Raycast slightly further than the capsule (as determined by jumpRayLength)
        RaycastHit[] hits = Physics.RaycastAll(ray, capsule.height * jumpRayLength);


        float nearest = Mathf.Infinity;

        if (grounded || rigidbody.velocity.y < 0.1f)
        {
            // Default value if nothing is detected:
            grounded = false;

            // Check every collider hit by the ray
            for (int i = 0; i < hits.Length; i++)
            {
                // Check it's not a trigger
                if (!hits[i].collider.isTrigger && hits[i].distance < nearest)
                {
                    // The character is grounded, and we store the ground angle (calculated from the normal)
                    grounded = true;
                    nearest  = hits[i].distance;
                    //Debug.DrawRay(transform.position, groundAngle * transform.forward, Color.green);
                }
            }
        }

        Debug.DrawRay(ray.origin, ray.direction * capsule.height * jumpRayLength, grounded ? Color.green : Color.red);



        // normalize input if it exceeds 1 in combined length:
        if (input.sqrMagnitude > 1)
        {
            input.Normalize();
        }

        // Get a vector which is desired move as a world-relative direction, including speeds
        Vector3 desiredMove = transform.forward * input.y * speed + transform.right * input.x * strafeSpeed;

        // preserving current y velocity (for falling, gravity)
        float yv = rigidbody.velocity.y;

        // add jump power
        if (grounded && jump)
        {
            yv      += jumpPower;
            grounded = false;
        }

        // Set the rigidbody's velocity according to the ground angle and desired move
        rigidbody.velocity = desiredMove + Vector3.up * yv;

        // Use low/high friction depending on whether we're moving or not
        if (desiredMove.magnitude > 0 || !grounded)
        {
            collider.material = advanced.zeroFrictionMaterial;
        }
        else
        {
            collider.material = advanced.highFrictionMaterial;
        }

        // add extra gravity
        rigidbody.AddForce(Physics.gravity * (advanced.gravityMultiplier - 1));
    }
Exemplo n.º 22
0
    void Update()
    {
        // Read the jump input in Update so button presses aren't missed.
#if CROSS_PLATFORM_INPUT
        if (CrossPlatformInput.GetButtonDown("Jump"))
        {
            jump = true;
        }
#else
//		touch = t1.senseTouch();
//		if(touch == "SwipeUp")
//			jump = true;
//		if (touch == "SwipeDown") c = true;
        if (Input.GetKey(KeyCode.X))
        {
            c = true;
        }
        if (Input.GetKey(KeyCode.Space))
        {
            jump = true;
        }

        /////////////////////////////////////////////////////////////////////////////////////////////

        /////////////////////////Touch Begin////////////////////////////////
        if (Input.touchCount == 1)
        {
            if (Input.GetTouch(0).position.x < Screen.width / 2)
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    v2previous = Input.GetTouch(0).position;
                }
                /////////////////////////Touch Stationary////////////////////////////////
                if (Input.GetTouch(0).phase == TouchPhase.Stationary)
                {
                    v2current   = Input.GetTouch(0).position;
                    fTouchDelta = v2current.magnitude - v2previous.magnitude;

                    if (Mathf.Abs(fTouchDelta) < minSwipeLenght)
                    {
                        if (Input.GetTouch(0).tapCount == 1)
                        {
                            jump = true;
                        }
                    }
                }
                /////////////////////////Touch End////////////////////////////////
                if (Input.GetTouch(0).phase == TouchPhase.Ended)
                {
                    jump = false;
                }
            }
            else
            {
                if (Input.GetTouch(0).phase == TouchPhase.Began)
                {
                    v2previous = Input.GetTouch(0).position;
                }
                /////////////////////////Touch Move////////////////////////////////
                if (Input.GetTouch(0).phase == TouchPhase.Moved)
                {
                    v2current   = Input.GetTouch(0).position;
                    fTouchDelta = v2current.magnitude - v2previous.magnitude;

                    if (Mathf.Abs(fTouchDelta) > minSwipeLenght)
                    {
                        if (fTouchDelta > 0)
                        {
                            // SwipeUp
                        }
                        else
                        {
                            c = true;
                        }
                    }
                }
            }
        }

        //////////////////////////////////////////////////////////////////////////////////////////////
#endif
    }
Exemplo n.º 23
0
 void Update()
 {
     // Get the axis and jump input.
     move = new Vector3(CrossPlatformInput.GetAxis("Horizontal"), 0f, CrossPlatformInput.GetAxis("Vertical"));
     jump = CrossPlatformInput.GetButton("Jump");
 }