统一相机移动之屏幕边缘检测的示例分析

  介绍

小编给大家分享一下统一相机移动之屏幕边缘检测的示例分析,希望大家阅读完这篇文章之后都有所收获、下面让我们一起去探讨吧!

具体内容如下

<强>功能:

类似LOL红警相机移动方式。

鼠标移动到屏幕边缘,相机随之移动。

当然还有可以加亿一点点细节,比如鼠标指针变化,滚轮推进拉远视野,中键平移视野等。(没做)只

<>强效果图:

相统一的机移动之屏幕边缘检测的示例分析

这里做了可视化数据(可以看到限定的屏幕距离),线框内为检测的距离。

相统一的机移动之屏幕边缘检测的示例分析

<强>代码:

复制脚本,直接挂载相机上就可以用。

using  UnityEngine;   ,///,& lt; summary>///,相机边缘移动///,& lt;/summary>   [RequireComponent typeof(相机)))   public  class  CameraScreenEdgeMove : MonoBehaviour   {   ,   ,   ,[标题(“使用边缘移动“))   ,public  bool  isUseMoveOnScreenEdge =,真的;   ,   ,///& lt; summary>   ,///打开调试   ,///& lt;/summary>   ,public  bool  isDebugScreenEdge =,真的;   ,   ,//移动速度   ,public  float  moveSpeed =, 1 f;   ,   ,///& lt; summary>   ,///距离屏幕边缘多远就开始移动相机   ,///& lt;/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 垫;   ,private  Vector3  dir =, Vector3.zero;   ,   ,private  void  Start ()   ,{   ,CreateLineMaterial ();   ,}   ,   ,private  void 更新()   ,{   ,if  (isUseMoveOnScreenEdge)   ,{   UpRect 才能=,new 矩形(1 f, Screen.height 作用;ScreenEdgeSize,,回答,,ScreenEdgeSize);   DownRect 才能=,new 矩形(f 1 f,, 1,,回答,,ScreenEdgeSize);   ,   LeftRect 才能=,new 矩形(f 1 f,, 1,, ScreenEdgeSize,, Screen.height);   RigthRect 才能=,new 矩形(Screen.width 安康;ScreenEdgeSize,, 1 f, 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  *,移动速度Time.deltaTime);   ,   ,}   ,}   ,   ,   ,void  CreateLineMaterial ()   ,{   ,if (垫)   ,{   Shader 才能;Shader =, Shader.Find(“隐藏/Internal-Colored");   时间=mat 才能;new 材料(材质);   时间=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)   ,{   绘制矩形才能(UpRect, MoveUp,, Color.cyan,, Color.red),,   绘制矩形才能(DownRect, MoveDown,, Color.green,, Color.red),,   绘制矩形才能(LeftRect, MoveLeft,, Color.yellow,, Color.red),,   绘制矩形才能(RigthRect, MoveRight,, Color.blue,, Color.red),,   ,}   ,}   ,   ,private  void 绘制矩形(Rect 矩形,bool  isMouseEnter,, Color  normalColor,, Color  HeighLightColor)   ,{   ,if  (isMouseEnter)   ,{   DrawScreenRect才能(矩形,HeighLightColor);   ,}   其他的,   ,{   DrawScreenRect才能(矩形,normalColor);   ,}   ,}   ,   ,private  void  DrawScreenRect (Rect 矩形,Color 颜色)   ,{   ,GL.LoadOrtho ();   ,GL.Begin (GL.LINES);   ,{   mat.SetPass才能(0);   GL.Color才能(颜色);   GL.Vertex3才能(rect.xMin /,回答,,rect.yMin /, Screen.height,, 0);   GL.Vertex3才能(rect.xMin /,回答,,rect.yMax /, Screen.height,, 0);   ,   null   null   null   null   null   null   null   null   null   null   null   null   null   null

统一相机移动之屏幕边缘检测的示例分析