原始的:

using UnityEngine;
using System.Collections; public class NGUIJoystick : MonoBehaviour
{
public float radius = 2.0f;
public Vector3 scale = Vector3.one;
private Plane mPlane;
private Vector3 mLastPos;
private UIPanel mPanel;
private Vector3 center;
[HideInInspector]
public Vector2 position; private void Start ()
{
center = transform.localPosition;
} /// <summary>
/// Create a plane on which we will be performing the dragging.
/// </summary> private void OnPress (bool pressed)
{
if (enabled && gameObject.activeInHierarchy) {
if (pressed) {
mLastPos = UICamera.lastHit.point;
mPlane = new Plane (Vector3.back, mLastPos);
} else {
transform.localPosition = center;
position=Vector2.zero;
}
}
} /// <summary>
/// Drag the object along the plane.
/// </summary> void OnDrag (Vector2 delta)
{
if (enabled && gameObject.activeInHierarchy) {
UICamera.currentTouch.clickNotification = UICamera.ClickNotification.BasedOnDelta; Ray ray = UICamera.currentCamera.ScreenPointToRay (UICamera.currentTouch.pos);
float dist = 0f; if (mPlane.Raycast (ray, out dist)) {
Vector3 currentPos = ray.GetPoint (dist);
Vector3 offset = currentPos - mLastPos;
mLastPos = currentPos; if (offset.x != 0f || offset.y != 0f) {
offset = transform.InverseTransformDirection (offset);
offset.Scale (scale);
offset = transform.TransformDirection (offset);
} offset.z = ;
transform.position += offset; float length = transform.localPosition.magnitude; if (length > radius) {
transform.localPosition = Vector3.ClampMagnitude (transform.localPosition, radius);
} position = new Vector2((transform.localPosition.x-center.x)/radius,(transform.localPosition.y-center.y)/radius);
}
}
} }

超凡特工的:

using System;
using UnityEngine; public class NGUIJoystick : MonoBehaviour
{
public bool activeFlag;
public Vector3 center;
private float deadRadius;
public float deadRadiusRatio = 0.3f;
public bool lastPressed;
protected UIPanel mPanel;
protected Plane mPlane;
public Vector2 normalizedPosition;
public float normalizedRadius = 2f;
public Vector2 position;
public float radius = 2f;
public Vector3 worldCenter; internal void Move(Vector3 offset)
{
offset.z = 0f;
base.transform.position = this.worldCenter + offset;
float magnitude = base.transform.localPosition.magnitude;
if (magnitude < this.deadRadius)
{
this.normalizedPosition = Vector2.zero;
}
else
{
if (magnitude > this.radius)
{
base.transform.localPosition = Vector3.ClampMagnitude(base.transform.localPosition, this.radius);
}
float num2 = base.transform.localPosition.x - this.center.x;
float num3 = base.transform.localPosition.y - this.center.y;
this.position.x = num2 / this.radius;
this.position.y = num3 / this.radius;
this.normalizedPosition.x = num2 / this.normalizedRadius;
this.normalizedPosition.y = num3 / this.normalizedRadius;
this.normalizedPosition.x = Mathf.Clamp(this.normalizedPosition.x, -1f, 1f);
this.normalizedPosition.y = Mathf.Clamp(this.normalizedPosition.y, -1f, 1f);
}
} internal virtual void OnDrag(Vector2 delta)
{
if (base.enabled && base.gameObject.activeInHierarchy)
{
Ray ray = UICamera.currentCamera.ScreenPointToRay((Vector3) UICamera.currentTouch.pos);
float enter = 0f;
if (this.mPlane.Raycast(ray, out enter))
{
Vector3 offset = ray.GetPoint(enter) - this.worldCenter;
this.Move(offset);
}
}
} internal virtual void OnPress(bool pressed)
{
if (base.enabled && base.gameObject.activeInHierarchy)
{
if (pressed)
{
this.mPlane = new Plane(Vector3.back, UICamera.lastHit.point);
}
else
{
base.transform.localPosition = this.center;
this.position = Vector2.zero;
this.activeFlag = true;
}
this.lastPressed = pressed;
}
} public void SetRadius(float r, float nr)
{
this.radius = r;
this.normalizedRadius = nr;
this.deadRadius = this.radius * this.deadRadiusRatio;
} internal virtual void Start()
{
this.center = base.transform.localPosition;
this.worldCenter = base.transform.position;
}
}
namespace Kola
{
using LuaInterface;
using System;
using UnityEngine; public class KJoystick : NGUIJoystick
{
public UIRect bgObject;
public bool FollowPressPosition;
private Vector3 originPos;
private Transform parent;
public LuaTable playerControl;
public LuaFunction playerControlSetInputFunc;
public KMoveBehavior playerMove;
public float pressAlpha = 0.5f; private void _OnDrag(GameObject go, Vector2 delta)
{
this.OnDrag(delta);
} private void _OnPress(GameObject go, bool pressed)
{
this.OnPress(pressed);
if (pressed)
{
Vector3 point = UICamera.lastHit.point;
if (this.FollowPressPosition)
{
this.parent.position = point;
}
else
{
Vector3 position = this.parent.position;
Vector3 offset = point - position;
base.Move(offset);
this.OnDrag(Vector2.zero);
}
if (this.bgObject != null)
{
this.bgObject.alpha = this.pressAlpha;
}
}
else
{
if (this.playerMove != null)
{
this.playerMove.Stand();
}
if (this.bgObject != null)
{
this.bgObject.alpha = 1f;
}
if (this.playerControlSetInputFunc != null)
{
object[] args = new object[] { this.playerControl, 0f, 0f, true };
KLuaFunc.Call(this.playerControlSetInputFunc, args);
}
if (this.FollowPressPosition)
{
this.parent.position = this.originPos;
}
}
} public void Attach(GameObject draggedObj, GameObject bgObj)
{
UIEventListener listener1 = UIEventListener.Get(draggedObj);
listener1.onPress = (UIEventListener.BoolDelegate) Delegate.Combine(listener1.onPress, new UIEventListener.BoolDelegate(this._OnPress));
UIEventListener listener2 = UIEventListener.Get(draggedObj);
listener2.onDrag = (UIEventListener.VectorDelegate) Delegate.Combine(listener2.onDrag, new UIEventListener.VectorDelegate(this._OnDrag));
this.bgObject = (bgObj != null) ? bgObj.GetComponent<UIRect>() : null;
} private void OnDisable()
{
this._OnPress(base.gameObject, false);
} internal override void OnDrag(Vector2 delta)
{
base.OnDrag(delta);
if ((this.playerMove != null) && (base.enabled && base.gameObject.activeInHierarchy))
{
if (this.playerControlSetInputFunc != null)
{
object[] args = new object[] { this.playerControl, this.normalizedPosition.x, this.normalizedPosition.y, false };
KLuaFunc.Call(this.playerControlSetInputFunc, args);
}
else
{
this.playerMove.Move(this.normalizedPosition.x, this.normalizedPosition.y);
}
}
} public void OnDrawGizmos()
{
Gizmos.color = Color.green;
Vector3 to = base.transform.position + ((Vector3) (new Vector3(this.position.x, this.position.y, 0f) * 100f));
Gizmos.DrawLine(base.transform.position, to);
} public void SetPlayer(GameObject obj)
{
this.playerMove = obj.GetComponent<KMoveBehavior>();
} public void SetPlayerControl(LuaTable control)
{
this.playerControl = control;
this.playerControlSetInputFunc = KLuaBehavior.GetFunction(control, "SetJoyStickInput");
} internal override void Start()
{
base.Start();
this.parent = base.transform.parent;
this.originPos = this.parent.position;
}
}
}

NGUIJoysticK的更多相关文章

  1. 【转】NGUI版虚拟摇杆

    http://blog.csdn.net/anyuanlzh/article/details/40107577 下面是我用nui实现的一个虚拟摇杆. 1,示图 2.代码如下,都有比较详细的注释,就不说 ...

  2. NGUI版虚拟摇杆

    以下是我用nui实现的一个虚拟摇杆. 1,示图 2.代码例如以下,都有比較具体的凝视.就不说明了. using UnityEngine; using System.Collections; using ...

  3. Unity基于NGUI的简单并可直接使用的虚拟摇杆实现(一)

    可能大家都听说过大名鼎鼎的easytouch,然而easytouch是基于UGUI的,两种不同的UI混用,可能会造成项目管理的混乱,并且可能会出现各种幺蛾子,比如事件传递互相扰乱的问题. 于是就想找一 ...

随机推荐

  1. 从实用主义深入理解c++虚函数

    记得几个月前看过C++虚函数的问题,当时其实就看懂了,最近笔试中遇到了虚函数竟然不太确定,所以还是理解的不深刻,所以想通过这篇文章来巩固下. 装逼一刻: 最近,本人思想发生了巨大的转变,在大学的时候由 ...

  2. session放到mongo里边

    为了session的共享和保存,常常把session放数据库里边,但是很多时候都放redis里边,今天看了一个放Mongo中的记录下,说不懂什么时候就用到了. app.use(session({ se ...

  3. VS2010版快捷键

    VS2010版快捷键 Ctrl+E,D ----格式化全部代码 Ctrl+E,F ----格式化选中的代码 CTRL + SHIFT + B生成解决方案 CTRL + F7 生成编译 CTRL + O ...

  4. [工具类]文件或文件夹xx已存在,则重命名为xx(n)

    写在前面 最近在弄一个文件传输的一个东东,在接收文件的时候,如果文件已经存在,该如何处理?提示?删除?感觉直接删除实在不太合适,万一这个文件对用户来说很重要,你给他删除了肯定不行.然后就想到了,win ...

  5. 第十八课:js样式操作需要注意的问题

    样式分为,外部样式(<link />),内部样式(<style></style>),行内样式(style:).再加上一个important对选择器权重的干扰. 大体 ...

  6. Eclipse自动生成UML图(转载)

    *ModelGoon是什么? 它是一个Eclipse插件,用于基于UML图的模型设计,以及逆向工程(即从已有源代码生成类图等,以作为代码分析或者文档使用). *适用条件 ModelGoon目前最新的版 ...

  7. 使用Web Deploy进行远程部署

    Web Deploy支持直接从本地Visual Studio的工程文件部署网站到远程服务器,部署的过程中可以对比哪些文件变化了需要拷贝,而不是一股脑的全部拷贝,效率和准确性会更好. 部署的过程主要要注 ...

  8. 【前端】Sublime text3 插件LiveReload 实现实时预览

    1.首先要安装插件LiveReload Sublime text3. 菜单 preferences->packages control,输入install.. 回车,输入LiveReload回车 ...

  9. (Beta)Let's-Beta阶段展示博客

    康家华:http://www.cnblogs.com/AmazingMax/ 马阿姨:http://www.cnblogs.com/oushihuahua/ 刘彦熙:http://www.cnblog ...

  10. codeforces 359D 二分答案+RMQ

    上学期刷过裸的RMQ模板题,不过那时候一直不理解>_< 其实RMQ很简单: 设f[i][j]表示从i开始的,长度为2^j的一段元素中的最小值or最大值 那么f[i][j]=min/max{ ...