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

 更新时间:2020年02月20日 09:57:48   作者:萌萌的提莫队长  
这篇文章主要为大家详细介绍了Unity相机移动之屏幕边缘检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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#使用字典存储事件示例及实现自定义事件访问器

    在C#使用字典存储事件示例及实现自定义事件访问器

    这篇文章主要介绍了在C#使用字典存储事件示例及实现自定义事件访问器的方法,是C#事件编程中的基础知识,需要的朋友可以参考下
    2016-02-02
  • Unity shader实现百叶窗特效

    Unity shader实现百叶窗特效

    这篇文章主要为大家详细介绍了Unity shader实现百叶窗特效,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-11-11
  • C# 9.0 特性全面总结

    C# 9.0 特性全面总结

    这篇文章主要介绍了C# 9.0 特性的相关资料,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • C#基于UDP进行异步通信的方法

    C#基于UDP进行异步通信的方法

    这篇文章主要介绍了C#基于UDP进行异步通信的方法,实例分析了C#基于UDP实现异步通信的相关技巧,需要的朋友可以参考下
    2015-04-04
  • C#调用WebService实例开发

    C#调用WebService实例开发

    那么,我们怎么在项目中调用WebService这个方法呢,其实这和调用天气的webservice是一个道理,首先,通过添加“web服务 引用”将,你写的webservice引用进来,我们需要注意的是其中有一处要我们填写请求webservice的URL地址,我们该怎么写?
    2015-09-09
  • C#之CLR内存字符串常量池(string)

    C#之CLR内存字符串常量池(string)

    这篇文章主要介绍了C#之CLR内存字符串常量池(string),对于学习和理解C#内存原理很有帮助,需要的朋友可以参考下
    2014-08-08
  • C#中异步回调函数用法实例

    C#中异步回调函数用法实例

    这篇文章主要介绍了C#中异步回调函数用法,实例分析了异步回调函数的定义及使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#使用Socket实现本地多人聊天室

    C#使用Socket实现本地多人聊天室

    这篇文章主要为大家详细介绍了C#使用Socket实现本地多人聊天室,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C#验证码的创建与使用示例

    C#验证码的创建与使用示例

    这篇文章主要介绍了C#验证码的创建与使用方法,结合实例形式较为详细的分析了C#验证码的创建、验证等操作步骤与相关技巧,需要的朋友可以参考下
    2017-01-01
  • C# double和decimal数据类型以截断的方式保留指定的小数位数

    C# double和decimal数据类型以截断的方式保留指定的小数位数

    从事ASP.NET in C#开发快一年了,今天才知道,C#中保留小数位数时没有使用截断的方式
    2012-05-05

最新评论