Unity相机移动之屏幕边缘检测

所属分类: 软件编程 / C#教程 阅读数: 61
收藏 0 赞 0 分享

本文实例为大家分享了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();
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C#中Datetimepicker出现问题的解决方法

这篇文章主要给大家介绍了关于C#中Datetimepicker出现问题的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C# SQLite数据库入门使用说明

这篇文章主要给大家介绍了关于C#中SQLite数据库入门使用的相关资料,文中通过图文以及示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#实现批量下载图片到本地示例代码

这篇文章主要给大家介绍了关于C#如何实现批量下载图片到本地的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用c#具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

如何获取C#中方法的执行时间以及其代码注入详解

这篇文章主要给大家介绍了关于如何获取C#中方法的执行时间以及其代码注入的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起看看吧
收藏 0 赞 0 分享

C#中通过LRU实现通用高效的超时连接探测

这篇文章主要介绍了c#中通过LRU实现通用高效的超时连接探测,非常不错,具有一定的参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

如何使用C#将Tensorflow训练的.pb文件用在生产环境详解

这篇文章主要给大家介绍了关于如何使用C#将Tensorflow训练的.pb文件用在生产环境的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#程序启动项的设置方法

这篇文章主要为大家详细介绍了C#程序启动项的设置方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

c#爬虫爬取京东的商品信息

这篇文章主要给大家介绍了关于利用c#爬虫爬取京东商品信息的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们随着小编来一起学习学习吧
收藏 0 赞 0 分享

C#随机数生成字母金字塔

这篇文章主要为大家详细介绍了C#随机数生成字母金字塔,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

WPF实现窗体中的悬浮按钮

这篇文章主要为大家详细介绍了WPF实现窗体中的悬浮按钮,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多