@@ -18,7 +18,7 @@ public sealed class MusicManager
18
18
{
19
19
private static readonly MusicManager instance = new ( ) ;
20
20
21
- private int num = 1 ;
21
+ private uint num = 0 ;
22
22
/// <summary>
23
23
/// 获取<seealso cref="MusicManager"/>单例
24
24
/// </summary>
@@ -46,7 +46,7 @@ private ReferenceHub CreateMusicNpc(string name)
46
46
ReferenceHub hubNpc = newNpc . GetComponent < ReferenceHub > ( ) ;
47
47
NetworkServer . AddPlayerForConnection ( new FakeConnection ( 0 ) , newNpc ) ;
48
48
hubNpc . nicknameSync . Network_myNickSync = name ;
49
- MusicNpc . Add ( name , hubNpc ) ;
49
+ MusicNpc . Add ( $ " { num ++ } : { name } " , hubNpc ) ;
50
50
return hubNpc ;
51
51
}
52
52
@@ -65,151 +65,105 @@ public void Stop(AudioPlayerBase playerBase)
65
65
UnityEngine . Object . Destroy ( npc . gameObject ) ;
66
66
}
67
67
/// <summary>
68
- /// 播放音频
68
+ /// 向所有玩家播放音频
69
69
/// </summary>
70
70
/// <param name="musicFile">音频文件</param>
71
71
/// <param name="npcName">NPC名称</param>
72
72
/// <returns></returns>
73
73
public AudioPlayerBase Play ( string musicFile , string npcName )
74
74
{
75
- return Play ( musicFile , npcName , new TrackEvent ( ) , null , 0 , [ ] , false , 80 , false ) ;
75
+ return Play ( musicFile , npcName , - 1 ) ;
76
76
}
77
77
/// <summary>
78
- /// 播放音频
78
+ /// 向一名玩家播放音频
79
79
/// </summary>
80
80
/// <param name="musicFile">音频文件</param>
81
81
/// <param name="npcName">NPC名称</param>
82
- /// <param name="source">传播距离检测源头玩家</param>
83
- /// <param name="distance">传播距离</param>
82
+ /// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
84
83
/// <returns></returns>
85
- public AudioPlayerBase Play ( string musicFile , string npcName , FramePlayer source , float distance )
84
+ public AudioPlayerBase Play ( string musicFile , string npcName , FramePlayer source )
86
85
{
87
- return Play ( musicFile , npcName , new TrackEvent ( ) , source , distance , [ ] , false , 80 , false ) ;
86
+ return Play ( musicFile , npcName , source , 0 ) ;
88
87
}
89
88
/// <summary>
90
- /// 单独给一个人播放音频
89
+ /// NPC向玩家播放音频
91
90
/// </summary>
92
91
/// <param name="musicFile">音频文件</param>
93
92
/// <param name="npcName">NPC名称</param>
94
- /// <param name="source">指定玩家 </param>
93
+ /// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家) </param>
95
94
/// <returns></returns>
96
- public AudioPlayerBase Play ( string musicFile , string npcName , FramePlayer source )
95
+ public AudioPlayerBase Play ( string musicFile , string npcName , float distance )
97
96
{
98
- return Play ( musicFile , npcName , new TrackEvent ( ) , source , [ ] , false , 80 , false ) ;
97
+ return Play ( musicFile , npcName , null , distance ) ;
99
98
}
100
99
/// <summary>
101
- /// 播放音频
100
+ /// 在多少米内向玩家播放音频
102
101
/// </summary>
103
102
/// <param name="musicFile">音频文件</param>
104
103
/// <param name="npcName">NPC名称</param>
105
- /// <param name="trackEvent">播放事件,可以是 null</param>
106
- /// <param name="source">传播距离检测源头玩家,如果是 null 所有人都将听到</param>
107
- /// <param name="distance">传播距离(源头玩家为 null 将无效)</param>
108
- /// <param name="extraPlay">额外可接收音频的玩家,可以是 null</param>
109
- /// <param name="isSole">是否覆盖播放</param>
110
- /// <param name="volume">音量大小</param>
111
- /// <param name="isLoop">是否循环</param>
104
+ /// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
105
+ /// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家)</param>
112
106
/// <returns></returns>
113
- public AudioPlayerBase Play ( string musicFile , string npcName , TrackEvent ? trackEvent , FramePlayer source , float distance , FramePlayer [ ] extraPlay , bool isSole = false , float volume = 80 , bool isLoop = false )
107
+ public AudioPlayerBase Play ( string musicFile , string npcName , FramePlayer source , float distance )
114
108
{
115
- AudioPlayerBase audioPlayerBase = null ;
116
- try
117
- {
118
- if ( trackEvent . HasValue )
119
- {
120
- OnTrackLoaded += trackEvent . Value . TrackLoaded ;
121
- }
122
-
123
- if ( ! MusicNpc . TryGetValue ( npcName , out ReferenceHub npc ) )
124
- {
125
- npc = CreateMusicNpc ( npcName ) ;
126
- audioPlayerBase = Get ( npc ) ;
127
- }
128
- else
129
- {
130
- if ( ! isSole )
131
- {
132
- npc = CreateMusicNpc ( npcName ) ;
133
- audioPlayerBase = Get ( npc ) ;
134
- MusicNpc . Add ( num + npcName , npc ) ;
135
- num ++ ;
136
- }
137
- }
138
-
139
-
140
- if ( source != null )
141
- {
142
- audioPlayerBase . AudioToPlay = FramePlayer . List . Where ( p => Vector3 . Distance ( p . ExPlayer . Position , source . ExPlayer . Position ) <= distance ) . Select ( ( s ) => s . ExPlayer . UserId ) . ToList ( ) ;
143
- }
144
- else
145
- {
146
- audioPlayerBase . AudioToPlay = FramePlayer . List . Select ( ( s ) => s . ExPlayer . UserId ) . ToList ( ) ;
147
- }
148
-
149
- if ( extraPlay != null )
150
- {
151
- foreach ( var player in extraPlay )
152
- {
153
- audioPlayerBase . AudioToPlay . Add ( player . ExPlayer . UserId ) ;
154
- }
155
- }
156
-
157
- audioPlayerBase . Enqueue ( $ "{ Paths . Plugins } /{ Server . Port } /YongAnPluginData/{ musicFile } .ogg", 0 ) ;
158
- audioPlayerBase . Volume = volume ;
159
- audioPlayerBase . Loop = isLoop ;
160
- audioPlayerBase . Play ( 0 ) ;
161
- }
162
- catch ( Exception )
163
- {
164
- Stop ( audioPlayerBase ) ;
165
- }
166
- return audioPlayerBase ;
109
+ return Play ( musicFile , npcName , null , source , distance , null , false , 80 , false ) ;
167
110
}
168
111
/// <summary>
169
112
/// 播放音频
170
113
/// </summary>
171
114
/// <param name="musicFile">音频文件</param>
172
115
/// <param name="npcName">NPC名称</param>
173
- /// <param name="trackEvent">播放事件</param>
174
- /// <param name="source">传播距离检测源头玩家</param>
175
- /// <param name="extraPlay">额外可接收音频的玩家</param>
176
- /// <param name="isSole">是否覆盖播放</param>
116
+ /// <param name="trackEvent">播放事件(可null)</param>
117
+ /// <param name="source">传播距离检测源头玩家(可null,null时是NPC)</param>
118
+ /// <param name="distance">传播距离(-1时是全部玩家,0时是源头玩家)</param>
119
+ /// <param name="extraPlay">额外可接收音频的玩家(可null)</param>
120
+ /// <param name="isSole">[弃用]是否覆盖播放</param>
177
121
/// <param name="volume">音量大小</param>
178
122
/// <param name="isLoop">是否循环</param>
179
123
/// <returns></returns>
180
- public AudioPlayerBase Play ( string musicFile , string npcName , TrackEvent trackEvent , FramePlayer source , FramePlayer [ ] extraPlay , bool isSole = false , float volume = 80 , bool isLoop = false )
124
+ public AudioPlayerBase Play ( string musicFile , string npcName , TrackEvent ? trackEvent , FramePlayer source , float distance , FramePlayer [ ] extraPlay , bool isSole = false , float volume = 80 , bool isLoop = false )
181
125
{
182
126
AudioPlayerBase audioPlayerBase = null ;
183
127
try
184
128
{
185
- OnTrackLoaded += trackEvent . TrackLoaded ;
186
- if ( ! MusicNpc . TryGetValue ( npcName , out ReferenceHub npc ) )
129
+ if ( trackEvent . HasValue )
187
130
{
188
- npc = CreateMusicNpc ( npcName ) ;
189
- audioPlayerBase = Get ( npc ) ;
131
+ OnTrackLoaded += trackEvent . Value . TrackLoaded ;
190
132
}
191
- else
133
+
134
+ ReferenceHub npc = CreateMusicNpc ( npcName ) ;
135
+ audioPlayerBase = Get ( npc ) ;
136
+
137
+ if ( distance != - 1 )
192
138
{
193
- if ( ! isSole )
139
+ if ( source != null )
194
140
{
195
- npc = CreateMusicNpc ( npcName ) ;
196
- audioPlayerBase = Get ( npc ) ;
197
- MusicNpc . Add ( num + npcName , npc ) ;
198
- num ++ ;
141
+ if ( distance == 0 )
142
+ {
143
+ audioPlayerBase . BroadcastTo . Add ( npc . PlayerId ) ;
144
+ }
145
+ else
146
+ {
147
+ audioPlayerBase . BroadcastTo = FramePlayer . List . Where ( p => Vector3 . Distance ( p . ExPlayer . Position , source . ExPlayer . Position ) <= distance ) . Select ( ( s ) => s . ExPlayer . Id ) . ToList ( ) ;
148
+ }
199
149
}
200
- }
201
150
202
- if ( extraPlay != null )
203
- {
204
- audioPlayerBase . AudioToPlay = extraPlay . Select ( ( s ) => { return s . ExPlayer . UserId ; } ) . ToList ( ) ;
151
+ if ( extraPlay != null )
152
+ {
153
+ foreach ( var player in extraPlay )
154
+ {
155
+ if ( ! audioPlayerBase . BroadcastTo . Contains ( player . ExPlayer . Id ) )
156
+ {
157
+ audioPlayerBase . BroadcastTo . Add ( player . ExPlayer . Id ) ;
158
+ }
159
+ }
160
+ }
205
161
}
206
162
207
- audioPlayerBase . AudioToPlay . Add ( source . ExPlayer . UserId ) ;
208
-
209
- audioPlayerBase . Enqueue ( $ "{ Paths . Plugins } /{ Server . Port } /YongAnPluginData/{ musicFile } .ogg", 0 ) ;
163
+ audioPlayerBase . CurrentPlay = $ "{ Paths . Plugins } /{ Server . Port } /YongAnPluginData/{ musicFile } .ogg";
210
164
audioPlayerBase . Volume = volume ;
211
165
audioPlayerBase . Loop = isLoop ;
212
- audioPlayerBase . Play ( 0 ) ;
166
+ audioPlayerBase . Play ( - 1 ) ;
213
167
}
214
168
catch ( Exception )
215
169
{
0 commit comments