欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity相機(jī)移動之屏幕邊緣檢測

 更新時間:2020年02月20日 09:57:48   作者:萌萌的提莫隊(duì)長  
這篇文章主要為大家詳細(xì)介紹了Unity相機(jī)移動之屏幕邊緣檢測,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Unity相機(jī)移動之屏幕邊緣檢測的具體代碼,供大家參考,具體內(nèi)容如下

功能:

類似LOL 紅警 相機(jī)移動方式。

鼠標(biāo)移動到屏幕邊緣,相機(jī)隨之移動。

當(dāng)然還有可以加億一點(diǎn)點(diǎn)細(xì)節(jié),比如鼠標(biāo)指針變化,滾輪推進(jìn)拉遠(yuǎn)視野,中鍵平移視野等。(沒做)。 

效果圖:

這里做了可視化數(shù)據(jù)(可以看到限定的屏幕距離),線框內(nèi)為檢測的距離。

代碼:

復(fù)制腳本,直接掛載相機(jī)上就可以用。

using UnityEngine;
 
/// <summary>
/// 相機(jī)邊緣移動
/// </summary>
[RequireComponent(typeof(Camera))]
public class CameraScreenEdgeMove :MonoBehaviour
{
 
 
 [Header("使用邊緣移動")]
 public bool isUseMoveOnScreenEdge = true;
 
 /// <summary>
 /// 打開調(diào)試
 /// </summary>
 public bool isDebugScreenEdge = true;
 
 //移動速度
 public float moveSpeed = 1f;
 
 /// <summary>
 /// 距離屏幕邊緣多遠(yuǎn)就開始移動相機(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();
 }
 
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vs2005中總是保留最近打開的項(xiàng)目和文件的記錄

    vs2005中總是保留最近打開的項(xiàng)目和文件的記錄

    這篇文章主要介紹了vs2005中總是保留最近打開的項(xiàng)目和文件的記錄,需要的朋友可以參考下
    2016-06-06
  • Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)

    Unity游戲開發(fā)之2048游戲的實(shí)現(xiàn)

    2048是一款數(shù)字益智游戲,初始數(shù)字則是由2+2組成的基數(shù)4。在操作方面的不同則表現(xiàn)為一步一格的移動,變成更為爽快的一次到底。相同數(shù)字的方?jīng)r在靠攏、相撞時會相加。本文將通過Unity3D實(shí)現(xiàn)這一游戲,需要的可以參考一下
    2022-03-03
  • C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實(shí)例

    C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類實(shí)例

    這篇文章主要介紹了C#實(shí)現(xiàn)的陰歷陽歷互相轉(zhuǎn)化類,結(jié)合實(shí)例形式分析了C#針對日期的轉(zhuǎn)換與計算相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#并發(fā)編程入門教程之概述

    C#并發(fā)編程入門教程之概述

    這篇文章主要給大家介紹了關(guān)于C#并發(fā)編程之概述的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者使用c#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Unity存儲游戲數(shù)據(jù)的多種方法小結(jié)

    Unity存儲游戲數(shù)據(jù)的多種方法小結(jié)

    這篇文章主要介紹了Unity存儲游戲數(shù)據(jù)的幾種方法,在游戲開發(fā)中,存儲游戲數(shù)據(jù)是非常重要的,因?yàn)橛螒驍?shù)據(jù)決定了游戲的各個方面,例如游戲的進(jìn)度、玩家的成就、游戲的設(shè)置,需要的朋友可以參考下
    2023-02-02
  • Unity PC版Log的具體位置介紹

    Unity PC版Log的具體位置介紹

    這篇文章主要介紹了Unity PC版Log的具體位置介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#處理MySql多個返回集的方法

    C#處理MySql多個返回集的方法

    這篇文章主要介紹了C#處理MySql多個返回集的方法,實(shí)現(xiàn)了對處理MySql多個返回集進(jìn)行封裝,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)

    C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)在線點(diǎn)餐系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • C#實(shí)現(xiàn)清空回收站的方法

    C#實(shí)現(xiàn)清空回收站的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)清空回收站的方法,涉及C#系統(tǒng)回收站的清空技巧,非常簡單實(shí)用,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-08-08
  • unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能

    unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲功能

    workerman?是一款開源高性能?PHP?應(yīng)用容器,他除了用于互聯(lián)網(wǎng)、即時通訊、APP?開發(fā)、硬件通訊、智能家居、物聯(lián)網(wǎng)等領(lǐng)域的開發(fā)外,這篇文章主要介紹了unity3d?對接?workerman?實(shí)現(xiàn)聯(lián)機(jī)游戲,需要的朋友可以參考下
    2022-10-10

最新評論