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#列表框、复选列表框、组合框的用法,实例分析了在一个简单存储项目中列表框、复选列表框、组合框的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-01-01
  • C#多线程开发之任务并行库详解

    C#多线程开发之任务并行库详解

    最近在学习C#的并行编程,在每本书上的看到的知识点都不全面,所以先参考多本书书籍的讲解,将并行编程,多线程编程的知识点整理一下,这篇文章主要给大家介绍了关于C#多线程开发之任务并行库的相关资料,需要的朋友可以参考下
    2021-09-09
  • C#实现路由器断开连接,更改公网ip的实例代码

    C#实现路由器断开连接,更改公网ip的实例代码

    C#实现路由器断开连接,更改公网ip的实例代码,需要的朋友可以参考一下
    2013-05-05
  • C#中GraphicsPath的Widen方法用法实例

    C#中GraphicsPath的Widen方法用法实例

    这篇文章主要介绍了C#中GraphicsPath的Widen方法用法,实例分析了Widen方法的使用技巧,需要的朋友可以参考下
    2015-06-06
  • C#使用UdpClient类进行简单通信的实例

    C#使用UdpClient类进行简单通信的实例

    本文主要介绍了C#使用UdpClient类进行简单通信的实例,具有很好的参考价值,需要的朋友可以看下
    2016-12-12
  • c# 实现位图算法(BitMap)

    c# 实现位图算法(BitMap)

    这篇文章主要介绍了c# 如何实现位图算法(BitMap),文中讲解非常细致,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • Unity实现批量Build打包详解

    Unity实现批量Build打包详解

    一般来讲如果项目是PC或Android、IOS端不会有批量Build打包这样的需求,但如果项目是WebGL端可能会遇到这样的需求。本文主要为大家介绍Unity中如何实现Build批量打包的,需要的朋友可以参考一下
    2021-12-12
  • 逐步讲解快速排序算法及C#版的实现示例

    逐步讲解快速排序算法及C#版的实现示例

    快速排序在时间复杂度同为O(N*logN)的几种排序方法中效率较高,因而比较常用,接下来这里就来逐步讲解快速排序算法及C#版的实现示例
    2016-06-06
  • C#如何动态创建lambda表达式

    C#如何动态创建lambda表达式

    这篇文章主要介绍了C#如何动态创建lambda表达式问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • 关于C# 类的封装详情

    关于C# 类的封装详情

    C#中可使用类来达到数据封装的效果,这样就可以使数据与方法封装成单一元素,以便于通过,接下来小编将为大家详细介绍,需要的朋友可以参考一下
    2021-10-10

最新评论