-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModBehaviour.cs
More file actions
66 lines (51 loc) · 1.78 KB
/
ModBehaviour.cs
File metadata and controls
66 lines (51 loc) · 1.78 KB
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
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
namespace CustomSound;
// 继承自游戏Mod系统的基础行为类
public class ModBehaviour : Duckov.Modding.ModBehaviour
{
// 定义一个新的输入动作,用于替代原有的鸭叫按键
private InputAction newAction = new();
// 读取JSON配置文件并填充soundGroups
private List<SoundGroup> soundGroups = []; // 声音组数组
/// <summary>
/// 当脚本实例被载入时调用,用于初始化Mod
/// </summary>
private void Awake()
{
Debug.Log("CustomSound 已加载。");
}
/// <summary>
/// 当脚本启用时调用,用于初始化音频文件和输入绑定
/// </summary>
private void OnEnable()
{
// 重新读取JSON配置文件并填充soundGroups
soundGroups = ReadConfig.ReadJsonConfig();
// 获取游戏中原有的鸭叫输入动作
InputAction inputAction = GameManager.MainPlayerInput.actions.FindAction("Quack");
// 禁用原有的鸭叫输入动作
inputAction.Disable();
// 将原有动作的控制绑定到新动作上
newAction.AddBinding(inputAction.controls[0]);
// 注册新动作的回调函数
newAction.performed += (context) => SoundPlayer.PlayCustomSound(soundGroups);
// 启用新动作
newAction.Enable();
Debug.Log("CustomSound:载入完成。");
}
/// <summary>
/// 当脚本禁用时调用,用于清理资源和恢复原始设置
/// </summary>
private void OnDisable()
{
// 取消注册新动作的回调函数
newAction.performed -= (context) => SoundPlayer.PlayCustomSound(soundGroups);
// 禁用新动作
newAction.Disable();
Debug.Log("CustomSound:已禁用。");
// 重新启用原有的鸭叫输入动作
GameManager.MainPlayerInput.actions.FindAction("Quack").Enable();
}
}