유 니 티,작은 지도 와 방향 내 비게 이 션 만 들 기.

6992 단어 Unity지도.항 법
1.유 니 티 방향 내 비게 이 션 제작
디자인 요 구 는 방향 내 비게 이 션 이 마우스 회전 에 따라 방향 을 바 꾸 는 것 이다.효과 도 는 다음 과 같다.

구체 적 인 실현 방법 은 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);
 }
 }
}

2.유 니 티 작은 지도 제작
작은 지도 제작 에 관 해 인터넷 에는 각종 게시 물이 쏟 아 져 나 왔 지만 자세히 보 니 대부분 똑 같 았 고 서로 베 끼 는 것 도 소 용이 없 었 다.각종 게시 물 은 대부분 정교 카메라 의 방식 으로 작은 지 도 를 표시 하지만 이 지 도 는 실제 장면 의 내 려 다 보 는 것 이다.우 리 는 영웅 연맹 과 같은 작은 지도 가 필요 하 다.여기 서 나 는 간단 한 방식 으로 작은 지 도 를 실현 한다.쓸데없는 소리 말고 먼저 효과 도 를 올 려 라.


이곳 의 지 도 는 단지 한 장의 그림 일 뿐,이것 은 지도의 유연성 을 증가 시 켰 다.이곳 의 작은 지 도 는 위의 방향 내 비게 이 션 과 유사 하 며,다른 것 은 스 크 립 트 의 작성 방식 이다.
구체 적 인 실현 도 UI 의 디자인 과 코드 의 작성 으로 두 단계 로 나 뉜 다.
STEP 1:지도 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);
 }
 }
 }

 }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기