디바이스의 로컬 시간을 기준으로 특정 시간 간격으로 하트 갯수를 충전하는 스크립트이다. 유저가 마지막으로 게임을 이탈, 종료했던 시간을 저장해 두었다가, 게임에 재진입 했을 때 이탈 했던 시간과 현재의 시간차를 계산하여 하트를 충전해 주고. 만약 하트가 최대값이 아니라면 타이머를 돌려 계속 하트가 충전되도록 한다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
using System;
using System.Collections;
using UnityEngine;
public class HeartRechargeManager : MonoBehaviour
{
#region Heart
/* 화면에 표시하기 위한 UI변수. NGUI가 있다면 사용가능
public UILabel appQuitTimeLabel = null;
public UILabel heartRechargeTimer = null;
public UILabel heartAmountLabel = null;
*/
private int m_HeartAmount = 0; //보유 하트 개수
private DateTime m_AppQuitTime = new DateTime(1970, 1, 1).ToLocalTime();
private const int MAX_HEART = 5; //하트 최대값
public int HeartRechargeInterval = 30;// 하트 충전 간격(단위:초)
private Coroutine m_RechargeTimerCoroutine = null;
private int m_RechargeRemainTime = 0;
#endregion
private void Awake()
{
Init();
}
//게임 초기화, 중간 이탈, 중간 복귀 시 실행되는 함수
public void OnApplicationFocus(bool value)
{
Debug.Log("OnApplicationFocus() : " + value);
if (value)
{
LoadHeartInfo();
LoadAppQuitTime();
SetRechargeScheduler();
}
else
{
SaveHeartInfo();
SaveAppQuitTime();
}
}
//게임 종료 시 실행되는 함수
public void OnApplicationQuit()
{
Debug.Log("GoodsRechargeTester: OnApplicationQuit()");
SaveHeartInfo();
SaveAppQuitTime();
}
//버튼 이벤트에 이 함수를 연동한다.
public void OnClickUseHeart()
{
Debug.Log("OnClickUseHeart");
UseHeart();
}
public void Init()
{
m_HeartAmount = 0;
m_RechargeRemainTime = 0;
m_AppQuitTime = new DateTime(1970, 1, 1).ToLocalTime();
Debug.Log("heartRechargeTimer : " + m_RechargeRemainTime + "s");
//heartRechargeTimer.text = string.Format("Timer : {0} s", m_RechargeRemainTime);
}
public bool LoadHeartInfo()
{
Debug.Log("LoadHeartInfo");
bool result = false;
try
{
if (PlayerPrefs.HasKey("HeartAmount"))
{
Debug.Log("PlayerPrefs has key : HeartAmount");
m_HeartAmount = PlayerPrefs.GetInt("HeartAmount");
if (m_HeartAmount < 0)
{
m_HeartAmount = 0;
}
}
else
{
m_HeartAmount = MAX_HEART;
}
//heartAmountLabel.text = m_HeartAmount.ToString();
Debug.Log("Loaded HeartAmount : " + m_HeartAmount);
result = true;
}
catch (System.Exception e)
{
Debug.LogError("LoadHeartInfo Failed (" + e.Message + ")");
}
return result;
}
public bool SaveHeartInfo()
{
Debug.Log("SaveHeartInfo");
bool result = false;
try
{
PlayerPrefs.SetInt("HeartAmount", m_HeartAmount);
PlayerPrefs.Save();
Debug.Log("Saved HeartAmount : " + m_HeartAmount);
result = true;
}
catch (System.Exception e)
{
Debug.LogError("SaveHeartInfo Failed (" + e.Message + ")");
}
return result;
}
public bool LoadAppQuitTime()
{
Debug.Log("LoadAppQuitTime");
bool result = false;
try
{
if (PlayerPrefs.HasKey("AppQuitTime"))
{
Debug.Log("PlayerPrefs has key : AppQuitTime");
var appQuitTime = string.Empty;
appQuitTime = PlayerPrefs.GetString("AppQuitTime");
m_AppQuitTime = DateTime.FromBinary(Convert.ToInt64(appQuitTime));
}
Debug.Log(string.Format("Loaded AppQuitTime : {0}", m_AppQuitTime.ToString()));
//appQuitTimeLabel.text = string.Format("AppQuitTime : {0}", m_AppQuitTime.ToString());
result = true;
}
catch (System.Exception e)
{
Debug.LogError("LoadAppQuitTime Failed (" + e.Message + ")");
}
return result;
}
public bool SaveAppQuitTime()
{
Debug.Log("SaveAppQuitTime");
bool result = false;
try
{
var appQuitTime = DateTime.Now.ToLocalTime().ToBinary().ToString();
PlayerPrefs.SetString("AppQuitTime", appQuitTime);
PlayerPrefs.Save();
Debug.Log("Saved AppQuitTime : " + DateTime.Now.ToLocalTime().ToString());
result = true;
}
catch (System.Exception e)
{
Debug.LogError("SaveAppQuitTime Failed (" + e.Message + ")");
}
return result;
}
public void SetRechargeScheduler(Action onFinish = null)
{
if (m_RechargeTimerCoroutine != null)
{
StopCoroutine(m_RechargeTimerCoroutine);
}
var timeDifferenceInSec = (int)((DateTime.Now.ToLocalTime() - m_AppQuitTime).TotalSeconds);
Debug.Log("TimeDifference In Sec :" + timeDifferenceInSec + "s");
var heartToAdd = timeDifferenceInSec / HeartRechargeInterval;
Debug.Log("Heart to add : " + heartToAdd);
var remainTime = timeDifferenceInSec % HeartRechargeInterval;
Debug.Log("RemainTime : " + remainTime);
m_HeartAmount += heartToAdd;
if (m_HeartAmount >= MAX_HEART)
{
m_HeartAmount = MAX_HEART;
}
else
{
m_RechargeTimerCoroutine = StartCoroutine(DoRechargeTimer(remainTime, onFinish));
}
//heartAmountLabel.text = string.Format("Hearts : {0}", m_HeartAmount.ToString());
Debug.Log("HeartAmount : " + m_HeartAmount);
}
public void UseHeart(Action onFinish = null)
{
if (m_HeartAmount <= 0)
{
return;
}
m_HeartAmount--;
//heartAmountLabel.text = string.Format("Hearts : {0}", m_HeartAmount.ToString());
if (m_RechargeTimerCoroutine == null)
{
m_RechargeTimerCoroutine = StartCoroutine(DoRechargeTimer(HeartRechargeInterval));
}
if (onFinish != null)
{
onFinish();
}
}
private IEnumerator DoRechargeTimer(int remainTime, Action onFinish = null)
{
Debug.Log("DoRechargeTimer");
if (remainTime <= 0)
{
m_RechargeRemainTime = HeartRechargeInterval;
}
else
{
m_RechargeRemainTime = remainTime;
}
Debug.Log("heartRechargeTimer : " + m_RechargeRemainTime + "s");
//heartRechargeTimer.text = string.Format("Timer : {0} s", m_RechargeRemainTime);
while (m_RechargeRemainTime > 0)
{
Debug.Log("heartRechargeTimer : " + m_RechargeRemainTime + "s");
//heartRechargeTimer.text = string.Format("Timer : {0} s", m_RechargeRemainTime);
m_RechargeRemainTime -= 1;
yield return new WaitForSeconds(1f);
}
m_HeartAmount++;
if (m_HeartAmount >= MAX_HEART)
{
m_HeartAmount = MAX_HEART;
m_RechargeRemainTime = 0;
//heartRechargeTimer.text = string.Format("Timer : {0} s", m_RechargeRemainTime);
Debug.Log("HeartAmount reached max amount");
m_RechargeTimerCoroutine = null;
}
else
{
m_RechargeTimerCoroutine = StartCoroutine(DoRechargeTimer(HeartRechargeInterval, onFinish));
}
//heartAmountLabel.text = string.Format("Hearts : {0}", m_HeartAmount.ToString());
Debug.Log("HeartAmount : " + m_HeartAmount);
}
}
|
cs |
'게임 개발 > Unity3D' 카테고리의 다른 글
[Unity | 유니티] OnApplicationFocus(bool value) vs. OnApplicationPause(bool value) (1) | 2019.10.08 |
---|---|
[Unity | 유니티] Monobehaviour LifeCycle (0) | 2019.09.10 |
[Unity | 유니티] 스크린 좌표를 월드 좌표, NGUI 좌표로 전환하기 (0) | 2019.09.10 |
[Unity | 유니티] NGUI와 ParticleSystem간 Depth 조정 (1) | 2019.09.06 |
[Unity | 유니티] 메모리 최적화 (0) | 2019.09.06 |