恋狱牌局游戏

项目结构

1
2
3
4
5
6
7
8
9
10
Assets
├── Art
├── Audio
├── Prefabs
├── Resources
├── Scenes
├── ScriptableObjects
├── Scripts
├── Settings
└── StreamingAssets

对于Scripts游戏脚本文件夹目前为

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
Scripts
├── AI
├── Battle
├── Characters
├── Core
│ ├── Bootstrap
│ │ ├── BootstrapEntry.cs
│ │ └─── BootstrapInstaller.cs
│ ├── Config
│ ├── Events
│ ├── Managers
│ │ ├── AudioManager.cs
│ │ ├── EventBus.cs
│ │ ├── GameManager.cs
│ │ ├── SaveManager.cs
│ │ └── SceneLoader.cs
│ ├── Save
│ ├── StateMachine
│ │ └── GameStateMachine.cs
│ └─── Utility
├── Data
├── Narrative
├── Roguelike
├── Tools
└── UI

对于Scenes游戏场景目前为

1
2
3
4
5
6
7
Scenes
├── BattleScene.unity
├── Bootstrap.unity
├── DialogueScene.unity
├── LoadingScene.unity
├── MainMenu.unity
└── MapScene.unity

其中Bootstrap场景是控制中枢,在游戏启动的时候第一个加载,在Hierarchy层级里的对象父子关系为

1
2
3
4
5
6
7
8
9
10
BootstrapRoot
├──Systems
│ ├── GameManager(游戏生命周期管理器)
│ ├── EventBus(事件联通器)
│ ├── AudioManager(音效管理器)
│ ├── SaveManager(游戏存储管理器)
│ ├── Bootstraplnstallar(初始化系统,用于管理其他的6个功能中枢)
│ ├── SceneLoader(场景切换器)
│ └── GameStateMachine(游戏状态机)
└──BootstrapEntry(管理Bootstraplnstallar)

对应脚本将与对应的对象同名,并挂载到对应的对象里

初始化脚本

Scripts-Core-Bootstrap

BootstrapEntry.cs

搭载到BootstrapEntry空对象上,负责游戏启动时启用BootstrapInstaller对象的BootstrapInstaller.cs脚本

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
using UnityEngine;

namespace LovesickDeal.Core
{
/// <summary>
/// 游戏启动入口
/// </summary>
public class BootstrapEntry : MonoBehaviour
{
[SerializeField] private BootstrapInstaller installer;

private void Awake()
{
Debug.Log("[Bootstrap] Game Start");

DontDestroyOnLoad(transform.root.gameObject);

installer.Initialize();

Debug.Log("[Bootstrap] Initialize Complete");
}

private void Start()
{
SceneLoader.Instance.LoadMainMenu();
}
}
}

BootstrapInstaller.cs

这里在inspector检查器里搭载对应的对象

image-20260518130953858

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
using UnityEngine;

namespace LovesickDeal.Core
{
/// <summary>
/// 安装并初始化所有核心系统
/// </summary>
public class BootstrapInstaller : MonoBehaviour
{
[Header("Core Systems")]
[SerializeField] private GameManager gameManager;
[SerializeField] private SceneLoader sceneLoader;
[SerializeField] private EventBus eventBus;
[SerializeField] private AudioManager audioManager;
[SerializeField] private SaveManager saveManager;
[SerializeField] private GameStateMachine stateMachine;

public void Initialize()
{
gameManager.Initialize();
sceneLoader.Initialize();
eventBus.Initialize();
audioManager.Initialize();
saveManager.Initialize();
stateMachine.Initialize();
}
}
}

然后其他的几个暂时随便写写简单代替一下

Scripts-Core-Managers

GameManager.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;

namespace LovesickDeal.Core
{
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }

public void Initialize()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}

Instance = this;


Debug.Log("[GameManager] Initialized");
}
}
}

SceneLoader.cs

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
using UnityEngine;
using UnityEngine.SceneManagement;

namespace LovesickDeal.Core
{
public class SceneLoader : MonoBehaviour
{
public static SceneLoader Instance { get; private set; }

public void Initialize()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}

Instance = this;


Debug.Log("[SceneLoader] Initialized");
}

public void LoadMainMenu()
{
SceneManager.LoadScene("MainMenu");
}
}
}

EventBus.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;

namespace LovesickDeal.Core
{
public class EventBus : MonoBehaviour
{
public static EventBus Instance { get; private set; }

public void Initialize()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}

Instance = this;


Debug.Log("[EventBus] Initialized");
}
}
}

AudioManager.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using UnityEngine;

namespace LovesickDeal.Core
{
public class AudioManager : MonoBehaviour
{
public static AudioManager Instance { get; private set; }

public void Initialize()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}

Instance = this;

Debug.Log("[AudioManager] Initialized");
}
}
}

SaveManager.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using UnityEngine;

namespace LovesickDeal.Core
{
public class SaveManager : MonoBehaviour
{
public static SaveManager Instance { get; private set; }

public void Initialize()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
return;
}

Instance = this;


Debug.Log("[SaveManager] Initialized");
}
}
}

Scripts-Core-StateMachine

GameStateMachine.cs

1
2
3
4
5
6
7
8
9
10
11
12
using UnityEngine;

namespace LovesickDeal.Core
{
public class GameStateMachine : MonoBehaviour
{
public void Initialize()
{
Debug.Log("[GameStateMachine] Initialized");
}
}
}
image-20260518132648626

实现了一个简单的生命周期

1
2
3
4
5
6
7
8
9
10
11
GameManager

SceneLoader

EventBus

AudioManager

SaveManager

GameStateMachine

下载dotween插件增强画面动画效果

image-20260518214023253

接下来不会再写日志了,因为用纯文字描述很繁琐而且浪费时间,只记录关键知识点即可