国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Unity制作小地圖和方向導航

2020-01-24 00:09:31
字體:
來源:轉載
供稿:網友

一、unity方向導航制作

設計要求是方向導航隨著鼠標旋轉轉換方向,效果圖如下:

具體的實現方法主要有兩個步驟,分別為UI設計和腳本編寫。我的設計思路是這個控件分為兩層,第一層為東西南北指示層,第二層為圖標指示層,這里我的圖標采用圓形圖標,方向指示這里采用控制圖標旋轉的方式實現,層級關系如下:

首先創建父節點1,然后在父節點下創建子節點2,3;最后調整好位置。

第二步腳本編寫,腳本如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;public class shijiao : MonoBehaviour{ public GameObject Gcanvas; public GameObject UIzhinanpicture; public GameObject Terren; public GameObject SMAP; //public GameObject bnt=GameObject.Find("Button"); //方向靈敏度  public float sensitivityX = 10F;  public float sensitivityY = 10F;  //上下最大視角(Y視角)  public float minimumY = -60F;  public float maximumY = 60F; float rotationY = 0F;  static public bool ifcanvas; void Update() {  if(Input.GetMouseButton (0)){  //按住鼠標左鍵才能調節角度,根據鼠標移動的快慢(增量), 獲得相機左右旋轉的角度(處理X)   float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;   //根據鼠標移動的快慢(增量), 獲得相機上下旋轉的角度(處理Y)   rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;   //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value   rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);   //總體設置一下相機角度   transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);   UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);  } }}

二、unity小地圖的制作

關于小地圖的制作,網上各種帖子鋪天蓋地,然而仔細看卻發現大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機的方式顯示小地圖,然而這個地圖是真實場景的俯視,我們需要的往往是像英雄聯盟那樣的小地圖,這里我采用一種簡單的方式實現小地圖。廢話不說先上效果圖:

這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創建跟上面方向導航類似,所不同的是腳本的編寫方式。

具體的實現也是分為兩個步驟,分別為UI的設計和代碼的編寫。

第一步:地圖UI的設計

層級關系如圖:

父節點1為背景地圖,子節點2為藍色箭頭,藍色箭頭表示目標目前所在的位置。這兩個節點僅僅是圖片控件。

第二步:腳本的編寫

腳本如下:

using UnityEngine;using System.Collections;using UnityEngine.UI;using System;using UnityEngine.EventSystems;public class shijiao : MonoBehaviour{ public GameObject Gcanvas;//畫布 public GameObject UIzhinanpicture; public GameObject Terren;//大地 public GameObject SMAP;//小地圖指針 public GameObject SMAPBK;//小地圖背景 GameObject Cm; //方向靈敏度  public float sensitivityX = 10F;  public float sensitivityY = 10F;  //上下最大視角(Y視角)  public float minimumY = -60F;  public float maximumY = 60F; //山地的大小 float Twidth; float Tlongth; //地圖大小 float mapwidth; float maplongth; //比例大小 static public float widthScale; static public float longthscal; //圖片縮放比例 //比例大小 //static public float PwidthScale; //static public float Plongthscal; float rotationY = 0F;  static public bool ifcanvas; private float movespeed = 20; CharacterController ctrlor; void Start () { RenderSettings.fog = false; ifcanvas =true;  Gcanvas.SetActive (ifcanvas); Cm = GameObject.Find("Mcam"); ctrlor = GetComponent<CharacterController>(); Twidth=Terren.GetComponent<Collider>().bounds.size.x; Tlongth =Terren.GetComponent<Collider>().bounds.size.z; mapwidth = SMAPBK.GetComponent<RectTransform>().rect.width; maplongth = SMAPBK.GetComponent<RectTransform>().rect.height; widthScale =(mapwidth) /Twidth; longthscal =(maplongth) /Tlongth; SMAP.transform.localPosition= new Vector3(Cm.transform.position.x* widthScale- 50, Cm.transform.position.z* longthscal-50,0); } void Update() { if (Input.GetMouseButton (1)) {  ifcanvas = true;  Gcanvas.SetActive (ifcanvas);  }  else{  if (Input.GetKey(KeyCode.Escape))  {    ifcanvas = false;  Gcanvas.SetActive (ifcanvas);  }  if (!EventSystem.current.IsPointerOverGameObject())  {  //W鍵前進  if (Input.GetKey (KeyCode.W)) {   Vector3 forward = transform.TransformDirection(Vector3.forward);   ctrlor.Move(forward*movespeed*Time.deltaTime);  }  //S鍵后退  if (Input.GetKey(KeyCode.S))  {   Vector3 back = transform.TransformDirection(Vector3.back);   ctrlor.Move(back * movespeed * Time.deltaTime);  }  //A鍵移動  if (Input.GetKey(KeyCode.A))  {   Vector3 left = transform.TransformDirection(Vector3.left);   ctrlor.Move(left* movespeed * Time.deltaTime);  }  //D鍵后退  if (Input.GetKey(KeyCode.D) && gameObject.transform.position.y > 0)  {   Vector3 right = transform.TransformDirection(Vector3.right);   ctrlor.Move(right * movespeed * Time.deltaTime);  }  //E鍵升高  if (Input.GetKey (KeyCode.E)) {   Vector3 upward = transform.TransformDirection(Vector3.up);   ctrlor.Move(upward * movespeed * Time.deltaTime);  }  SMAP.transform.localPosition = new Vector3(Cm.transform.position.x * widthScale - 50, Cm.transform.position.z * longthscal - 50, 0);  if (Input.GetMouseButton (0)){  //根據鼠標移動的快慢(增量), 獲得相機左右旋轉的角度(處理X)   float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;   //根據鼠標移動的快慢(增量), 獲得相機上下旋轉的角度(處理Y)   rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;   //角度限制. rotationY小于min,返回min. 大于max,返回max. 否則返回value   rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);   //總體設置一下相機角度   transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);   UIzhinanpicture.transform.localEulerAngles = new Vector3(0,0,- rotationX);   SMAP.transform.localEulerAngles = new Vector3(0, 0, -rotationX);  }  }  } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 栾城县| 大悟县| 余庆县| 汉川市| 盖州市| 阳泉市| 宁都县| 英德市| 沙坪坝区| 东乌珠穆沁旗| 柳林县| 萍乡市| 克什克腾旗| 寻甸| 离岛区| 重庆市| 周口市| 棋牌| 泉州市| 阿瓦提县| 绥江县| 韩城市| 黄骅市| 庆阳市| 蒙城县| 确山县| 许昌市| 霞浦县| 盱眙县| 石渠县| 黄陵县| 开封市| 广东省| 塔城市| 淳化县| 沙河市| 霍山县| 合江县| 天气| 高平市| 巨鹿县|