NGUI版虚拟摇杆
以下是我用nui实现的一个虚拟摇杆。
1,示图
2、代码例如以下,都有比較具体的凝视。就不说明了。
using UnityEngine;
using System.Collections;
using System.Collections.Generic; [ExecuteInEditMode]
public class Joystick : MonoBehaviour
{
#region Delegate & Event
public delegate void JoystickEventHandler(Joystick joystick);
/// <summary>
/// 开如
/// </summary>
public static event JoystickEventHandler On_JoystickMoveStart;
/// <summary>
/// Occurs when the joystick move.
/// </summary>
public static event JoystickEventHandler On_JoystickMove;
/// <summary>
/// thumb偏离中心位置。并牌按住时,每帧的回调
/// </summary>
public static event JoystickEventHandler On_JoystickHolding;
/// <summary>
/// Occurs when the joystick stops move
/// </summary>
public static event JoystickEventHandler On_JoystickMoveEnd; #endregion #region property
[SerializeField] bool isRunInEditor = false;
[SerializeField]private string joystickName = "NguiJoystick";
public string JoystickName { get { return this.joystickName; } }
[HideInInspector]private bool isLimitInCircle = true;
public bool IsLimitInCircle { get { return this.isLimitInCircle; } }
[SerializeField]private int radius = 100;
public int Radius { get{ return this.radius; } } [SerializeField]
private float minAlpha = 0.3f;
public float MinAlpha { get { return this.minAlpha; } } private Vector2 joystickAxis = Vector2.zero;
/// <summary>
/// Gets the joystick axis value between -1 & 1...
/// </summary>
/// <value>
/// The joystick axis.
/// </value>
public Vector2 JoystickAxis { get { return this.joystickAxis; } } private Vector2 lastJoystickAxis = Vector2.zero;
public Vector2 LastJoystickAxis { get { return this.lastJoystickAxis; } } bool isForBid = false;
/// <summary>
/// 推断joystick是否被禁用
/// </summary>
public bool IsForBid { get { return this.isForBid; } }
bool isHolding = false;
public bool IsHolding { get { return this.isHolding; } }
#endregion UIWidget root;
[SerializeField]UISprite bg;
[SerializeField]UISprite thumb; void Awake()
{
this.name = this.JoystickName;
root = this.GetComponent<UIWidget>();
Init();
} // Update is called once per frame
void Update ()
{
if (isRunInEditor && Application.isEditor && !Application.isPlaying)
{
SetJoystickSize(radius);
} if (!isForBid && isHolding)
{
Debug.Log("111111");
if (On_JoystickHolding != null)
{
On_JoystickHolding(this);
}
}
} void Init()
{
bg.transform.localPosition = Vector3.zero;
thumb.transform.localPosition = Vector3.zero;
SetJoystickSize(radius);
Lighting(minAlpha);
} #region ngui event
///// <summary>
///// test
///// </summary>
//void OnClick ()
//{
// Debug.Log("mouse pos :" + Input.mousePosition + " -- touch pos :" + ScreenPos_to_NGUIPos(Input.mousePosition));
// thumb.transform.localPosition = ScreenPos_to_NGUIPos(Input.mousePosition);
//}
void OnPress (bool isPressed)
{
if (isForBid)
{
Debug.Log("joystick is forbid!");
return;
}
Debug.Log("OnPress:" + isPressed.ToString());
if(isPressed)
{
Lighting(1f);
CalculateJoystickAxis();
if (On_JoystickMoveStart != null)
{
On_JoystickMoveStart(this);
}
isHolding = true;
}
else
{
CalculateJoystickAxis();
if (On_JoystickMoveEnd != null)
{
On_JoystickMoveEnd(this);
}
thumb.transform.localPosition = Vector3.zero;
FadeOut(1f, minAlpha);
isHolding = false;
}
} //void OnDragStart ()
//{
// if (isForBid)
// {
// Debug.Log("joystick is forbid!");
// return;
// } // Debug.Log("OnDragStart");
// Lighting(1f);
// CalculateJoystickAxis();
// if(On_JoystickMoveStart!=null)
// {
// On_JoystickMoveStart(this);
// }
// isHolding = true;
// Debug.Log(string.Format("time:{0} - axis:{1}", Time.time, joystickAxis));
//} void OnDrag(Vector2 delta)
{
if (isForBid)
{
return;
} //Debug.Log("OnDrag:"+delta.ToString());
CalculateJoystickAxis();
if (On_JoystickMoveStart != null)
{
On_JoystickMoveStart(this);
}
} //void OnDragEnd ()
//{
// if (isForBid)
// {
// return;
// } // Debug.Log("OnDragEnd");
// CalculateJoystickAxis();
// if (On_JoystickMoveEnd != null)
// {
// On_JoystickMoveEnd(this);
// }
// thumb.transform.localPosition = Vector3.zero;
// FadeOut(1f, minAlpha);
// isHolding = false;
//}
#endregion #region utile /// <summary>
/// 计算JoystickAxis
/// </summary>
/// <returns></returns>
void CalculateJoystickAxis()
{
Vector3 offset = ScreenPos_to_NGUIPos(UICamera.currentTouch.pos);
offset -= transform.localPosition;
if (isLimitInCircle)
{
if (offset.magnitude > radius)
{
offset = offset.normalized * radius;
}
}
thumb.transform.localPosition = offset; lastJoystickAxis = joystickAxis;
joystickAxis = new Vector2(offset.x / radius, offset.y / radius);
} /// <summary>
/// Axis2s the angle.
/// </summary>
/// <returns>
/// The angle.
/// </returns>
public float Axis2Angle(bool inDegree = true)
{
float angle = Mathf.Atan2(joystickAxis.x, joystickAxis.y); if (inDegree)
{
return angle * Mathf.Rad2Deg;
}
else
{
return angle;
}
} /// <summary>
/// Axis2s the angle.
/// </summary>
/// <returns>
/// The angle.
/// </returns>
public float Axis2Angle(Vector2 axis, bool inDegree = true)
{
float angle = Mathf.Atan2(axis.x, axis.y); if (inDegree)
{
return angle * Mathf.Rad2Deg;
}
else
{
return angle;
}
} /// <summary>
/// 屏幕坐标-->ui坐标
/// </summary>
/// <param name="screenPos"></param>
/// <returns></returns>
Vector3 ScreenPos_to_NGUIPos(Vector3 screenPos)
{
Vector3 uiPos = UICamera.currentCamera.ScreenToWorldPoint(screenPos);
uiPos = UICamera.currentCamera.transform.InverseTransformPoint(uiPos);
return uiPos;
} /// <summary>
/// 屏幕坐标-->ngui坐标
/// </summary>
/// <param name="screenPos"></param>
/// <returns></returns>
Vector3 ScreenPos_to_NGUIPos(Vector2 screenPos)
{
return ScreenPos_to_NGUIPos(new Vector3(screenPos.x, screenPos.y, 0f));
} /// <summary>
/// 设置摇杆的大小
/// </summary>
/// <param name="radius"></param>
void SetJoystickSize(int radius)
{
root.width = 2 * radius;
root.height = 2 * radius;
thumb.width = (int)(40f / 100f * root.width);
thumb.height = (int)(40f / 100f * root.height);
} /// <summary>
/// 点亮摇杆
/// </summary>
void Lighting(float alpha)
{
iTween.Stop(this.gameObject, "value");
root.alpha = alpha;
} /// <summary>
/// 渐变摇杆的透明度
/// </summary>
void FadeOut(float fromAlpha, float toAlpha)
{
Hashtable itweenArgs = new Hashtable();
itweenArgs.Add("easetype", iTween.EaseType.linear);
itweenArgs.Add("from", fromAlpha);
itweenArgs.Add("to", toAlpha);
itweenArgs.Add("time", 0.5f);
itweenArgs.Add("onupdate", "OnFadeOutTween");
iTween.ValueTo(this.gameObject, itweenArgs);
}
void OnFadeOutTween(float value)
{
root.alpha = value;
} #endregion #region 激活、禁用的控制
List<string> keys = new List<string>(); /// <summary>
/// 禁用
/// </summary>
/// <returns>返回值是,取消这个禁用要用到的key</returns>
public string ForbidJosystick()
{
string key = System.Guid.NewGuid().ToString();
keys.Add(key);
isForBid = true;
return key;
} /// <summary>
/// 启用
/// </summary>
/// <param name="key"></param>
public void ActivizeJosystick(string key)
{
if(keys.Contains(key))
{
keys.Remove(key);
} isForBid = true;
if(keys.Count==0)
{
isForBid = false;
}
} #endregion
}
3、demo包,有兴趣的,也能够看看。
下载:
NGUI版虚拟摇杆的更多相关文章
- 【转】NGUI版虚拟摇杆
http://blog.csdn.net/anyuanlzh/article/details/40107577 下面是我用nui实现的一个虚拟摇杆. 1,示图 2.代码如下,都有比较详细的注释,就不说 ...
- 【转】简单的虚拟摇杆控制移动(NGUI)
http://www.cnblogs.com/zhangbaochong/p/4928688.html 一.用NGUI创建虚拟摇杆贴图 先创建一个sprite作为背景叫做JoyStick 并添加一个B ...
- 简单的虚拟摇杆控制移动(NGUI)
一.用NGUI创建虚拟摇杆贴图 先创建一个sprite作为背景叫做JoyStick 并添加一个BoxCollider,再创建一个sprite child作为虚拟摇杆中间的按钮,叫做button 二.通 ...
- unity中虚拟摇杆的实现
实现效果: 实现: 使用NGUI添加虚拟摇杆背景和其子物体按钮,为按钮Attach boxcollider和ButtionScript.为按钮添加如下脚本: 注意:其中的静态属性可以在控制物体移动的 ...
- Unity3d项目入门之虚拟摇杆
Unity本身不提供摇杆的组件,开发者可以使用牛逼的EasyTouch插件或者应用NGUI实现相关的需求,下面本文通过Unity自身的UGUI属性,实现虚拟摇杆的功能. 主参考 <Unity:使 ...
- Unity 使用有限状态机 完美还原 王者荣耀 虚拟摇杆
Unity 使用有限状态机 完美还原 王者荣耀 虚拟摇杆 效果如图所示 摇杆的UI组成 如图所示 简单的可以认为摇杆由1.2.3贴图组成 为摇杆的底座 为摇杆的杆 为摇杆的指向 可以理解这就是街机上的 ...
- 【转】Unity3D学习日记(二)使用UGUI制作虚拟摇杆控制摄像机
http://blog.csdn.net/begonia__z/article/details/51178907 前天撸了一个简单的UGUI虚拟摇杆,今天我就利用前天做的虚拟摇杆做了一个简单的摄像机控 ...
- [Unity3D]Unity3D游戏开发之使用EasyTouch虚拟摇杆控制人物移动
大家好,欢迎大家关注我的博客,我是秦元培,我的博客地址是blog.csdn.net/qinyuanpei.今天呢,我们来一起学习在Unity3D中使用EasyTouch虚拟摇杆来控制人物移动.虽然Un ...
- unity零基础开始学习做游戏(三)鼠标输入,来个虚拟摇杆怎么样?
-------小基原创,转载请给我一个面子 现在移动游戏越来越火,大家都拿手机平板玩游戏,没有键盘和手柄输入,所以就不得不看看虚拟摇杆怎么搞?(小基对于没有实体反馈不是很喜欢呢) 首先要清楚,鼠标操作 ...
随机推荐
- loj2026 「JLOI / SHOI2016」成绩比较
orz #include <iostream> #include <cstdio> using namespace std; typedef long long ll; int ...
- powerdesigner约束名唯一出错的解决办法
powerdesigner中自动生成的约束名有时会因为表的前缀一样而不具有唯一性,这样在生成时就会出错,一般的解决办法有以下两种: 1.模型=>Reference中可以看到当前模型中的所有Ref ...
- day04_06 短路原则
True和False不能写成ture和false,不然会报错 not not True or False and not True 按照not>and>or来进行括号 (not (not ...
- 正在创建模型,此时不可使用上下文“的解决办法。 正在创建模型,此时不可使用上下文。如果在 OnModelCreating 方法内使用上下文或如果多个线程同时访问同一上下文实例,可能引发此异常。请注意不
//默认为: Database.SetInitializer<testContext>(null);//这里报错, 检查原因:catch(Exception ex) 错误提示: 基础连接未 ...
- [python][oldboy]关键字参数和位置参数,默认参数,可变长参数(无名,有名)
关键字参数和位置参数是在函数调用的时候定义 关键字参数是以键-值对出现,无序,多一个不行,少一个不行 位置参数,有序,多一个不行,少一个不行 混搭:所有位置参数必须在关键字参数的左边 默认参数是在函数 ...
- [python学习篇][廖雪峰][1]高级特性 ---迭代
由于字符串也是可迭代对象,因此,也可以作用于for循环: >>> for ch in 'ABC': ... print ch ... A B C 所以,当我们使用for循环时,只要作 ...
- spring scope 属性的取值
Spring 容器是通过单例模式创建 Bean 对象的,也就是说,默认情况下,通过调用 ac.getBean("mybean")方法获得的对象都是同一个 mybean 对象 使用单 ...
- hdu2094 stl之set的应用
产生冠军 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- Bash Command 1: find
GNU find searches the directory tree rooted at each given starting-point by evaluating the given exp ...
- NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)
Problem 1037: Wormhole Time Limits: 5000 MS Memory Limits: 200000 KB 64-bit interger IO format: ...