服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - Unity相机移动之屏幕边缘检测

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

2022-08-28 16:21萌萌的提莫队长 C#

这篇文章主要为大家详细介绍了Unity相机移动之屏幕边缘检测,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity相机移动之屏幕边缘检测的具体代码,供大家参考,具体内容如下

功能:

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

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

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

效果图:

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

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

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

代码:

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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();
 }
 
}

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

原文链接:https://blog.csdn.net/qq_35030499/article/details/104363468

延伸 · 阅读

精彩推荐
  • C#C#中通过LRU实现通用高效的超时连接探测

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

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

    smark4562022-03-05
  • C#c# winform treelistview的使用(treegridview)实例详解

    c# winform treelistview的使用(treegridview)实例详解

    这篇文章主要介绍了c# winform treelistview的使用(treegridview),本文通过实例代码给大家详细介绍,需要的朋友可以参考下...

    波谷6192022-02-13
  • C#c#中@的3种作用

    c#中@的3种作用

    本文主要介绍了c#中@的3种作用。具有很好的参考价值,下面跟着小编一起来看下吧...

    贤勇7832021-12-23
  • C#C#简单查询SQLite数据库是否存在数据的方法

    C#简单查询SQLite数据库是否存在数据的方法

    这篇文章主要介绍了C#简单查询SQLite数据库是否存在数据的方法,涉及C#调用SQLite组件及针对SQLite数据库基本的连接、查询、关闭等使用技巧,需要的朋友可以...

    IT部落格9372021-11-29
  • C#C#实现简单合并word文档的方法

    C#实现简单合并word文档的方法

    这篇文章主要介绍了C#实现简单合并word文档的方法,涉及C#针对word文档的读取、插入、保存等技巧,非常具有实用价值,需要的朋友可以参考下...

    C#教程网3892021-10-26
  • C#C# websocket及时通信协议的实现方法示例

    C# websocket及时通信协议的实现方法示例

    说到websocket大家一定不会陌生,WebSocket是HTML5一种新的协议。下面这篇文章主要给大家介绍了关于C# websocket及时通信协议的实现方法,文中通过示例代码介...

    C#教程网7432022-02-12
  • C#浅谈C# 类的继承

    浅谈C# 类的继承

    本文主要介绍了C# 类的继承相关知识。具有很好的参考价值,下面跟着小编一起来看下吧...

    liyongke7252021-12-20
  • C#C#编程实现四舍五入、向上及下取整的方法

    C#编程实现四舍五入、向上及下取整的方法

    这篇文章主要介绍了C#编程实现四舍五入、向上及下取整的方法,涉及C#数学运算的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    碧水寒潭4142021-11-03