时间:2021-05-20
本文实例为大家分享了Unity相机移动之屏幕边缘检测的具体代码,供大家参考,具体内容如下
功能:
类似LOL 红警 相机移动方式。
鼠标移动到屏幕边缘,相机随之移动。
当然还有可以加亿一点点细节,比如鼠标指针变化,滚轮推进拉远视野,中键平移视野等。(没做)。
效果图:
这里做了可视化数据(可以看到限定的屏幕距离),线框内为检测的距离。
代码:
复制脚本,直接挂载相机上就可以用。
using UnityEngine; /// <summary>/// 相机边缘移动/// </summary>[RequireComponent(typeof(Camera))]public class CameraScreenEdgeMove :MonoBehaviour{ [Header("使用边缘移动")] public bool isUseMoveOnScreenEdge = true; /// <summary> /// 打开调试 /// </summary> public bool isDebugScreenEdge = true; //移动速度 public float moveSpeed = 1f; /// <summary> /// 距离屏幕边缘多远就开始移动相机 /// </summary> 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(); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#图像处理之边缘检测(Smoothed)的方法。分享给大家供大家参考。具体如下://定义smoothed算子边缘检测函数privatestati
最近自己在做一个有关图像处理的小项目,涉及到图像的边缘检测、直线检测、轮廓检测以及角点检测等,本文首先介绍图像的边缘检测,使用的是Canny边缘检测算法,具体代
本文实例讲述了C#图像处理之边缘检测(Sobel)的方法。分享给大家供大家参考。具体如下://定义sobel算子函数privatestaticBitmapsob
边缘检测Canny边缘检测器是一种被广泛使用的算法,并被认为是边缘检测最优的算法,该方法使用了比高斯差分算法更复杂的技巧,如多向灰度梯度和滞后阈值化。Canny
图像轮廓Contours:轮廓轮廓是将没有连着一起的边缘连着一起。边缘检测检测出边缘,边缘有些未连接在一起。注意问题1.对象为二值图像,首先进行阈值分割或者边缘