您好,登錄后才能下訂單哦!
小編給大家分享一下Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè)的示例分析,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內(nèi)容如下
功能:
類似LOL 紅警 相機(jī)移動(dòng)方式。
鼠標(biāo)移動(dòng)到屏幕邊緣,相機(jī)隨之移動(dòng)。
當(dāng)然還有可以加億一點(diǎn)點(diǎn)細(xì)節(jié),比如鼠標(biāo)指針變化,滾輪推進(jìn)拉遠(yuǎn)視野,中鍵平移視野等。(沒做)。
效果圖:
這里做了可視化數(shù)據(jù)(可以看到限定的屏幕距離),線框內(nèi)為檢測(cè)的距離。
代碼:
復(fù)制腳本,直接掛載相機(jī)上就可以用。
using UnityEngine; /// <summary> /// 相機(jī)邊緣移動(dòng) /// </summary> [RequireComponent(typeof(Camera))] public class CameraScreenEdgeMove :MonoBehaviour { [Header("使用邊緣移動(dòng)")] public bool isUseMoveOnScreenEdge = true; /// <summary> /// 打開調(diào)試 /// </summary> public bool isDebugScreenEdge = true; //移動(dòng)速度 public float moveSpeed = 1f; /// <summary> /// 距離屏幕邊緣多遠(yuǎn)就開始移動(dòng)相機(jī) /// </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(); } }
看完了這篇文章,相信你對(duì)“Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè)的示例分析”有了一定的了解,如果想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。