一、unity方向導航制作
設計要求是方向導航隨著鼠標旋轉轉換方向,效果圖如下:
具體的實現(xiàn)方法主要有兩個步驟,分別為ui設計和腳本編寫。我的設計思路是這個控件分為兩層,第一層為東西南北指示層,第二層為圖標指示層,這里我的圖標采用圓形圖標,方向指示這里采用控制圖標旋轉的方式實現(xiàn),層級關系如下:
首先創(chuàng)建父節(jié)點1,然后在父節(jié)點下創(chuàng)建子節(jié)點2,3;最后調整好位置。
第二步腳本編寫,腳本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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)){ //按住鼠標左鍵才能調節(jié)角度,根據(jù)鼠標移動的快慢(增量), 獲得相機左右旋轉的角度(處理x) float rotationx = transform.localeulerangles.y + input.getaxis ( "mouse x" ) * sensitivityx; //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉的角度(處理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小地圖的制作
關于小地圖的制作,網(wǎng)上各種帖子鋪天蓋地,然而仔細看卻發(fā)現(xiàn)大部分都一樣,互相抄襲,很多都是沒用的。各種帖子大都采用是正交相機的方式顯示小地圖,然而這個地圖是真實場景的俯視,我們需要的往往是像英雄聯(lián)盟那樣的小地圖,這里我采用一種簡單的方式實現(xiàn)小地圖。廢話不說先上效果圖:
這里的地圖只是一張圖片,這增加了地圖的靈活性,這里的小地圖創(chuàng)建跟上面方向導航類似,所不同的是腳本的編寫方式。
具體的實現(xiàn)也是分為兩個步驟,分別為ui的設計和代碼的編寫。
第一步:地圖ui的設計
層級關系如圖:
父節(jié)點1為背景地圖,子節(jié)點2為藍色箭頭,藍色箭頭表示目標目前所在的位置。這兩個節(jié)點僅僅是圖片控件。
第二步:腳本的編寫
腳本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
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)){ //根據(jù)鼠標移動的快慢(增量), 獲得相機左右旋轉的角度(處理x) float rotationx = transform.localeulerangles.y + input.getaxis ( "mouse x" ) * sensitivityx; //根據(jù)鼠標移動的快慢(增量), 獲得相機上下旋轉的角度(處理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); } } } } } |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/u010989951/article/details/52316578