1. 项目名称及描述
一个简单的换装游戏
要求:
·开始界面
-开始游戏
-退出游戏
·主界面
-选项菜单
`*字体大小
*
字体颜色
*
音量
*
明暗模式`
-实现一键随机换装
2. 制作过程
2.1 准备场景
本游戏包括两个场景
Main:换装游戏主场景
Boot:游戏主菜单
2.2 场景之间的过渡和游戏退出
首先将两个场景都拖入层级窗口,然后在Build Setting上加载这两个场景>创建GameManager物体和GameManager脚本>在GameManager脚本中加入场景过渡OnClickStart()和游戏退出public void QuitGame()>将两个方法分别挂到主菜单相应的按钮上。
GameManager
.
cs
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
UnityEngine
;
using
UnityEngine
.
SceneManagement
;
public
class
GameManager
:
MonoBehaviour
{
public
void
OnClickStart
(
)
{
StartCoroutine
(
SceneSwitch
(
)
)
;
}
IEnumerator
SceneSwitch
(
)
{
AsyncOperation
load
=
SceneManager
.
LoadSceneAsync
(
"Main"
,
LoadSceneMode
.
Additive
)
;
yield
return
load
;
SceneManager
.
UnloadSceneAsync
(
"Boot"
)
;
}
public
void
QuitGame
(
)
{
#
if
UNITY_EDITOR
UnityEditor
.
EditorApplication
.
isPlaying
=
false
;
#
else
Application
.
Quit
(
)
;
#
endif
}
}
2.3 制作选项菜单
选项菜单包括字体大小、字体颜色、音量、明/暗四个选项。
2.4 呼出和关闭选项菜单
创建UIManager物体和UIManager脚本,把脚本挂到物体上>在Update()中输入呼出和关闭选项菜单的脚本
void
Update
(
)
{
if
(
Input
.
GetKeyDown
(
"escape"
)
)
{
if
(
menuIsActive
==
false
)
{
menu
.
SetActive
(
true
)
;
menuIsActive
=
true
;
Debug
.
Log
(
"Menu is "
+
menuIsActive
)
;
}
else
{
menu
.
SetActive
(
false
)
;
menuIsActive
=
false
;
Debug
.
Log
(
"Menu is "
+
menuIsActive
)
;
}
}
}
2.5 选项菜单
2.5.1 用下拉菜单改变字体大小
public
Dropdown
m_Dropdown
;
private
int
m_DropdownValue
;
public
void
DropdownOnClick
(
)
{
m_DropdownValue
=
m_Dropdown
.
value
;
switch
(
m_DropdownValue
)
{
case
0
:
changeCloth_Text
.
fontSize
=
25
;
break
;
case
1
:
changeCloth_Text
.
fontSize
=
30
;
break
;
case
2
:
changeCloth_Text
.
fontSize
=
35
;
break
;
}
}
2.5.2 用按钮改变字体颜色
public
TMP_Text
changeCloth_Text
;
public
void
TextColorGray
(
)
{
changeCloth_Text
.
color
=
new
Color32
(
79
,
79
,
79
,
255
)
;
}
public
void
TextColorYellow
(
)
{
changeCloth_Text
.
color
=
new
Color32
(
178
,
106
,
21
,
255
)
;
}
public
void
TextColorRed
(
)
{
changeCloth_Text
.
color
=
new
Color32
(
140
,
34
,
34
,
255
)
;
}
public
void
TextColorBlue
(
)
{
changeCloth_Text
.
color
=
new
Color32
(
32
,
28
,
84
,
255
)
;
}
2.5.3 用滑动条改变音量
public
AudioSource
BGM
;
public
Slider
BGMSlider
;
public
void
BGMVolume
(
)
{
BGM
.
volume
=
BGMSlider
.
value
;
}
2.5.4 用滑动条切换明/暗模式
public
Light
mainLight
;
public
Slider
lightModeSlider
;
public
void
LightMode
(
)
{
if
(
lightModeSlider
.
value
==
0
)
{
mainLight
.
intensity
=
4.0f
;
}
else
{
mainLight
.
intensity
=
1.0f
;
}
}
2.6 随机换装脚本
ChangeColors
.
cs
using
System
.
Collections
;
using
System
.
Collections
.
Generic
;
using
UnityEngine
;
public
class
ChangeColors
:
MonoBehaviour
{
private
GameObject
ob
;
private
Renderer
body
;
public
Material
hairmat
;
public
Material
onemat
;
public
Material
fangmat
;
public
Material
clothmat
;
public
Material
shouldermat
;
public
Material
ninemat
;
public
Material
tenmat
;
public
Material
trousersmat
;
public
Material
newCloth
;
public
Material
[
]
clothsInCloset
=
new
Material
[
5
]
;
void
Start
(
)
{
ob
=
GameObject
.
Find
(
"U_Char_0"
)
;
Debug
.
Log
(
ob
.
name
+
" is here!"
)
;
}
public
void
ClickChangeColors
(
)
{
body
=
ob
.
GetComponent
<
SkinnedMeshRenderer
>
(
)
;
newCloth
=
clothsInCloset
[
Random
.
Range
(
0
,
clothsInCloset
.
Length
)
]
;
body
.
materials
[
0
]
=
hairmat
;
body
.
materials
[
1
]
=
onemat
;
body
.
materials
[
2
]
=
fangmat
;
body
.
materials
[
3
]
=
newCloth
;
body
.
materials
[
4
]
=
shouldermat
;
body
.
materials
[
5
]
=
ninemat
;
body
.
materials
[
6
]
=
tenmat
;
body
.
materials
[
7
]
=
newCloth
;
Material
[
]
mats
=
new
Material
[
]
{
hairmat
,
onemat
,
fangmat
,
newCloth
,
shouldermat
,
ninemat
,
tenmat
,
newCloth
}
;
body
.
materials
=
mats
;
}
}
3. 后期处理
安装URP和Post Processing资源包>在相机上添加Post-process Volume图层>Add Overrides>添加Bloom和Depth Of Field两个特效,并调整相应参数。
本文详细介绍了如何制作一个包含主菜单、随机换装功能的游戏,涉及场景管理、选项菜单设计(包括字体、颜色、音量和模式)、以及关键代码片段如场景过渡、UI管理与随机服装更换。通过Unity引擎实现,适合初学者了解游戏开发流程。

被折叠的 条评论
为什么被折叠?



