原始的:

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. 小白学习mysql之存储过程的优劣分析以及接入控制

    存储过程的优劣 存储过程是一组实现特定功能的SQL语句集合,存储过程一经编译便存储在了服务器上,可以通过调用存储过程的名字以及传入相应的参数来使用存储过程.要高层次的掌握存储过程,不能觉得依葫芦画瓢, ...

  2. 让C#开发更简单,抽象增删改

    相信经常做一些MS,CRM 项目的童鞋非常有体会,大部分时间都是在复制和粘贴,大部分项目框架都是建一个三层,首先是DAL建一些增删改查,然后呢是BLL业务层再建一些增删改查,然后UI层再调用增删改查, ...

  3. 每天一个linux命令(41):at命令

    在windows系统中,windows提供了计划任务这一功能,在控制面板 -> 性能与维护 -> 任务计划, 它的功能就是安排自动运行的任务. 通过'添加任务计划'的一步步引导,则可建立一 ...

  4. 每天一个linux命令(12):head 命令

    head 与 tail 就像它的名字一样的浅显易懂,它是用来显示开头或结尾某个数量的文字区块,head 用来显示档案的开头至标准输出中,而 tail 想当然尔就是看档案的结尾. 1.命令格式: hea ...

  5. HDU 5115 Dire Wolf 区间dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5115 Dire Wolf Time Limit: 5000/5000 MS (Java/Others ...

  6. beta汇总

    第一天:http://www.cnblogs.com/hxh969012806/p/5034085.html 第二天:http://www.cnblogs.com/zyk150910/p/503783 ...

  7. Yii2修改默认布局

    public $layout = 'layout';//在类中定义一个变量,名为$layout的php文件 <?php echo $content; ?>

  8. 40.Android之新手指引界面学习

    我们经常可以看到打开新App会有新手指引界面,类似蒙板效果今天来学习.原理其实很简单,设置一个透明Activity或者Dialog,然后修改其属性即可.由于实现比较简单,就贴一部分代码. 1.在And ...

  9. UVa 1347 Tour

    Tour Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Description   Joh ...

  10. 循序渐进Linux 3:Linux下软件安装与管理

    一.源码安装 ./configuremakemake install 二.RPM包 1. 安装软件包 rpm -i [辅助选项] file1.rpm file2.rpm主选项 -i: install, ...