Unity相机移动之屏幕边缘检测的示例分析
小编给大家分享一下Unity相机移动之屏幕边缘检测的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:成都做网站、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的定南网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
具体内容如下
功能:
类似LOL 红警 相机移动方式。
鼠标移动到屏幕边缘,相机随之移动。
当然还有可以加亿一点点细节,比如鼠标指针变化,滚轮推进拉远视野,中键平移视野等。(没做)。
效果图:
这里做了可视化数据(可以看到限定的屏幕距离),线框内为检测的距离。
代码:
复制脚本,直接挂载相机上就可以用。
using UnityEngine; ////// 相机边缘移动 /// [RequireComponent(typeof(Camera))] public class CameraScreenEdgeMove :MonoBehaviour { [Header("使用边缘移动")] public bool isUseMoveOnScreenEdge = true; ////// 打开调试 /// public bool isDebugScreenEdge = true; //移动速度 public float moveSpeed = 1f; ////// 距离屏幕边缘多远就开始移动相机 /// public int ScreenEdgeSize = 20; private bool MoveUp; private bool MoveDown; private bool MoveRight; private bool MoveLeft; private Rect RigthRect; private Rect UpRect; private Rect DownRect; private Rect LeftRect; private Material mat; private Vector3 dir = Vector3.zero; private void Start() { CreateLineMaterial(); } private void Update() { if (isUseMoveOnScreenEdge) { UpRect = new Rect(1f, Screen.height - ScreenEdgeSize, Screen.width, ScreenEdgeSize); DownRect = new Rect(1f, 1f, Screen.width, ScreenEdgeSize); LeftRect = new Rect(1f, 1f, ScreenEdgeSize, Screen.height); RigthRect = new Rect(Screen.width - ScreenEdgeSize, 1f, ScreenEdgeSize, Screen.height); MoveUp = (UpRect.Contains(Input.mousePosition)); MoveDown = (DownRect.Contains(Input.mousePosition)); MoveLeft = (LeftRect.Contains(Input.mousePosition)); MoveRight = (RigthRect.Contains(Input.mousePosition)); dir.z = MoveUp ? 1 : MoveDown ? -1 : 0; dir.x = MoveLeft ? -1 : MoveRight ? 1 : 0; transform.position = Vector3.Lerp(transform.position, transform.position + dir * moveSpeed,Time.deltaTime); } } void CreateLineMaterial() { if (!mat) { Shader shader = Shader.Find("Hidden/Internal-Colored"); mat = new Material(shader); mat.hideFlags = HideFlags.HideAndDontSave; mat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha); mat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); mat.SetInt("_Cull", (int)UnityEngine.Rendering.CullMode.Off); mat.SetInt("_ZWrite", 0); } } void OnPostRender() { if (isUseMoveOnScreenEdge && isDebugScreenEdge) { DrawRect(UpRect, MoveUp, Color.cyan, Color.red); DrawRect(DownRect, MoveDown, Color.green, Color.red); DrawRect(LeftRect, MoveLeft, Color.yellow, Color.red); DrawRect(RigthRect, MoveRight, Color.blue, Color.red); } } private void DrawRect(Rect rect, bool isMouseEnter, Color normalColor, Color HeighLightColor) { if (isMouseEnter) { DrawScreenRect(rect, HeighLightColor); } else { DrawScreenRect(rect, normalColor); } } private void DrawScreenRect(Rect rect, Color color) { GL.LoadOrtho(); GL.Begin(GL.LINES); { mat.SetPass(0); GL.Color(color); GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0); GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0); GL.Vertex3(rect.xMin / Screen.width, rect.yMax / Screen.height, 0); GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0); GL.Vertex3(rect.xMax / Screen.width, rect.yMax / Screen.height, 0); GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0); GL.Vertex3(rect.xMax / Screen.width, rect.yMin / Screen.height, 0); GL.Vertex3(rect.xMin / Screen.width, rect.yMin / Screen.height, 0); } GL.End(); } }
看完了这篇文章,相信你对“Unity相机移动之屏幕边缘检测的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
分享文章:Unity相机移动之屏幕边缘检测的示例分析
网站URL:http://ybzwz.com/article/jhipgi.html