質問編集履歴
2
誤字 書式の修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -44,7 +44,7 @@
|
|
44
44
|
|
45
45
|
(関連ソースコード)
|
46
46
|
|
47
|
-
1. CharacterStatus
|
47
|
+
1. CharacterStatus.cs:
|
48
48
|
|
49
49
|
|
50
50
|
|
@@ -206,6 +206,8 @@
|
|
206
206
|
|
207
207
|
2. CharacterStatusGui.cs:
|
208
208
|
|
209
|
+
|
210
|
+
|
209
211
|
using UnityEngine;
|
210
212
|
|
211
213
|
using System.Collections;
|
1
PlayerのCharacterStatus・lastAttacktargetとCharacterStatusguiを定義しているファイルの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -42,6 +42,370 @@
|
|
42
42
|
|
43
43
|
|
44
44
|
|
45
|
+
(関連ソースコード)
|
46
|
+
|
47
|
+
1. CharacterStatusCharacterStatus.cs:
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
using UnityEngine;
|
52
|
+
|
53
|
+
using System.Collections;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
public class CharacterStatus : MonoBehaviour
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
//---------- 攻撃の章で使用します. ----------
|
64
|
+
|
65
|
+
// 体力.
|
66
|
+
|
67
|
+
public int HP = 100;
|
68
|
+
|
69
|
+
public int MaxHP = 100;
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
// 攻撃力.
|
74
|
+
|
75
|
+
public int Power = 10;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
// 最後に攻撃した対象.
|
80
|
+
|
81
|
+
public GameObject lastAttackTarget = null;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
//---------- GUIおよびネットワークの章で使用します. ----------
|
86
|
+
|
87
|
+
// プレイヤー名.
|
88
|
+
|
89
|
+
public string characterName = "Player";
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
//--------- アニメーションの章で使用します. -----------
|
94
|
+
|
95
|
+
//状態.
|
96
|
+
|
97
|
+
public bool attacking = false;
|
98
|
+
|
99
|
+
public bool died = false;
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
// 攻撃力強化
|
104
|
+
|
105
|
+
public bool powerBoost = false;
|
106
|
+
|
107
|
+
// 攻撃強化時間
|
108
|
+
|
109
|
+
float powerBoostTime = 0.0f;
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
// 攻撃力強化エフェクト
|
114
|
+
|
115
|
+
ParticleSystem powerUpEffect;
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
// アイテム取得
|
120
|
+
|
121
|
+
public void GetItem(DropItem.ItemKind itemKind)
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
switch (itemKind)
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
case DropItem.ItemKind.Attack:
|
130
|
+
|
131
|
+
powerBoostTime = 5.0f;
|
132
|
+
|
133
|
+
powerUpEffect.Play ();
|
134
|
+
|
135
|
+
break;
|
136
|
+
|
137
|
+
case DropItem.ItemKind.Heal:
|
138
|
+
|
139
|
+
// MaxHPの半分回復
|
140
|
+
|
141
|
+
HP = Mathf.Min(HP + MaxHP / 2, MaxHP);
|
142
|
+
|
143
|
+
break;
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
|
151
|
+
void Start()
|
152
|
+
|
153
|
+
{
|
154
|
+
|
155
|
+
if (gameObject.tag == "Player")
|
156
|
+
|
157
|
+
{
|
158
|
+
|
159
|
+
powerUpEffect = transform.Find("PowerUpEffect").GetComponent<ParticleSystem>();
|
160
|
+
|
161
|
+
}
|
162
|
+
|
163
|
+
}
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
void Update()
|
168
|
+
|
169
|
+
{
|
170
|
+
|
171
|
+
if (gameObject.tag != "Player")
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
return;
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
powerBoost = false;
|
180
|
+
|
181
|
+
if (powerBoostTime > 0.0f)
|
182
|
+
|
183
|
+
{
|
184
|
+
|
185
|
+
powerBoost = true;
|
186
|
+
|
187
|
+
powerBoostTime = Mathf.Max(powerBoostTime - Time.deltaTime, 0.0f);
|
188
|
+
|
189
|
+
}
|
190
|
+
|
191
|
+
else
|
192
|
+
|
193
|
+
{
|
194
|
+
|
195
|
+
powerUpEffect.Stop();
|
196
|
+
|
197
|
+
}
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
}
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
2. CharacterStatusGui.cs:
|
208
|
+
|
209
|
+
using UnityEngine;
|
210
|
+
|
211
|
+
using System.Collections;
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
public class CharacterStatusGui : MonoBehaviour
|
216
|
+
|
217
|
+
{
|
218
|
+
|
219
|
+
float baseWidth = 854f;
|
220
|
+
|
221
|
+
float baseHeight = 480f;
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
// ステータス.
|
226
|
+
|
227
|
+
CharacterStatus playerStatus;
|
228
|
+
|
229
|
+
Vector2 playerStatusOffset = new Vector2(8f, 80f);
|
230
|
+
|
231
|
+
|
232
|
+
|
233
|
+
// 名前.
|
234
|
+
|
235
|
+
Rect nameRect = new Rect(0f, 0f, 120f, 24f);
|
236
|
+
|
237
|
+
public GUIStyle nameLabelStyle;
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
// ライフバー.
|
242
|
+
|
243
|
+
public Texture backLifeBarTexture;
|
244
|
+
|
245
|
+
public Texture frontLifeBarTexture;
|
246
|
+
|
247
|
+
float frontLifeBarOffsetX = 2f;
|
248
|
+
|
249
|
+
float lifeBarTextureWidth = 128f;
|
250
|
+
|
251
|
+
Rect playerLifeBarRect = new Rect(0f, 0f, 128f, 16f);
|
252
|
+
|
253
|
+
Color playerFrontLifeBarColor = Color.green;
|
254
|
+
|
255
|
+
Rect enemyLifeBarRect = new Rect(0f, 0f, 128f, 24f);
|
256
|
+
|
257
|
+
Color enemyFrontLifeBarColor = Color.red;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
// プレイヤーステータスの描画.
|
262
|
+
|
263
|
+
void DrawPlayerStatus()
|
264
|
+
|
265
|
+
{
|
266
|
+
|
267
|
+
float x = baseWidth - playerLifeBarRect.width - playerStatusOffset.x;
|
268
|
+
|
269
|
+
float y = playerStatusOffset.y;
|
270
|
+
|
271
|
+
DrawCharacterStatus(
|
272
|
+
|
273
|
+
x, y,
|
274
|
+
|
275
|
+
playerStatus,
|
276
|
+
|
277
|
+
playerLifeBarRect,
|
278
|
+
|
279
|
+
playerFrontLifeBarColor);
|
280
|
+
|
281
|
+
}
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
// 敵ステータスの描画.
|
286
|
+
|
287
|
+
void DrawEnemyStatus()
|
288
|
+
|
289
|
+
{
|
290
|
+
|
291
|
+
if (playerStatus.lastAttackTarget != null)
|
292
|
+
|
293
|
+
{
|
294
|
+
|
295
|
+
CharacterStatus target_status = playerStatus.lastAttackTarget.GetComponent<CharacterStatus>();
|
296
|
+
|
297
|
+
DrawCharacterStatus(
|
298
|
+
|
299
|
+
(baseWidth - enemyLifeBarRect.width) / 2.0f, 0f,
|
300
|
+
|
301
|
+
target_status,
|
302
|
+
|
303
|
+
enemyLifeBarRect,
|
304
|
+
|
305
|
+
enemyFrontLifeBarColor);
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
}
|
310
|
+
|
311
|
+
|
312
|
+
|
313
|
+
// キャラクターステータスの描画.
|
314
|
+
|
315
|
+
void DrawCharacterStatus(float x, float y, CharacterStatus status, Rect bar_rect, Color front_color)
|
316
|
+
|
317
|
+
{
|
318
|
+
|
319
|
+
// 名前.
|
320
|
+
|
321
|
+
GUI.Label(
|
322
|
+
|
323
|
+
new Rect(x, y, nameRect.width, nameRect.height),
|
324
|
+
|
325
|
+
status.characterName,
|
326
|
+
|
327
|
+
nameLabelStyle);
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
float life_value = (float)status.HP / status.MaxHP;
|
332
|
+
|
333
|
+
if(backLifeBarTexture != null)
|
334
|
+
|
335
|
+
{
|
336
|
+
|
337
|
+
// 背面ライフバー.
|
338
|
+
|
339
|
+
y += nameRect.height;
|
340
|
+
|
341
|
+
GUI.DrawTexture(new Rect(x, y, bar_rect.width, bar_rect.height), backLifeBarTexture);
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
// 前面ライフバー.
|
348
|
+
|
349
|
+
if(frontLifeBarTexture != null)
|
350
|
+
|
351
|
+
{
|
352
|
+
|
353
|
+
float resize_front_bar_offset_x = frontLifeBarOffsetX * bar_rect.width / lifeBarTextureWidth;
|
354
|
+
|
355
|
+
float front_bar_width = bar_rect.width - resize_front_bar_offset_x * 2;
|
356
|
+
|
357
|
+
var gui_color = GUI.color;
|
358
|
+
|
359
|
+
GUI.color = front_color;
|
360
|
+
|
361
|
+
GUI.DrawTexture(new Rect(x + resize_front_bar_offset_x, y, front_bar_width * life_value, bar_rect.height), frontLifeBarTexture);
|
362
|
+
|
363
|
+
GUI.color = gui_color;
|
364
|
+
|
365
|
+
}
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
|
370
|
+
|
371
|
+
void Awake()
|
372
|
+
|
373
|
+
{
|
374
|
+
|
375
|
+
PlayerCtrl player_ctrl = GameObject.FindObjectOfType(typeof(PlayerCtrl)) as PlayerCtrl;
|
376
|
+
|
377
|
+
playerStatus = player_ctrl.GetComponent<CharacterStatus>();
|
378
|
+
|
379
|
+
}
|
380
|
+
|
381
|
+
|
382
|
+
|
383
|
+
void OnGUI()
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
// 解像度対応.
|
388
|
+
|
389
|
+
GUI.matrix = Matrix4x4.TRS(
|
390
|
+
|
391
|
+
Vector3.zero,
|
392
|
+
|
393
|
+
Quaternion.identity,
|
394
|
+
|
395
|
+
new Vector3(Screen.width / baseWidth, Screen.height / baseHeight, 1f));
|
396
|
+
|
397
|
+
|
398
|
+
|
399
|
+
// ステータス.
|
400
|
+
|
401
|
+
DrawPlayerStatus();
|
402
|
+
|
403
|
+
DrawEnemyStatus();
|
404
|
+
|
405
|
+
}
|
406
|
+
|
407
|
+
}
|
408
|
+
|
45
409
|
|
46
410
|
|
47
411
|
### 試したこと
|