//tips
//カメラの切り替わりと切り替わり時間の観測を行う
コルーチンで3秒後にカメラを自動切り替えにし、その3秒の時間経過をsliderで表示する。
SliderではなくスクリプトがアタッチされているHPBarCtrlの方を非アクティブ化する。
時間間隔に不安になったらDebug.LogでTime.timeを計測する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraController4 : MonoBehaviour
{
public GameObject mainCam;
public GameObject subCam;
public GameObject HPBarCtrl;
void Start()
{
mainCam.SetActive(true);
subCam.SetActive(false);
HPBarCtrl.SetActive(false);
}
public void Pushsky()
{
StartCoroutine("Pushcam");
}
IEnumerator Pushcam()
{
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
Debug.Log("Started Coroutine at timestamp : " + Time.time);
yield return new WaitForSeconds(3);
Debug.Log("Finished Coroutine at timestamp : " + Time.time);
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HPBarctrl : MonoBehaviour
{
Slider _slider;
float _hp = 3;
private float timeleft;
void Start()
{
// スライダーを取得する
_slider = GameObject.Find("Slider").GetComponent<Slider>();
Debug.Log("Started HP at timestamp : " + Time.time);
}
void Update()
{
timeleft = Time.deltaTime;
_hp -= timeleft;
if (_hp <= 0)
{
// 最大を超えたら0に戻す
_hp = 0;
}
// HPゲージに値を設定
_slider.value = _hp;
}
}
//再度クリックしたときにバーの値を回復する
再度クリックさせたときにバーの値をもとに戻すためにOnDisableメソッドを使用し、hpの値をもとに戻す。
OnDisableメソッドはゲームオブジェクトが非アクティブになったときに呼ばれる。
アクティブになったときに呼びたい場合はOnEnableメソッドを使用する。
https://gametukurikata.com/basic/enabledisabledestroy
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class HPBarctrl : MonoBehaviour
{
Slider _slider;
float _hp = 3;
private float timeleft;
void Start()
{
// スライダーを取得する
_slider = GameObject.Find("Slider").GetComponent<Slider>();
}
void OnDisable()
{
_hp = 3;
}
void Update()
{
timeleft = Time.deltaTime;
_hp -= timeleft;
// HPゲージに値を設定
_slider.value = _hp;
if (_hp <= 0)
{
_hp = 0;
}
}
//void OnEnable()
}
//一定時間ボタンのクリックをさせない
カメラの切り替え後一定時間ボタンの切り替えを行わないようにするために、ボタンの無効を組み込む。ボタンの有効/無効は、Buttonコンポーネントの Interactable を使用する。
コルーチンとbtn.interactable = false/true;で対応する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraController4 : MonoBehaviour
{
public GameObject mainCam;
public GameObject subCam;
public GameObject HPBarCtrl;
public GameObject Button;
Button btn;
void Start()
{
mainCam.SetActive(true);
subCam.SetActive(false);
HPBarCtrl.SetActive(false);
btn = Button.GetComponent<Button>();
}
public void Pushsky()
{
StartCoroutine("Pushcam");
}
IEnumerator Pushcam()
{
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
yield return new WaitForSeconds(3);
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
btn.interactable = false;
yield return new WaitForSeconds(3);
btn.interactable = true;
}
}
//hpバーの表示を回復させる
カメラ切り替えの制限時間終了後、クリック後hpバーが0表示(赤表示)のまま、元のメインカメラの状態に戻ってしまうので、メインカメラに戻る際にはsliderのvalue値を元の値(緑表示)に直す。
Sliderに別途下記のスクリプトを追加する。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SliderRef : MonoBehaviour
{
Slider hpSlider;
void Start()
{
hpSlider = GetComponent<Slider>();
}
void Update()
{
if (hpSlider.value ==0 )
{
hpSlider.value = 1;
}
}
}
//ポイントがある時のみカメラの切り替えを行う
切り替えを行うためのポイントを設け、ポイントがある時のみ、カメラ切り替えをできるようにする。
変数public int point_numを作成し、PlayerPrefsを行い記録を保存する。
ポイントが0の時は切り替えが行いように処理。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraController4 : MonoBehaviour
{
public GameObject mainCam;
public GameObject subCam;
public GameObject HPBarCtrl;
public GameObject Button;
Button btn;
public int point_num = 2; // スコア変数
void Start()
{
mainCam.SetActive(true);
subCam.SetActive(false);
HPBarCtrl.SetActive(false);
btn = Button.GetComponent<Button>();
point_num = PlayerPrefs.GetInt("CPoint", 2);
}
public void Pushsky()
{
StartCoroutine("Pushcam");
}
IEnumerator Pushcam()
{
point_num = PlayerPrefs.GetInt("CPoint", 2);
Debug.Log(point_num);
if (point_num > 0)
{
point_num -= 1;
Debug.Log(point_num);
PlayerPrefs.SetInt("CPoint", point_num);
PlayerPrefs.Save();
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
yield return new WaitForSeconds(3);
mainCam.SetActive(!mainCam.activeSelf);
subCam.SetActive(!subCam.activeSelf);
HPBarCtrl.SetActive(!HPBarCtrl.activeSelf);
btn.interactable = false;
yield return new WaitForSeconds(3);
btn.interactable = true;
}
}
}
//切り替えポイントの表示
カメラ切り替えをする上で必要なポイントを数値化し、表示する。
point_num = PlayerPrefs.GetInt("CPoint", 2);をテキストで表示できるように、新たなimageおよびテキストとIPointManagerとしてスクリプトを書き、制御する。
また、PlayerPrefsの一部記録を初期化したい場合は、PlayerPrefs.DeleteKey("CPoint”);などとし、CPointのデータのみ初期化することができる。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class IPointManager : MonoBehaviour
{
[SerializeField] UnityEngine.UI.Text textbox;
public int point_num;
void Start()
{
point_num = PlayerPrefs.GetInt("CPoint", 2);
//text = textbox.GetComponent<Text>().text;
textbox.text = point_num.ToString();
Debug.Log(textbox.text);
}
void Update()
{
point_num = PlayerPrefs.GetInt("CPoint", 2);
textbox.text = point_num.ToString();
Debug.Log(textbox.text);
}
}