PlayerPrefs를 사용해 클라이언트에서 time(=Score) 저장
- Player
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
float moveSpeed = 5f;
int maxHp = 5;
float delta = 0;
Renderer renderer;
Color rendererColor;
public UICtrl uICtrl;
private void Start()
{
UICtrl uICtrl = GetComponent<UICtrl>();
renderer = this.gameObject.GetComponent<Renderer>();
rendererColor = renderer.material.color; // 기본 칼러 저장
FindObjectOfType<Bullet>();
Bullet bullet = GetComponent<Bullet>();
}
void Update()
{
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
// Debug.Log(moveSpeed);
Vector3 dir = new Vector3(h, 0, v); //방향
Vector3 movement = dir.normalized * this.moveSpeed * Time.deltaTime;
this.transform.Translate(movement);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Finish")
{
// Debug.Log("총 맞은 것처럼♪");
Destroy(other.gameObject);
}
uICtrl.Life();
}
public void DeathColor()
{
// Debug.Log("오냐?");
renderer = this.gameObject.GetComponent<Renderer>();
if (renderer != null)
{
// Debug.Log(renderer.gameObject.name); // 어떤 객체의 랜더러를 불러오는지 이름 출력
renderer.material.color = Color.red;
}
}
public IEnumerator HitColor() // 무한이 아닌 한 번만 실행
{
renderer.material.color = Color.red;
yield return new WaitForSeconds(0.1f);
renderer.material.color = rendererColor;
}
}
- Ground
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ground : MonoBehaviour
{
void Update()
{
transform.Rotate(0, 0.03f, 0);
}
}
- Pillar
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Pillar : MonoBehaviour
{
Vector3 pPiont;
Player player;
void Start()
{
player = FindObjectOfType<Player>();
}
// Update is called once per frame
void Update()
{
pPiont = player.transform.position;
transform.LookAt(pPiont);
}
}
- Bullet
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using UnityEngine;
public class Bullet : MonoBehaviour
{
float speed = 1.1f;
Rigidbody rb;
void Update()
{
rb = GetComponent<Rigidbody>();
rb.velocity = transform.forward * speed;
}
}
- BulletGenerator
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class BulletGenerator : MonoBehaviour
{
Player player;
Vector3 pPiont;
float delta = 0;
public GameObject BulletFrepabs;
void Start()
{
player = FindObjectOfType<Player>();
}
void Update()
{
delta += Time.deltaTime;
// Debug.Log(delta);
if (delta > 2.5)
{
delta = 0;
Instantiate(BulletFrepabs, transform.position, transform.rotation);
BulletFrepabs.transform.LookAt(player.transform);
}
}
}
- UICtrl
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Unity.VisualScripting;
using UnityEngine.SceneManagement;
public class UICtrl : MonoBehaviour
{
public TextMeshProUGUI TimeCount;
public TextMeshProUGUI LifeCount;
public TextMeshProUGUI Score;
int maxHp = 3;
float delta = 0;
public Player player;
public GameObject redFilter;
public GameObject reStart;
void Start()
{
player = FindObjectOfType<Player>();
Score.GetComponent<TextMeshProUGUI>();
Time.timeScale = 1f;
}
void Update()
{
delta += Time.deltaTime;
this.TimeCount.GetComponent<TMP_Text>().text = delta.ToString("Time: 0");
this.LifeCount.GetComponent<TMP_Text>().text = maxHp.ToString($"Life: {maxHp}");
if (Input.GetMouseButtonUp(0) && this.maxHp == 0)
{
// Debug.Log("끝!");
SceneManager.LoadScene("SampleScene");
}
}
public void Life()
{
// Debug.Log("악");
this.maxHp -= 1;
// Debug.Log($"현재 체력: {maxHp}");
player.StartCoroutine(player.HitColor()); //다른 클래스에 포함된 코루틴 실행
if (this.maxHp <= 2)
{
LifeCount.color = Color.yellow;
StartCoroutine(RedFilter());
}
if (maxHp <= 0)
{
// Debug.Log("주금");
player.DeathColor();
float bestTime = PlayerPrefs.GetFloat("BestTime", float.MaxValue); // 최고 스코어 변수 선언
if (this.maxHp <= 0)
{
this.maxHp = 0;
}
if (bestTime < delta)
{
PlayerPrefs.SetFloat("BestTime", delta); // 현재 스코어가 크면 최고 스코어 변수에 현재 값을 넣음
PlayerPrefs.Save(); // 위의 값을 저장
}
// Debug.Log($"best=delta:{bestTime}");
Score.gameObject.SetActive(true); // txt를 게임 오브젝트로 선언해 제어함
this.reStart.SetActive(true);
if (this.delta >= bestTime)
{
this.Score.GetComponent<TMP_Text>().text = delta.ToString("Best Time: 0"); // 생존 시간
Debug.Log($"delta:{delta}");
}else
{
this.Score.GetComponent<TMP_Text>().text = bestTime.ToString("Best Time: 0");
}
Time.timeScale = 0f;
}
}
IEnumerator RedFilter()
{
while (true) // 무한 반복
{
redFilter.gameObject.SetActive(true);
yield return new WaitForSeconds(0.3f);
redFilter.gameObject.SetActive(false);
yield return new WaitForSeconds(0.3f);
}
}
}