NGUIJoysticK
原始的:
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的更多相关文章
- 【转】NGUI版虚拟摇杆
http://blog.csdn.net/anyuanlzh/article/details/40107577 下面是我用nui实现的一个虚拟摇杆. 1,示图 2.代码如下,都有比较详细的注释,就不说 ...
- NGUI版虚拟摇杆
以下是我用nui实现的一个虚拟摇杆. 1,示图 2.代码例如以下,都有比較具体的凝视.就不说明了. using UnityEngine; using System.Collections; using ...
- Unity基于NGUI的简单并可直接使用的虚拟摇杆实现(一)
可能大家都听说过大名鼎鼎的easytouch,然而easytouch是基于UGUI的,两种不同的UI混用,可能会造成项目管理的混乱,并且可能会出现各种幺蛾子,比如事件传递互相扰乱的问题. 于是就想找一 ...
随机推荐
- [工具类]将时间转换为unix时间戳格式
写在前面 由于在数据库中存的时间有时间戳格式的数据,在解析以及保存的时候,就需要考虑到数据格式的兼容性问题.看到数据库中的时间字段基本上都是以时间戳格式存储的,没办法,只能将时间进行转换了,考虑到其他 ...
- ListView 和 Adapter用法
一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...
- G-nav-03
/*dele masthead.css style*/.masthead .navigation .btn.btn-masthead.btn-apply:after { content: ''; di ...
- Java_HttpURLConnection使用
包括使用HttpURLConnection执行get/post请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...
- Freemarker-标签使用
FreeMarker标签使用 FreeMarker模板文件主要有4个部分组成 1.文本,直接输出的部分 2.注释,即<#--...-->格式不会输出 3.插值(Interpolatio ...
- codeforces 715B:Complete The Graph
Description ZS the Coder has drawn an undirected graph of n vertices numbered from 0 to n - 1 and m ...
- [NOIP2011] 提高组 洛谷P1312 Mayan游戏
题目描述 Mayan puzzle是最近流行起来的一个游戏.游戏界面是一个 7 行5 列的棋盘,上面堆放着一些方块,方块不能悬空堆放,即方块必须放在最下面一行,或者放在其他方块之上.游戏通关是指在规定 ...
- windows搭建openacs编译环境
1.下载ant工具用来编译openacs源码 apache-ant-1.8.2 下载地址http://ant.apache.org/ 这个文件不用编译,在目录bin/下有针对windows的ant 2 ...
- Extjs Form用法详解(适用于Extjs5)
Extjs Form是一个比较常用的控件,主要用来显示和编辑数据的,今天这篇文章将介绍Extjs Form控件的详细用法,包括创建Form.添加子项.加载和更新数据.验证等. 本文的示例代码适用于Ex ...
- CustomerConfigHelper
public static class CustomerConfigHelper { public static object _lockObject = new object(); private ...