Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
顺
顺职VR迷宫游戏教学
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
杨泽宇
顺职VR迷宫游戏教学
Commits
6582f6c6
Commit
6582f6c6
authored
Feb 28, 2024
by
杨泽宇
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
更新迷宫第二层:播放视频、回答数学问题
parent
8d9b103b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
1960 additions
and
373 deletions
+1960
-373
2_Maze2.unity
Assets/_Scenes/2_Maze2.unity
+1858
-354
GameSystem.cs
Assets/_Scripts/GameSystem.cs
+4
-1
MathGame.cs
Assets/_Scripts/MathGame.cs
+60
-18
SingletonBase.cs
Assets/_Scripts/SingletonBase.cs
+27
-0
SingletonBase.cs.meta
Assets/_Scripts/SingletonBase.cs.meta
+11
-0
No files found.
Assets/_Scenes/2_Maze2.unity
View file @
6582f6c6
This diff is collapsed.
Click to expand it.
Assets/_Scripts/GameSystem.cs
View file @
6582f6c6
...
...
@@ -5,7 +5,7 @@ using UnityEngine.Events;
using
UnityEngine.UI
;
using
TMPro
;
public
class
GameSystem
:
MonoBehaviour
public
class
GameSystem
:
SingletonBase
<
GameSystem
>
{
[
SerializeField
]
private
float
totalTime
=
60f
;
// 倒计时总时间
[
Tooltip
(
"时间文本"
)]
...
...
@@ -60,6 +60,9 @@ public class GameSystem : MonoBehaviour
}
}
/// <summary>
/// 倒计时结束
/// </summary>
public
void
EndGame
()
{
if
(
currentTime
<=
0
)
...
...
Assets/_Scripts/MathGame.cs
View file @
6582f6c6
...
...
@@ -14,9 +14,20 @@ public class MathGame : MonoBehaviour
[
Tooltip
(
"倒计时时长"
)]
public
float
countdownDuration
=
10f
;
[
Tooltip
(
"错误机会数量"
)]
public
int
chances
=
3
;
// 初始错误机会数量
public
int
chances
=
3
;
[
Tooltip
(
"错误机会文本"
)]
public
TMP_Text
chancesText
;
[
Tooltip
(
"重新开始提示UI"
)]
public
GameObject
restartUI
;
[
Tooltip
(
"重新生成问题的按钮"
)]
public
Button
regenerateButton
;
[
Tooltip
(
"游戏失败提示UI"
)]
public
GameObject
failedUI
;
[
Tooltip
(
"重新生成问题的按钮"
)]
public
Button
failedButton
;
[
Tooltip
(
"游戏成功提示UI"
)]
public
GameObject
SuccessUI
;
private
int
num1
;
private
int
num2
;
...
...
@@ -24,11 +35,21 @@ public class MathGame : MonoBehaviour
private
int
correctAnswer
;
private
bool
isAnswered
=
false
;
private
Coroutine
countdownCoroutine
;
private
int
currentChance
;
void
Start
()
{
currentChance
=
chances
;
regenerateButton
.
onClick
.
AddListener
(
RegenerateQuestion
);
GenerateQuestion
();
countdownCoroutine
=
StartCoroutine
(
StartCountdown
());
failedButton
.
onClick
.
AddListener
(
GameSystem
.
Instance
.
EndGame
);
}
private
void
OnDestroy
()
{
regenerateButton
.
onClick
.
RemoveAllListeners
();
failedButton
.
onClick
.
RemoveAllListeners
();
}
/// <summary>
...
...
@@ -47,25 +68,23 @@ public class MathGame : MonoBehaviour
if
(!
isAnswered
)
{
Debug
.
Log
(
"时间到!"
);
// 处理时间到的情况,例如重新生成问题
GenerateQuestion
();
ReduceChances
();
// 减少错误机会
if
(
currentChance
<=
0
)
{
failedUI
.
SetActive
(
true
);
}
else
{
restartUI
.
SetActive
(
true
);
ReduceChances
();
// 减少错误机会
}
}
}
/// <summary>
/// 生成问题
/// </summary>
p
rivate
void
GenerateQuestion
()
p
ublic
void
GenerateQuestion
()
{
// 检查错误机会是否大于零
if
(
chances
<=
0
)
{
Debug
.
Log
(
"没有机会了!"
);
return
;
}
// 生成两个随机数(1-9)
num1
=
Random
.
Range
(
1
,
10
);
num2
=
Random
.
Range
(
1
,
10
);
...
...
@@ -112,17 +131,31 @@ public class MathGame : MonoBehaviour
{
if
(
answerText
.
text
==
correctAnswer
.
ToString
())
{
Debug
.
Log
(
"正确"
);
isAnswered
=
true
;
if
(
countdownCoroutine
!=
null
)
{
StopCoroutine
(
countdownCoroutine
);
// 停止倒计时
}
}
else
{
Debug
.
Log
(
"错误"
);
ReduceChances
();
// 减少错误机会
if
(
countdownCoroutine
!=
null
)
{
StopCoroutine
(
countdownCoroutine
);
// 停止倒计时
}
if
(!
isAnswered
)
{
if
(
currentChance
<=
0
)
{
failedUI
.
SetActive
(
true
);
}
else
{
restartUI
.
SetActive
(
true
);
ReduceChances
();
// 减少错误机会
}
}
}
}
...
...
@@ -131,7 +164,7 @@ public class MathGame : MonoBehaviour
/// </summary>
private
void
ReduceChances
()
{
c
hances
--;
c
urrentChance
--;
UpdateChancesText
();
}
...
...
@@ -140,7 +173,7 @@ public class MathGame : MonoBehaviour
/// </summary>
private
void
UpdateChancesText
()
{
chancesText
.
text
=
"
Chances: "
+
chances
;
chancesText
.
text
=
"
剩余机会: "
+
currentChance
;
}
/// <summary>
...
...
@@ -159,4 +192,13 @@ public class MathGame : MonoBehaviour
{
answerText
.
text
=
""
;
}
/// <summary>
/// 重新生成问题
/// </summary>
void
RegenerateQuestion
()
{
restartUI
.
SetActive
(
false
);
GenerateQuestion
();
// 重新生成问题
}
}
Assets/_Scripts/SingletonBase.cs
0 → 100644
View file @
6582f6c6
using
UnityEngine
;
/// <summary>
/// 单例基类
/// </summary>
public
abstract
class
SingletonBase
<
T
>
:
MonoBehaviour
where
T
:
Component
{
private
static
T
_instance
;
public
static
T
Instance
{
get
=>
_instance
;
}
protected
virtual
void
Awake
()
{
if
(
_instance
==
null
)
_instance
=
this
as
T
;
else
Destroy
(
this
);
}
protected
virtual
void
OnDestroy
()
{
if
(
_instance
==
this
)
_instance
=
null
;
}
}
Assets/_Scripts/SingletonBase.cs.meta
0 → 100644
View file @
6582f6c6
fileFormatVersion: 2
guid: 41d39d6504479094ab5bf66c04d989e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment