欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-14 17:31:04
本文实例为大家分享了unity相机移动之屏幕边缘检测的具体代码,供大家参考,具体内容如下功能:类似lol 红警 相机移动方式。鼠标移动到屏幕边缘,相机随之移动。当然还有可以加亿一点点细节,比如鼠标指针...

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

功能:

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

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

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

效果图:

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

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

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

代码:

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

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();
 }
 
}

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