Skip to content

Instantly share code, notes, and snippets.

@thorikawa
Created August 20, 2018 09:11
Show Gist options
  • Save thorikawa/83186ed3fe232c32dc2e0e2547495522 to your computer and use it in GitHub Desktop.
Save thorikawa/83186ed3fe232c32dc2e0e2547495522 to your computer and use it in GitHub Desktop.
Unityで状態管理を行うための汎用クラス
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public delegate void OnStateIsLasting(LastingStateDetector sender, float lastingTime);
public delegate void OnStateEnter(LastingStateDetector sender);
public class LastingStateDetector
{
public OnStateIsLasting OnStateOKIsLasting;
public OnStateIsLasting OnStateNGIsLasting;
public OnStateEnter OnStateOKEnter;
public OnStateEnter OnStateNGEnter;
public float deltaTime;
public object Data;
public object lastData;
private Func<bool> checkFunc;
private bool isCheckOk = false;
private float checkOkBeginingTime = -1;
private float checkNgBeginingTime = -1;
private float previousTime;
// Use this for initialization
public LastingStateDetector(Func<bool> f)
{
checkFunc = f;
}
// This method is expected to be called in MonoBehavior's Update()
public void Update()
{
if (checkFunc())
{
if (isCheckOk)
{
// ok state is lasting...
OnStateOKIsLasting(this, Time.time - checkOkBeginingTime);
}
else
{
// it starts lasting
checkOkBeginingTime = Time.time;
OnStateOKEnter(this);
}
isCheckOk = true;
}
else
{
if (isCheckOk)
{
checkNgBeginingTime = Time.time;
OnStateNGEnter(this);
}
else
{
OnStateNGIsLasting(this, Time.time - checkNgBeginingTime);
}
isCheckOk = false;
}
deltaTime = Time.time - previousTime;
previousTime = Time.time;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Sample : MonoBehaviour
{
private LastingStateDetector mouseIsInsideScreen;
// Use this for initialization
void Start()
{
var screenRect = new Rect(0, 0, Screen.width, Screen.height);
mouseIsInsideScreen = new LastingStateDetector(() =>
{
return screenRect.Contains(Input.mousePosition);
});
mouseIsInsideScreen.OnStateOKEnter += MouseIsInsideScreen_OnStateOKEnter;
mouseIsInsideScreen.OnStateNGEnter += MouseIsInsideScreen_OnStateNGEnter;
mouseIsInsideScreen.OnStateOKIsLasting += MouseIsInsideScreen_OnStateOKIsLasting;
mouseIsInsideScreen.OnStateNGIsLasting += MouseIsInsideScreen_OnStateNGIsLasting;
}
// Update is called once per frame
void Update()
{
mouseIsInsideScreen.Update();
}
void MouseIsInsideScreen_OnStateOKEnter(LastingStateDetector sender)
{
Debug.Log("mouse just entered in a screen.");
}
void MouseIsInsideScreen_OnStateNGEnter(LastingStateDetector sender)
{
Debug.Log("mouse just left from a screen.");
}
void MouseIsInsideScreen_OnStateOKIsLasting(LastingStateDetector sender, float lastingTime)
{
Debug.LogFormat("mouse is inside a screen for {0} sec.", lastingTime);
}
void MouseIsInsideScreen_OnStateNGIsLasting(LastingStateDetector sender, float lastingTime)
{
Debug.LogFormat("mouse is outside a screen for {0} sec.", lastingTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment