using UnityEngine;

/**

 * File: MPJoystick.cs

 * Author: Chris Danielson of (monkeyprism.com)

 * 

// USED TO BE: Joystick.js taken from Penelope iPhone Tutorial

//

// Joystick creates a movable joystick (via GUITexture) that 

// handles touch input, taps, and phases. Dead zones can control

// where the joystick input gets picked up and can be normalized.

//

// Optionally, you can enable the touchPad property from the editor

// to treat this Joystick as a TouchPad. A TouchPad allows the finger

// to touch down at any point and it tracks the movement relatively 

// without moving the graphic

*/
[RequireComponent(typeof(GUITexture))]
public class MPJoystick : MonoBehaviour
{ class Boundary
{ public Vector2 min = Vector2.zero; public Vector2 max = Vector2.zero; }
private static MPJoystick[] joysticks; // A static collection of all joysticks private static bool enumeratedJoysticks = false; private static float tapTimeDelta = 0.3f; // Time allowed between taps
public bool touchPad; public Vector2 position = Vector2.zero; public Rect touchZone; public Vector2 deadZone = Vector2.zero; // Control when position is output public bool normalize = false; // Normalize output after the dead-zone? public int tapCount; private int lastFingerId = -; // Finger last used for this joystick private float tapTimeWindow; // How much time there is left for a tap to occur private Vector2 fingerDownPos; //private float fingerDownTime; //private float firstDeltaTime = 0.5f;
private GUITexture gui; private Rect defaultRect; // Default position / extents of the joystick graphic private Boundary guiBoundary = new Boundary(); // Boundary for joystick graphic private Vector2 guiTouchOffset; // Offset to apply to touch input [HideInInspector]
public Vector2 guiCenter; // Center of joystick
void Start()
{ gui = (GUITexture)GetComponent(typeof(GUITexture));
defaultRect = gui.pixelInset; defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // - Screen.width * 0.5; defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5;
transform.position = Vector3.zero; if (touchPad)
{ // If a texture has been assigned, then use the rect ferom the gui as our touchZone if (gui.texture) touchZone = defaultRect; }
else
{ guiTouchOffset.x = defaultRect.width * 0.5f; guiTouchOffset.y = defaultRect.height * 0.5f;
// Cache the center of the GUI, since it doesn't change guiCenter.x = defaultRect.x + guiTouchOffset.x; guiCenter.y = defaultRect.y + guiTouchOffset.y;
// Let's build the GUI boundary, so we can clamp joystick movement guiBoundary.min.x = defaultRect.x - guiTouchOffset.x; guiBoundary.max.x = defaultRect.x + guiTouchOffset.x; guiBoundary.min.y = defaultRect.y - guiTouchOffset.y; guiBoundary.max.y = defaultRect.y + guiTouchOffset.y; } }
public Vector2 getGUICenter()
{ return guiCenter; }
void Disable()
{ gameObject.active = false; //enumeratedJoysticks = false; }
private void ResetJoystick()
{ gui.pixelInset = defaultRect; lastFingerId = -; position = Vector2.zero; fingerDownPos = Vector2.zero; }
private bool IsFingerDown()
{ return (lastFingerId != -); }
public void LatchedFinger(int fingerId)
{ // If another joystick has latched this finger, then we must release it if (lastFingerId == fingerId) ResetJoystick(); }
void Update()
{ if (!enumeratedJoysticks)
{ // Collect all joysticks in the game, so we can relay finger latching messages joysticks = (MPJoystick[])FindObjectsOfType(typeof(MPJoystick)); enumeratedJoysticks = true; }
int count = Input.touchCount;
if (tapTimeWindow > )
tapTimeWindow -= Time.deltaTime;
else
tapCount = ; if (count == )
{
ResetJoystick();
}
else
{
for (int i = ; i < count; i++)
{
Touch touch = Input.GetTouch(i); Vector2 guiTouchPos = touch.position - guiTouchOffset;
bool shouldLatchFinger = false; if (touchPad)
{ if (touchZone.Contains(touch.position)) shouldLatchFinger = true; } else if (gui.HitTest(touch.position))
{ shouldLatchFinger = true; }
// Latch the finger if this is a new touch if (shouldLatchFinger && (lastFingerId == - || lastFingerId != touch.fingerId))
{
if (touchPad)
{ //gui.color.a = 0.15; lastFingerId = touch.fingerId; //fingerDownPos = touch.position; //fingerDownTime = Time.time; }
lastFingerId = touch.fingerId; // Accumulate taps if it is within the time window if (tapTimeWindow > ) tapCount++; else
{ tapCount = ; tapTimeWindow = tapTimeDelta; }
// Tell other joysticks we've latched this finger //for ( j : Joystick in joysticks ) foreach (MPJoystick j in joysticks)
{ if (j != this) j.LatchedFinger(touch.fingerId); } }
if (lastFingerId == touch.fingerId)
{ // Override the tap count with what the iPhone SDK reports if it is greater // This is a workaround, since the iPhone SDK does not currently track taps // for multiple touches if (touch.tapCount > tapCount) tapCount = touch.tapCount;
if (touchPad)
{ // For a touchpad, let's just set the position directly based on distance from initial touchdown position.x = Mathf.Clamp((touch.position.x - fingerDownPos.x) / (touchZone.width / ), -, ); position.y = Mathf.Clamp((touch.position.y - fingerDownPos.y) / (touchZone.height / ), -, ); }
else
{ // Change the location of the joystick graphic to match where the touch is Rect r = gui.pixelInset; r.x = Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x); r.y = Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y); gui.pixelInset = r; }
if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled) ResetJoystick(); } } }
if (!touchPad)
{ // Get a value between -1 and 1 based on the joystick graphic location position.x = (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x) / guiTouchOffset.x; position.y = (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y) / guiTouchOffset.y; }
// Adjust for dead zone var absoluteX = Mathf.Abs(position.x); var absoluteY = Mathf.Abs(position.y); if (absoluteX < deadZone.x)
{ // Report the joystick as being at the center if it is within the dead zone position.x = ; } else if (normalize)
{ // Rescale the output after taking the dead zone into account position.x = Mathf.Sign(position.x) * (absoluteX - deadZone.x) / ( - deadZone.x); }
if (absoluteY < deadZone.y)
{ // Report the joystick as being at the center if it is within the dead zone position.y = ; } else if (normalize)
{ // Rescale the output after taking the dead zone into account position.y = Mathf.Sign(position.y) * (absoluteY - deadZone.y) / ( - deadZone.y); }
}
}

MPJoystick的更多相关文章

随机推荐

  1. Android 曲线动画animation,类似加入购物车动画

    按照惯例先放效果图:图中小球做抛物线运动 资源图片 1.首先布局文件activity_main.xml,布局很简单,就一个测试按钮 <RelativeLayout xmlns:android=& ...

  2. Java中的new关键字和引用传参

    先看看Java中如何使用new关键字创建一个对象的. [java] view plain copy public class Student { public String name; public  ...

  3. Python 进阶 之 函数对象

    Python的世界里,万物皆对象,函数当然也是: 首先要定义一个函数: def add(a,b): print a+b 其次定义一个字典来引用该函数: dic = {"add":a ...

  4. WN7下安装office2013编辑文档反应这么慢?

    把office在高级选项里面“禁用硬件加速”给打勾就OK了. [office 2013密钥] 9MBNG-4VQ2Q-WQ2VF-X9MV2-MPXKV F2V7V-8WN76-77BPV-MKY36 ...

  5. debian 更换sh的默认链接为bash

    https://blog.csdn.net/mudongliangabcd/article/details/43458895

  6. windows下tortoiseGit安装和使用

    一.安装git for windows 首先下载git for windows客户端http://msysgit.github.io/ 安装过程没什么特别的,不停next就ok了     图太多就不继 ...

  7. Nodejs获取Azure Active Directory AccessToken

    因为现有的代码已经迁入至Azure中,并且受AD保护,所以在获取数据时,需要传入Token后才可以获取到数据,那么第一步肯定是需要先提取到token var adal = require('adal- ...

  8. CodeForces 669C

    链接:http://codeforces.com/problemset/problem/669/C http://www.cnblogs.com/Ash-ly/p/5443155.html 题意: 给 ...

  9. 模板—数学—Lucas

    模板—数学—Lucas Code: #include <cstdio> #include <algorithm> using namespace std; #define N ...

  10. Codeforces 1059E. Split the Tree

    题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...