Unity相机移动之屏幕边缘检测
本文实例为大家分享了Unity相机移动之屏幕边缘检测的具体代码,供大家参考,具体内容如下
功能:
类似LOL红警相机移动方式。
鼠标移动到屏幕边缘,相机随之移动。
当然还有可以加亿一点点细节,比如鼠标指针变化,滚轮推进拉远视野,中键平移视野等。(没做)。
效果图:
这里做了可视化数据(可以看到限定的屏幕距离),线框内为检测的距离。
代码:
复制脚本,直接挂载相机上就可以用。
usingUnityEngine; //////相机边缘移动 /// [RequireComponent(typeof(Camera))] publicclassCameraScreenEdgeMove:MonoBehaviour { [Header("使用边缘移动")] publicboolisUseMoveOnScreenEdge=true; //////打开调试 /// publicboolisDebugScreenEdge=true; //移动速度 publicfloatmoveSpeed=1f; //////距离屏幕边缘多远就开始移动相机 /// publicintScreenEdgeSize=20; privateboolMoveUp; privateboolMoveDown; privateboolMoveRight; privateboolMoveLeft; privateRectRigthRect; privateRectUpRect; privateRectDownRect; privateRectLeftRect; privateMaterialmat; privateVector3dir=Vector3.zero; privatevoidStart() { CreateLineMaterial(); } privatevoidUpdate() { if(isUseMoveOnScreenEdge) { UpRect=newRect(1f,Screen.height-ScreenEdgeSize,Screen.width,ScreenEdgeSize); DownRect=newRect(1f,1f,Screen.width,ScreenEdgeSize); LeftRect=newRect(1f,1f,ScreenEdgeSize,Screen.height); RigthRect=newRect(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); } } voidCreateLineMaterial() { if(!mat) { Shadershader=Shader.Find("Hidden/Internal-Colored"); mat=newMaterial(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); } } voidOnPostRender() { 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); } } privatevoidDrawRect(Rectrect,boolisMouseEnter,ColornormalColor,ColorHeighLightColor) { if(isMouseEnter) { DrawScreenRect(rect,HeighLightColor); } else { DrawScreenRect(rect,normalColor); } } privatevoidDrawScreenRect(Rectrect,Colorcolor) { 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(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。