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混用,可能会造成项目管理的混乱,并且可能会出现各种幺蛾子,比如事件传递互相扰乱的问题. 于是就想找一 ...
随机推荐
- 记录我开始学习 Git的路程
工作半年多了,总觉得没学到什么东西,于是乎找了个Git学习一下,感觉还蛮厉害的样子.为此记录下我的路程 2015,11,26 更新 前面的路都挺艰难的,在官网下载msysgit网速几乎为0(心情千万只 ...
- Java问题:Quartz,Hibernate,Spring,Tomcat中定时任务无故停止,没有错误
最近在做一个java项目的时候遇到一个十分奇怪的问题,想到大家可能也会遇到这样的问题,所以在此发出来,希望大家遇到的时候能够快速解决! 直入主题 问题:使用quartz进行定时任务自动执行的时候,用到 ...
- Linq之Expression进阶
目录 写在前面 系列文章 表达式树解析 表达式树特性 编译表达树 总结 写在前面 让我们首先简单回顾一下上篇文章介绍的内容,上篇文章介绍了表达式树的基本概念(表达式树又称为“表达式目录树”,以数据形式 ...
- 软工实践练习一(个人)----将Androidstudio的项目共享到github
在Androidstudio上使用git插件 将项目共享至github 将 显示共享成功但是出了点问题 项目文件并没有上传至github库中,而是只创建了一个新的库 问题在于我的gitforwindo ...
- Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]
Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...
- oracle 的安装脚本
==[root@oracle ~]# cat 1.preusers.sh ==#!/bin/bash#Purpose:Create 3 groups named 'oinstall','dba','o ...
- CXF 自定义拦截器
此例子来自apache cxf sample. /** * Licensed to the Apache Software Foundation (ASF) under one * or more ...
- Shell重定向&>file、2>&1、1>&2的区别
shell上: 0表示标准输入 1表示标准输出 2表示标准错误输出 > 默认为标准输出重定向,与 1> 相同 2>&1 意思是把 标准错误输出 重定向到 标准输出. & ...
- 【前端】Sublime text3 插件HTML/CSS/JS prettify 格式化代码
1.首先安装插件 菜单的preference->packages control,然后输入install .. 回车,再输入HTML/CSS/JS prettify 再回车,重启后就可以了. 2 ...
- JAVA的整型与字符串相互转换
1如何将字串 String 转换成整数 int? A. 有两个方法: 1). int i = Integer.parseInt([String]); 或 i = Integer.parseInt([S ...