방치형 보상
- DateTime.Now 로 저장 시간을 구해서 PlayerPrefs로 로컬 저장
- DateTime.Now로 현재 시간을 구해서 종료 후, 경과한 시간을 구해서 시간에 따른 보상 지급
구현 방법
스크립트 작성
DataManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;
public class DataManagerJaeMin : MonoBehaviour
{
[Header("Reward")]
[SerializeField] private GameObject _rewardMenu;
[SerializeField] private TMP_Text _coinRewardText;
[SerializeField] private TMP_Text _woodRewardText;
[SerializeField] private TMP_Text _bulletRewardText;
[SerializeField] private TMP_Text _cannonRewardText;
private int _coinSecond = 1;
private int _woodSecond = 10;
private int _bulletSecond = 10;
private int _cannonSecond = 10;
private int _coinReward;
private int _woodReward;
private int _bulletReward;
private int _cannonReward;
private DateTime _lastTime;
private DateTime _currentTime;
private TimeSpan _timeSpan;
private double _rewardTime;
private PlayerController _playerController;
private PlayerConditions _playerConditions;
private ResourceCollector _resourceCollector;
// 초기화
public void Init()
{
_playerController = GameManager.I.PlayerManager.Player.GetComponent<PlayerController>();
_playerConditions = GameManager.I.PlayerManager.Player.GetComponent<PlayerConditions>();
_resourceCollector = GameManager.I.PlayerManager.Player.GetComponent<ResourceCollector>();
if (PlayerPrefs.HasKey("LastTime"))
{
_lastTime = DateTime.Parse(PlayerPrefs.GetString("LastTime"));
_currentTime = DateTime.Now;
_timeSpan = _currentTime - _lastTime;
_rewardTime = _timeSpan.TotalSeconds;
}
}
// 메모리 해제
public void Release()
{
}
public void DataSave()
{
// 마지막 저장 시, 시간 저장
PlayerPrefs.SetString("LastTime", DateTime.Now.ToString());
}
private void RewardActive()
{
_rewardMenu.SetActive(true);
_coinReward = (int)(_rewardTime / _coinSecond) * _playerController.PartnerSpawnCount;
_woodReward = (int)(_rewardTime / _woodSecond) * _playerController.PartnerSpawnCount;
_bulletReward = (int)(_rewardTime / _bulletSecond) * _playerController.PartnerSpawnCount;
_cannonReward = (int)(_rewardTime / _cannonSecond) * _playerController.PartnerSpawnCount;
_coinRewardText.text = _coinReward.ToString();
_woodRewardText.text = _woodReward.ToString();
_bulletRewardText.text = _bulletReward.ToString();
_cannonRewardText.text = _cannonReward.ToString();
}
public void GetReward()
{
_playerController.CurrentCoin = PlayerPrefs.GetInt("SaveCoin") + _coinReward;
_resourceCollector.Resources = PlayerPrefs.GetInt("SaveWood") + _woodReward;
_playerController.GunController.CurrentGun.CarryBulletCount = PlayerPrefs.GetInt("SaveCarryBullet") + _bulletReward;
_playerController.CannonBall = PlayerPrefs.GetInt("SaveCannonBall") + _cannonReward;
_playerController.CurrentCoinText.text = _playerController.CurrentCoin.ToString();
_resourceCollector.UpdateResourceText();
_playerController.CannonBallText.text = _playerController.CannonBall.ToString();
_rewardMenu.SetActive(false);
}
}
'유니티' 카테고리의 다른 글
Resources 폴더 (0) | 2024.03.19 |
---|---|
롱클릭 구현 (Event Trigger) (0) | 2024.03.19 |
게임 종료 (0) | 2024.02.07 |
로딩 씬 구현 (0) | 2024.02.01 |
튜토리얼 (1) | 2024.01.31 |