Skip to content

Instantly share code, notes, and snippets.

@Islington036
Last active June 19, 2022 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Islington036/989bbd3724e9a79b38fce66e025730c8 to your computer and use it in GitHub Desktop.
Save Islington036/989bbd3724e9a79b38fce66e025730c8 to your computer and use it in GitHub Desktop.
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;
public class ErrorWindow : MonoBehaviour
{
//=============================================================================
// 変数
//=============================================================================
// ウィンドウの本来のポジション
private Vector3 WindowOriginalPos = Vector3.zero;
// ウィンドウを拡大するスピード
[SerializeField, Tooltip("ウィンドウを拡大するスピード")]
private float ChangeSpeed = 40f;
// ウィンドウが揺れる範囲
[SerializeField, Tooltip("ウィンドウを振動させる範囲")]
private float PositionShakeRange = 10f;
// ウィンドウが振動させる時間
[SerializeField, Tooltip("ウィンドウを振動させる時間")]
private float ShakeStopTime = 1.5f;
// ウィンドウの振動が開始してからの経過時間
[SerializeField]
private float ShakeTime = 0;
// ウィンドウの拡大をしているか
private bool isWindowScale = false;
public bool IsWindowScale { get { return isWindowScale; } }
//-------------------------ゲームパッドの振動
//private Gamepad GamePad;
//=============================================================================
// スタート
//=============================================================================
void Start()
{
// ウィンドウ本来のポジションを記録
WindowOriginalPos = transform.position;
// ウィンドウサイズを0にする
transform.localScale = Vector3.zero;
// ゲームパッドの取得
//GamePad = Gamepad.current;
// ウィンドウの表示位置を中央に寄せる
GetComponent<RectTransform>().anchoredPosition = Vector2.zero;
// ウィンドウを拡大
StartCoroutine(WindowScale(this.transform, ChangeSpeed, false));
isWindowScale = true;
// ウィンドウを揺らす
StartCoroutine(WindowShake(this.transform, ShakeStopTime));
}
//=============================================================================
// アップデート
//=============================================================================
private void Update()
{
// Aボタンが押された場合
// InputSystemを使う場合はここを改変してください
if (Input.GetKeyDown("joystick button 0"))
{
// ウィンドウを終了
WindowEnd();
}
}
//=============================================================================
// ウィンドウ終了時
//=============================================================================
private void OnDestroy()
{
// ゲームパッドを停止させる
//if (GamePad != null) { GamePad.SetMotorSpeeds(0.0f, 0.0f); }
}
//=============================================================================
// ウィンドウを縮小
//=============================================================================
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;
// 拡大の場合
if (!reduction)
{
variation = (1.5f - Target.localScale.x) / Speed;
// サイズを拡大
Target.localScale += new Vector3(variation, variation, variation);
// 拡大が終了した場合
if (1 - Target.localScale.x <= 0.01f)
{
isWindowScale = false;
// コルーチンをストップ
yield break;
}
else
{
// 次フレームに送る
yield return null;
}
}
else
{
variation = (Target.localScale.x + 0.5f) / Speed;
// サイズを拡大
Target.localScale -= new Vector3(variation, variation, variation);
// 拡大が終了した場合
if (Target.localScale.x < 0)
{
// オブジェクトを削除
Destroy(this.gameObject);
// コルーチンをストップ
yield break;
}
else
{
// 次フレームに送る
yield return null;
}
}
}
}
//=============================================================================
// 一定の期間、振動させる
//=============================================================================
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;
// ゲームパッドを停止させる
//if (GamePad != null) { GamePad.SetMotorSpeeds(0.0f, 0.0f); }
// コルーチンをストップ
yield break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment