Last active
June 19, 2022 12:06
-
-
Save Islington036/c5a911d4c71f2f3ccbe8c347b28d0f51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.InputSystem; | |
using UnityEngine.UI; | |
public class WarningWindow : MonoBehaviour | |
{ | |
//============================================================================= | |
// 列挙型 | |
//============================================================================= | |
// 選択肢 | |
private enum SelectItemList | |
{ | |
Yes, | |
No, | |
LIMIT | |
} | |
// ここに役割別の処理を書いていく | |
private enum RoleList | |
{ | |
GameEnd // ゲーム終了 | |
} | |
//============================================================================= | |
// 変数 | |
//============================================================================= | |
//------------------------------ウィンドウの役割 | |
[SerializeField, Tooltip("ウィンドウの役割")] | |
private RoleList role = RoleList.GameEnd; | |
//------------------------------ウィンドウの表示 | |
// ウィンドウの本来のポジション | |
private Vector3 WindowOriginalPos = Vector3.zero; | |
// ウィンドウを拡大するスピード | |
[SerializeField, Tooltip("ウィンドウを拡大するスピード")] | |
private float ChangeSpeed = 40f; | |
// ウィンドウが揺れる範囲 | |
[SerializeField, Tooltip("ウィンドウを振動させる範囲")] | |
private float PositionShakeRange = 10f; | |
// ウィンドウが振動させる時間 | |
[SerializeField, Tooltip("ウィンドウを振動させる時間")] | |
private float ShakeStopTime = 0.5f; | |
// ウィンドウの振動が開始してからの経過時間 | |
private float ShakeTime = 0; | |
//------------------------------選択項目の切り替え | |
// 選択している項目 | |
[SerializeField] | |
private SelectItemList SelectItem = SelectItemList.Yes; | |
// 選択項目 | |
[SerializeField, Tooltip("「はい」の選択項目")] | |
private Image SelectItemYes = null; | |
[SerializeField, Tooltip("「いいえ」の選択項目")] | |
private Image SelectItemNo = null; | |
// 選択項目の色 | |
[SerializeField, Tooltip("選択項目の色")] | |
private Color SelectItemColor = Color.red; | |
// 非選択項目の色 | |
[SerializeField, Tooltip("非選択項目の色")] | |
private Color NotSelectedItemColor = Color.black; | |
// 「はい」を選択した場合 | |
[SerializeField, ReadOnly] | |
private bool SelectON = false; | |
//-------------------------ゲームパッドの振動 | |
//private Gamepad GamePad; | |
//------------------------------入力制御 | |
// 連続入力制御 | |
private bool InputON = true; | |
// 入力待ち時間 | |
[SerializeField, RenameField("入力の待ち時間")] | |
private const float StandbyTime = 0.2f; | |
//============================================================================= | |
// スタート | |
//============================================================================= | |
void Start() | |
{ | |
// ウィンドウ本来のポジションを記録 | |
WindowOriginalPos = transform.position; | |
// ウィンドウサイズを0にする | |
transform.localScale = Vector3.zero; | |
// ゲームパッドの取得 | |
//GamePad = Gamepad.current; | |
// 選択項目の色の切り替え | |
ChangeSelectItemColor(); | |
// ウィンドウの表示位置を中央に寄せる | |
GetComponent<RectTransform>().anchoredPosition = Vector2.zero; | |
// ウィンドウを拡大 | |
StartCoroutine(WindowScale(this.transform, ChangeSpeed, false)); | |
// ウィンドウを揺らす | |
StartCoroutine(WindowShake(this.transform, ShakeStopTime)); | |
// SEを再生 | |
SoundManager.Instance.PlaySE("se_outgame_05_error_1"); | |
} | |
//============================================================================= | |
// アップデート | |
//============================================================================= | |
private void Update() | |
{ | |
// ウィンドウの表示位置を中央に寄せる | |
GetComponent<RectTransform>().anchoredPosition = Vector2.zero; | |
// 選択項目の切り替え | |
ChangeSelectItem(); | |
// 選択項目を切り替え | |
ApplySelectItem(); | |
} | |
//============================================================================= | |
// ウィンドウ終了時 | |
//============================================================================= | |
private void OnDestroy() | |
{ | |
// ゲームパッドを停止させる | |
//if (GamePad != null) { GamePad.SetMotorSpeeds(0.0f, 0.0f); } | |
} | |
//============================================================================= | |
// 選択項目の切り替え | |
//============================================================================= | |
private void ChangeSelectItem() | |
{ | |
// 左キーが押された場合 | |
if (MenuInputHelper.LeftKeyInput(0.75f) && InputON) | |
{ | |
// カーソル入力音 | |
SoundManager.Instance.PlaySE("se_outgame_01_cursor"); | |
// 選択中の項目を一つ上に持っていく | |
SelectItem = SelectItem - 1; | |
if ((int)SelectItem < 0) | |
{ | |
// 0を超えた場合は一番下へと移動 | |
SelectItem = SelectItemList.LIMIT - 1; | |
} | |
// 選択項目の色の切り替え | |
ChangeSelectItemColor(); | |
// 連続入力を制限する | |
InputON = false; | |
// 入力待ち時間を設定 | |
StartCoroutine(WaitInput(StandbyTime)); | |
} | |
// 下キーが押された場合 | |
if (MenuInputHelper.RightKeyInput(0.75f) && InputON) | |
{ | |
// カーソル入力音 | |
SoundManager.Instance.PlaySE("se_outgame_01_cursor"); | |
// 選択中の項目を一つ上に持っていく | |
SelectItem = SelectItem + 1; | |
if ((int)SelectItem == (int)SelectItemList.LIMIT) | |
{ | |
// 最大値に到達した場合は最初へと移動 | |
SelectItem = 0; | |
} | |
// 選択項目の色の切り替え | |
ChangeSelectItemColor(); | |
// 連続入力を制限する | |
InputON = false; | |
// 入力待ち時間を設定 | |
StartCoroutine(WaitInput(StandbyTime)); | |
} | |
} | |
//============================================================================= | |
// 選択項目を決定・ウィンドウを閉じる | |
//============================================================================= | |
private void ApplySelectItem() | |
{ | |
// Aボタンを押した場合 | |
if (Input.GetKeyDown("joystick button 0")) | |
{ | |
// 決定音を再生 | |
SoundManager.Instance.PlaySE("se_menu_01_decision_2"); | |
if(SelectItem == SelectItemList.Yes) | |
{ | |
// アイテム選択を有効化 | |
SelectON = true; | |
} | |
else | |
{ | |
// アイテム選択を無効化 | |
SelectON = false; | |
} | |
// ウィンドウを閉じる | |
WindowEnd(); | |
} | |
// Bボタンを押した場合 | |
else if (Input.GetKeyDown("joystick button 1")) | |
{ | |
// 決定音を再生 | |
SoundManager.Instance.PlaySE("se_menu_01_decision_2"); | |
// アイテム削除を無効化 | |
SelectON = false; | |
// ウィンドウを閉じる | |
WindowEnd(); | |
} | |
} | |
//============================================================================= | |
// 選択項目の色切り替え | |
//============================================================================= | |
private void ChangeSelectItemColor() | |
{ | |
if (SelectItem == SelectItemList.Yes) | |
{ | |
// 選択項目の色を切り替え | |
SelectItemYes.color = SelectItemColor; | |
// 非選択項目の色を切り替え | |
SelectItemNo.color = NotSelectedItemColor; | |
} | |
else | |
{ | |
// 選択項目の色を切り替え | |
SelectItemNo.color = SelectItemColor; | |
// 非選択項目の色を切り替え | |
SelectItemYes.color = NotSelectedItemColor; | |
} | |
} | |
//============================================================================= | |
// ウィンドウを縮小 | |
//============================================================================= | |
public void WindowEnd() | |
{ | |
// ウィンドウの拡大を終了 | |
StopCoroutine(WindowScale(this.transform, ChangeSpeed, false)); | |
// ゲームパッドを停止させる | |
//if (GamePad != null) { GamePad.SetMotorSpeeds(0.0f, 0.0f); } | |
// ウィンドウを縮小 | |
StartCoroutine(WindowScale(this.transform, ChangeSpeed, true)); | |
} | |
//============================================================================= | |
// ウィンドウサイズを拡大・縮小 | |
//============================================================================= | |
private IEnumerator WindowScale(Transform Target, float Speed, bool reduction) | |
{ | |
while (true) | |
{ | |
// 変化量を設定 | |
float variation = 1 / Speed; | |
// 拡大の場合 | |
if (!reduction) | |
{ | |
// サイズを拡大 | |
Target.localScale += new Vector3(variation, variation, variation); | |
// 拡大が終了した場合 | |
if (Target.localScale.x > 1) | |
{ | |
// コルーチンをストップ | |
yield break; | |
} | |
else | |
{ | |
// 次フレームに送る | |
yield return null; | |
} | |
} | |
else | |
{ | |
// サイズを拡大 | |
Target.localScale -= new Vector3(variation, variation, variation); | |
// 拡大が終了した場合 | |
if (Target.localScale.x < 0) | |
{ | |
// 「はい」を選択した場合 | |
if (SelectON) | |
{ | |
// 終了時の処理を行う | |
EndProcess(); | |
} | |
// オブジェクトを削除 | |
Destroy(this.gameObject); | |
// コルーチンをストップ | |
yield break; | |
} | |
else | |
{ | |
// 次フレームに送る | |
yield return null; | |
} | |
} | |
} | |
} | |
//============================================================================= | |
// 終了時に行う処理 | |
//============================================================================= | |
private void EndProcess() | |
{ | |
// ここに役割別の処理を書いていく | |
switch (role) | |
{ | |
// ゲームを終了する場合 | |
case RoleList.GameEnd: | |
#if UNITY_EDITOR | |
// エディター上で起動している場合の処理 | |
UnityEditor.EditorApplication.isPlaying = false; | |
#else | |
// アプリ上で動いている場合の処理 | |
Application.Quit(); | |
#endif | |
break; | |
} | |
} | |
//============================================================================= | |
// 一定の期間、振動させる | |
//============================================================================= | |
private IEnumerator WindowShake(Transform Target, float sec) | |
{ | |
while (true) | |
{ | |
// 経過時間を計測 | |
ShakeTime += Time.deltaTime; | |
// ランダムで範囲を指定して揺らす | |
Target.transform.position = WindowOriginalPos + Random.insideUnitSphere * PositionShakeRange; | |
// ゲームパッドを振動させる | |
//if (GamePad != null) { GamePad.SetMotorSpeeds(1.0f, 1.0f); } | |
// 経過時間が引数未満の場合 | |
if (ShakeTime < sec) | |
{ | |
// 次フレームに送って処理を継続 | |
yield return null; | |
} | |
else | |
{ | |
// 経過時間をリセット | |
ShakeTime = 0; | |
// コルーチンをストップ | |
yield break; | |
} | |
} | |
} | |
//============================================================================= | |
// 入力待ち | |
//============================================================================= | |
private IEnumerator WaitInput(float sec) | |
{ | |
yield return new WaitForSeconds(sec); | |
// 入力を可能にする | |
InputON = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment