MMORPG格斗之王

思考并回答以下问题:

  • 框架设计
  • 战斗系统设计
  • 角色系统
  • 关卡系统
  • Protobuf使用
  • 网络客户端底层封装
  • 多线程资源下载

游戏UI设计

功能点:

  • 游戏闪屏界面(通用)
  • 数据加载界面
  • 登录界面
  • 角色展示界面
  • 创建角色界面
  • 主城场景
  • 副本选择

流程:

00_InitScene -> 01_LogoScene -> 01_PatchScene -> Login -> 02_TitleScene -> 03_LobbyScene -> 04_GameScene

00_InitScene:空对象,数据加载的处理
01_PatchScene:为了后期更新使用的,补丁。

ResetScene.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using UnityEngine;
using System.Collections;
using System;
using Whales.Game.Manager;
using Whales.Game.Scenes;

/// <summary>
/// 跳转至下一场景前,重置卸载当前场景
/// </summary>
public class ResetScene : MonoBehaviour
{
private AsyncOperation m_Async;
private static bool m_bUseLoadingScene; //Loading Scene
private static string m_NextSceneName; //下一场景名称

public Camera m_UICam;
public Camera m_WorldCam;

void Awake()
{
UnityEngine.Object[] objArray = Resources.FindObjectsOfTypeAll(typeof(Material));
for (int i = 0; i < objArray.Length; i++)
{
objArray[i] = null;
}

UnityEngine.Object[] objArray2 = Resources.FindObjectsOfTypeAll(typeof(Texture));
for (int j = 0; j < objArray2.Length; j++)
{
objArray2[j] = null;
}
Resources.UnloadUnusedAssets();
GC.Collect();
GC.WaitForPendingFinalizers();//挂起当前线程,直到处理终结器队列的线程清空该队列为止。
GC.Collect();
CommonHelper.SetScreenAdjust(this.m_UICam, this.m_WorldCam);
}

/// <summary>
///
/// </summary>
/// <param name="nextSceneName"></param>
/// <param name="bUseLoadingScene"></param>
public static void LoadLevel(string nextSceneName, bool bUseLoadingScene)
{
UIManager.ClearRoot();
m_NextSceneName = nextSceneName;
m_bUseLoadingScene = bUseLoadingScene;
Application.LoadLevel("ResetScene");
}

// Use this for initialization
void Start ()
{
if (m_bUseLoadingScene)
{
LoadingScene.SetNextScene(m_NextSceneName);
Application.LoadLevelAdditive("LoadingScene");
}
else
{
StartCoroutine("StartLoad", m_NextSceneName);
}
}

/// <summary>
///
/// </summary>
/// <param name="strSceneName"></param>
/// <returns></returns>
public IEnumerator StartLoad(string strSceneName)
{
m_Async = Application.LoadLevelAsync(strSceneName);
yield return m_Async;
}

// Update is called once per frame
void Update ()
{
TimeManager.Update();
}

private void OnDestroy()
{
this.m_Async = null;
this.m_UICam = null;
this.m_WorldCam = null;
}
}

InitScene.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
using UnityEngine;
using System.Collections;
using Whales.Game.Systems;
using Whales.Game.Helper;
using Whales.Game.Manager;
using Whales.Game.UI;
using Whales.Game.Systems.Security;
using Whales.Game.Net;

/// <summary>
/// 场景初始化
///
/// </summary>
public class InitScene : MonoBehaviour
{
/// <summary>
/// 标识是否已初始化
/// </summary>
private static bool m_bInit;

/// <summary>
/// 标识加载结束
/// </summary>
private bool m_bLoadEnd;

public static string m_CheatName = string.Empty;

public static string m_CheckInstalledTstore = string.Empty;

private float m_PassTime;

/// <summary>
/// 上一个场景
/// </summary>
private static string m_PrevScene = "01_LogoScene";

public static string m_RecvNetworkOperationName = string.Empty;

public static string m_UUID = string.Empty;

public static string m_XigncodeCookie = string.Empty;

// Use this for initialization
void Start ()
{
// 初始化加载
StartCoroutine(this.InitAndLoad());
}

// Update is called once per frame
void Update ()
{
this.m_PassTime += Time.deltaTime;
if (this.m_bLoadEnd && (this.m_PassTime >= 3f))
{
// m_PrevScene默认为01_LogoScene
// 在Update中处理
ResetScene.LoadLevel(m_PrevScene, false);
}
}

/// <summary>
///
/// </summary>
public void InitSystem()
{
if (!m_bInit)
{
m_bInit = true;
SPlayerPrefs.keys = new string[] { "68XBEdre", "SP375pHa", "frA56843", "tWSZ2epr", "jaw64DAs" };
NetworkHelper.Init();
//1.6
float default_aspect = (float)GeneralDefine.m_UIDefaultW / (float)GeneralDefine.m_UIDefaultH;

float screen_aspect = (float)Screen.width / (float)Screen.height;

if (screen_aspect < default_aspect)
{
GeneralDefine.m_bNeedScreenAdjust = true;
}
else
{
GeneralDefine.m_bNeedScreenAdjust = false;
}

AhHelper.Init();
NetworkHelper.bNetworkOnOff = false;

PlayerDataManager.SetMajorVersion(0);
PlayerDataManager.SetMiddleVersion(0);
PlayerDataManager.SetMinorVersion(1);
Crypto.InitKey();
Application.targetFrameRate = GeneralDefine.m_FrameRate;
QualitySettings.vSyncCount = 0;

//Caching.enabled = false;
DebugLog.Log("Caching.expirationDelay Sec:" + Caching.expirationDelay);
DebugLog.Log("Caching.maximumAvailableDiskSpack M:" + Caching.maximumAvailableDiskSpace / 0x100000L);
DebugLog.Log("Caching.spaceFree M:" + Caching.spaceFree / 0x100000L);
DebugLog.Log("Caching.spaceOccupied M:" + Caching.spaceOccupied / 0x100000L);
Screen.sleepTimeout = -1;

Time.timeScale = 1f;

// 文件管理初始化
FileManager.init();
// UI管理初始化
UIManager.Init();
// 特效
FxManager.Init();
// 声音
SoundManager.Init();
// 技能定义
SkillDefine.Init();

ObjMsgRouter.Init();
// 新手引导
TutorialManager.Init();

UtilManager.Instance.Init();
}

}

public static bool IsInit()
{
return m_bInit;
}

/// <summary>
/// 初始化加载
/// </summary>
/// <returns></returns>
private IEnumerator InitAndLoad()
{
AndroidJavaClass ajc;
string marketStr;
// 初始化系统
this.InitSystem();

if (Application.platform != RuntimePlatform.Android)
{

}
if (Application.platform == RuntimePlatform.Android)
{
//ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
//GeneralDefine.m_AndroidJavaActivity = ajc.GetStatic<AndroidJavaObject>("currentActivity");
//object[] objArr = new object[] { "_InitScene", "RecvExecArgsInfo_type", "type" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetexecArgsInfo", objArr);
//object[] objArr2 = new object[] { "_InitScene", "RecvExecArgsInfo_item01", "item01" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetExecArgsInfo", objArr2);
//object[] objArr3 = new object[] { "_InitScene", "RecvExecArgsInfo_item02", "item02" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetExecArgsInfo", objArr3);
//object[] objArr4 = new object[] { "_InitScene", "RecvExecArgsInfo_item03", "item03" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetExecArgsInfo", objArr4);
//object[] objArr5 = new object[] { "_InitScene", "RecvExecArgsInfo_item04", "item04" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetExecArgsInfo", objArr5);
//if (OptionUI.GetRecvPush() != 0)
//{
// GeneralDefine.m_AndroidJavaActivity.Call("GCM_On", new object[0]);
//}
//GeneralDefine.m_AndroidJavaActivity.Call("GCM_Off", new object[0]);

//object[] args = new object[] { "_InitScene", "RecvGCMRegistID" };
//GeneralDefine.m_AndroidJavaActivity.Call("GCM_GetRegID", args);

//object[] objArr6 = new object[] { "_InitScene", "RecvNetworkOperationName" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetNetworkOperationName", objArr6);
//marketStr = EnumDefine.m_MarketTSprNames[(int)GeneralDefine.m_MarketT];
//object[] objArr7 = new object[] { GeneralDefine.m_YDSDKGameName, marketStr };
//GeneralDefine.m_AndroidJavaActivity.Call("YDInit", objArr7);
//object[] objArr8 = new object[] { "_InitScene", "RecvCheatInfo" };
//GeneralDefine.m_AndroidJavaActivity.Call("CheckCheat", objArr8);
//object[] objArr9 = new object[] { "_InitScene", "RecvXigncodeInfo" };
//GeneralDefine.m_AndroidJavaActivity.Call("GetXigncodeInfo", objArr9);

//GeneralDefine.m_AndroidJavaActivity.Call("NetCheck_Init", new object[0]);
}


UIManager.AttachCommonUIAll(GameObject.Find("UI Root/Camera"));
TcpNetWorks.Instance.InitMemoryStream();
TcpChatNetWorks.Instacne.InitMemoryStream();
NetMessageCoder.Instance.Init();
NetWorkCheckManager.Instance.Init();
this.m_bLoadEnd = true;


yield return null;

}

/// <summary>
/// 检查系统是否已初始化,m_bInit变量如果为false,即代表未从00_InitScene场景启动,无法初始化系统。
/// </summary>
/// <param name="str"></param>
public static void CheckSystemInit(string str)
{
if (!m_bInit)
{
//m_PrevScene = str;
ResetScene.LoadLevel("00_InitScene", false);
}
}

/// <summary>
///
/// </summary>
/// <param name="recv"></param>
public void RecvCheatInfo(string recv)
{
if (recv == string.Empty)
{
m_CheatName = string.Empty;
}
else
{
m_CheatName = recv;
}
}

public void RecvGCMRegistID(string registID)
{
PlayerDataManager.SetGCMRegistID(registID);
Debug.Log("GCM RegistID:" + registID);
}

void OnDestroy()
{

}
}

LogoScene.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Whales.Game.Helper;
using Whales.Game.Manager;

namespace Whales.Game.Scenes
{
/// <summary>
/// Logo场景
/// </summary>
public class LogoScene : MonoBehaviour
{
/// <summary>
/// 标识是否再次运行Logo 场景
/// </summary>
private static bool m_bRetrunLogoScene;

private bool m_bUseSnowPopcornLogo;

public GameObject m_CompanyLogo;

public GameObject m_PublisherLogo;

public GameObject m_SnowPopcornLogo;

public GameObject m_ScreenBG;

public Camera m_UICam;

public Camera m_WorldCam;

void Awake()
{
InitScene.CheckSystemInit(Application.loadedLevelName);

CommonHelper.SetScreenAdjust(this.m_UICam, this.m_WorldCam);
}

void Start()
{
Time.timeScale = 1f;
CommonHelper.PlayFullScreenMovie("logo.mp4");
UIManager.AttachCommonUIAll(GameObject.Find("UI Root/Camera"));
StartCoroutine(this.LogoStep());
}

void Update()
{
SoundManager.Update(Time.deltaTime);
}

/// <summary>
///
/// </summary>
/// <returns></returns>
private IEnumerator LogoStep()
{
//if (!LogoScene.m_bRetrunLogoScene)
//{
// yield return new WaitForSeconds(1.5f);
//}
//else
//{
// yield return new WaitForSeconds(2f);
//}
yield return new WaitForSeconds(0f);
SoundManager.PlaySnd(0x7f);
//yield return new WaitForSeconds(2.5f);
LogoScene.m_bRetrunLogoScene = true;
ResetScene.LoadLevel("01_PatchScene", false);
}

private void OnDestroy()
{
this.m_UICam = null;
this.m_WorldCam = null;
this.m_ScreenBG = null;
}
}
}

LoadingScene.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Whales.Game.Data;
using Whales.Game.Manager;
using Whales.Game.Helper;

namespace Whales.Game.Scenes
{
/// <summary>
/// 加载场景
/// </summary>
public class LoadingScene : MonoBehaviour
{
private bool IsLoadGame;
public GameObject m_AnchorCenterObj;
private AsyncOperation m_Async;
public UILabel m_LabelTipDes;
private GameObject m_LoadingUIObj;
private static string m_NextSceneName = "04_GameScene";
private GameObject m_PFLoadingUIObj;
public Camera m_UICam;
public Camera m_WorldCam;

private void Awake()
{
// 进入下一个场景,检查系统
InitScene.CheckSystemInit(Application.loadedLevelName);
// 屏幕自适应
CommonHelper.SetScreenAdjust(this.m_UICam, this.m_WorldCam);
}

void Start()
{
List<GuideInLoadData> guideInLoadDataList = FileManager.GetGuideInLoadDataList();
int num = UnityEngine.Random.Range(0, guideInLoadDataList.Count);
UIHelper.SetContent(this.m_LabelTipDes, guideInLoadDataList[num].Tip);

if (this.m_PFLoadingUIObj == null)
{
DebugLog.Log("Load Fail" + name);
}
else
{
this.m_LoadingUIObj = Instantiate(this.m_PFLoadingUIObj, Vector3.zero, Quaternion.identity) as GameObject;
this.m_LoadingUIObj.SetActive(true);
this.m_LoadingUIObj.transform.parent = this.m_AnchorCenterObj.transform;
this.m_LoadingUIObj.transform.localPosition = this.m_PFLoadingUIObj.transform.localPosition;
this.m_LoadingUIObj.transform.localScale = this.m_PFLoadingUIObj.transform.localScale;
this.m_LoadingUIObj.layer = this.m_PFLoadingUIObj.layer;
}

StartCoroutine("StartLoad", m_NextSceneName);
}

void Update()
{
NetWorkCheckManager.CheckUpdate();
// 时间管理
TimeManager.Update();
}

public IEnumerator StartLoad(string NsceneName)
{
if(IsLoadGame){

yield break;
}
IsLoadGame = true;
this.m_Async = Application.LoadLevelAsync(NsceneName);
yield return m_Async;

}

/// <summary>
/// 对外提供的接口,设置下一个场景
/// </summary>
/// <param name="nextSceneName"></param>
public static void SetNextScene(string nextSceneName)
{
m_NextSceneName = nextSceneName;
}

private void OnDestroy()
{
this.m_Async = null;
this.m_UICam = null;
this.m_WorldCam = null;
this.m_LabelTipDes = null;
this.m_AnchorCenterObj = null;
this.m_LoadingUIObj = null;
this.m_PFLoadingUIObj = null;
}
}
}

对象池封装

ObjectPool.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Collections.Generic;
using UnityEngine;
using Whales.Game.Manager;

namespace Whales.Game.BO.Pool
{
/// <summary>
/// @author:
/// @time:
/// @company:
/// @desc:
/// 对象管理池
/// 供其他类调用,不需要继承MonoBehavior
/// </summary>
public class ObjectPool
{
// 分配的数量
private int m_AllocNum = 0;
private GameObject m_BaseGameObj;


// 有两个列表,一个空闲的,一个使用的

/// <summary>
/// 空闲对象列表
/// </summary>
private List<GameObject> m_IdleList = new List<GameObject>();
// 第一次分配可能没有那么大,重新分配
private int m_ReAllocNum = 0;

/// <summary>
/// //正在使用中的对象列表
/// </summary>
private List<GameObject> m_UsingList = new List<GameObject>();

// 构造函数中初始化各种字段属性
public ObjectPool()
{
this.m_IdleList.Clear();
this.m_UsingList.Clear();
this.m_BaseGameObj = null;
this.m_IdleList.Clear();
this.m_UsingList.Clear();
}

/// <summary>
/// 初始化
/// </summary>
/// <param name="baseobj"></param>
/// <param name="allocnum">分配数量</param>
/// <param name="reallocnum"></param>
public void Init(GameObject baseobj, int allocnum, int reallocnum)
{
DebugLog.Log("eff name:" + baseobj.name);
this.m_BaseGameObj = baseobj;
this.m_AllocNum = allocnum;
this.m_ReAllocNum = reallocnum;
this.Alloc(this.m_AllocNum);
}

/// <summary>
/// 分配
/// </summary>
/// <param name="allocnum"></param>
public void Alloc(int allocnum)
{
// 根据数量去循环实例化
for (int i = 0; i < allocnum; i++)
{
GameObject item = UnityEngine.Object.Instantiate(this.m_BaseGameObj, Vector3.zero, Quaternion.identity) as GameObject;

if (item == null)
{
Debug.LogError("failed");
}

item.SendMessage("Init", ObjectManager.GetUid());
item.SendMessage("Disable");
item.gameObject.SetActive(false);
this.m_IdleList.Add(item);
}
}

/// <summary>
/// 回收不在使用的对象
/// </summary>
/// <param name="pushobj"></param>
/// <returns></returns>
public bool Push(GameObject pushobj)
{
if (this.m_UsingList.Find(x => x == pushobj))
{
pushobj.SendMessage("Disable");
pushobj.gameObject.SetActive(false);

this.m_IdleList.Add(pushobj);
this.m_UsingList.Remove(pushobj);

return true;
}
return false;
}

/// <summary>
/// 返回索引为0的元素
/// </summary>
/// <returns>可以使用的对象</returns>
public GameObject Pop()
{
if (this.m_IdleList.Count == 0)
{
if (this.m_ReAllocNum == 0)
{
if (this.m_UsingList.Count > 0)
{
GameObject obj2 = this.m_UsingList[0];
this.m_IdleList.Add(obj2);
this.m_UsingList.Remove(obj2);
}
else
{

}
}
else
{
this.Alloc(this.m_ReAllocNum);
}
}

if (this.m_IdleList.Count <= 0)
{
return null;
}

GameObject item = this.m_IdleList[0];
item.gameObject.SetActive(true);
this.m_UsingList.Add(item);
this.m_IdleList.Remove(item);
return item;
}

// 对字段的读取
public int GetAllocNum()
{
return this.m_AllocNum;
}

public int GetReAllocNum()
{
return this.m_ReAllocNum;
}

/// <summary>
/// 清理对象池
/// </summary>
public void Clean()
{
for (int i = 0; i < this.m_UsingList.Count; i++)
{
UnityEngine.Object.DestroyImmediate(this.m_UsingList[i]);
this.m_UsingList[i] = null;
}

for (int j = 0; j < this.m_IdleList.Count; j++)
{
UnityEngine.Object.DestroyImmediate(this.m_IdleList[j]);
this.m_IdleList[j] = null;
}
}
}
}

ObjectManager.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Whales.Game.BO.Pool;
using Whales.Game.Actor;
using Whales.Game.Actor.Bullet;
using Whales.Game.BO;
using Whales.Game.Manager;
using Whales.Game.Data;
using Whales.Game.Helper;
using Whales.Game.Systems;

namespace Whales.Game.Manager
{
/// <summary>
/// 对象管理器
/// </summary>
public static class ObjectManager
{
public static GameObject m_GameScene;

// 塔防场景
public static GameObject m_InfinityTowerScene;

/// <summary>
/// 英雄资源
/// </summary>
private static GameObject[] m_HeroSources;

/// <summary>
/// 特效资源
/// </summary>
private static GameObject[] m_EffSources;

// 特效信息
private static List<EffectManager.EffectInfo> m_EffInfos;

/// <summary>
/// 子弹特效
/// </summary>
private static GameObject[] m_BulletSources;

/// <summary>
///
/// </summary>
private static GameObject[] m_GameItemSources;

/// <summary>
/// 英雄列表
/// </summary>
public static List<GameObject> m_Heros = new List<GameObject>();

/// <summary>
/// 相机
/// </summary>
public static GameObject m_MainCamObj;

/// <summary>
/// 相机目标
/// </summary>
public static GameObject m_MainCamTarget;

/// <summary>
/// 怪物列表
/// </summary>
public static List<GameObject> m_Monsters = new List<GameObject>();

public static List<GameObject> m_ObjMonsters = new List<GameObject>();

// 怪物id数量
private static int m_MonUidCount;

//
private static int m_UidCount;

private static int m_GameItemUidCount;

private static Dictionary<string, GameObject> m_Objects = new Dictionary<string, GameObject>();

//对象管理池
private static ObjectPool[] m_HeroLists;
private static ObjectPool[] m_BulletLists;
private static ObjectPool[] m_GameItemLists;
private static ObjectPool[] m_EffLists;
private static ObjectPool[] m_MonEffLists;

private static Dictionary<string, ObjectPool> m_MonLists = new Dictionary<string, ObjectPool>();
private static Dictionary<string, GameObject> m_MonSources = new Dictionary<string, GameObject>();
private static Dictionary<EnumDefine.EffT, ObjectPool[]> m_EffTLists = new Dictionary<EnumDefine.EffT, ObjectPool[]>();
private static EffectManager m_EffManager;

/// <summary>
/// 英雄基础脚本
/// </summary>
public static BaseHero m_MyHeroScript;

/// <summary>
/// 英雄
/// </summary>
public static GameObject m_MyHero;
public static void Init(EnumDefine.JobType jobT)
{
m_UidCount = 0;
m_MonUidCount = 0;
m_GameItemUidCount = 0;

m_GameScene = GameObject.Find("GameScene");


m_MainCamObj = GameObject.Find("MainCamObj");
m_MainCamTarget = GameObject.Find("MainCamTarget");

Resources.UnloadUnusedAssets();
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
LoadHeroSources(jobT);
LoadBulletSources();
LoadEffSources();

InitHero(jobT);
InitBullet();
InitHeroEff(jobT);
InitMonEff();
m_MonSources.Clear();

m_MonLists.Clear();

m_Objects.Clear();

m_Heros.Clear();

m_Monsters.Clear();

m_ObjMonsters.Clear();

}

/// <summary>
/// 清理对象管理器
/// </summary>
public static void Clean()
{
m_UidCount = 0;
m_MonUidCount = 0;
m_GameItemUidCount = 0;
m_MyHero = null;
m_MyHeroScript = null;
m_MainCamObj = null;
m_MainCamTarget = null;
m_GameScene = null;
m_InfinityTowerScene = null;
m_Objects.Clear();
m_Heros.Clear();
m_Monsters.Clear();
m_ObjMonsters.Clear();
m_EffTLists.Clear();
if (m_HeroLists != null)
{
for (int i = 0; i < m_HeroLists.Length; i++)
{
m_HeroLists[i].Clean();
m_HeroLists[i] = null;
}
}

m_HeroLists = null;
m_HeroSources = null;

if (m_BulletLists != null)
{
for (int j = 0; j < m_BulletLists.Length; j++)
{
m_BulletLists[j].Clean();
m_BulletLists[j] = null;
}
}
m_BulletLists = null;
m_BulletSources = null;

if (m_EffLists != null)
{
for (int m = 0; m < m_EffLists.Length; m++)
{
m_EffLists[m].Clean();
m_EffLists[m] = null;
}
}
m_EffLists = null;
m_EffSources = null;
m_EffInfos = null;
}

public static void CleanMon()
{
foreach (KeyValuePair<string, ObjectPool> pair in m_MonLists)
{
pair.Value.Clean();
}
m_MonLists.Clear();
m_MonSources.Clear();
}

public static GameObject[] GetEffSources()
{
return m_EffSources;
}


/// <summary>
/// 加载特效资源
/// </summary>
private static void LoadEffSources()
{
m_EffSources = new GameObject[EffDefine.m_EffSrcPaths.Length];

for (int i = 0; i < m_EffSources.Length; i++)
{
string name = EffDefine.m_EffSrcPaths[i];
m_EffSources[i] = Resources.Load(name, typeof(GameObject)) as GameObject;
}
}

/// <summary>
///
/// </summary>
private static void LoadBulletSources()
{
m_BulletSources = new GameObject[BulletDefine.m_BulletSrcPaths.Length];
for (int i = 0; i < m_BulletSources.Length; i++)
{
string name = BulletDefine.m_BulletSrcPaths[i];
m_BulletSources[i] = Resources.Load(name, typeof(GameObject)) as GameObject;
}
}

/// <summary>
///
/// </summary>
public static void InitBullet()
{
if (m_BulletSources == null)
{
Debug.LogError("Not Set BulletSources");
}
else
{
m_BulletLists = new ObjectPool[m_BulletSources.Length];
for (int i = 0; i < m_BulletLists.Length; i++)
{
m_BulletLists[i] = new ObjectPool();

}
for (int j = 0; j < m_BulletSources.Length; j++)
{
m_BulletLists[j].Init(m_BulletSources[j], BulletDefine.m_BulletPoolSize, 1);
}
}
}


public static EffectManager.EffectInfo GetEffInfo(int effId,EnumDefine.EffT effT)
{
if (effT == EnumDefine.EffT.PublicEffect)
return null;
return m_EffInfos[effId];
}


/// <summary>
/// 实例化特效
/// </summary>
//private static void InitEff()
//{
// if (m_EffSources == null){
// DebugLog.LogError("Not Set EffSource");
// }
// else
// {
// m_EffLists = new ObjectPool[m_EffSources.Length];
// for (int i = 0; i < m_EffLists.Length; i++)
// {
// m_EffLists[i] = new ObjectPool();
// }

// for (int j = 0; j < m_EffSources.Length; j++)
// {
// DebugLog.Log(m_EffSources[j]);
// m_EffLists[j].Init(m_EffSources[j], EffDefine.m_EffPoolSizes[j], 0);
// }
// }
//}

private static void InitMonEff()
{
if(m_EffSources == null){
DebugLog.LogError("Not Set EffSources");
}
else{

m_MonEffLists = new ObjectPool[m_EffSources.Length];
for (int i = 0; i < m_MonEffLists.Length; i++)
{
m_MonEffLists[i] = new ObjectPool();
}

for (int j = 0; j < m_EffSources.Length; j++)
{
m_MonEffLists[j].Init(m_EffSources[j], EffDefine.m_EffPoolSizes[j], 0);
}
}

m_EffTLists.Add(EnumDefine.EffT.PublicEffect, m_MonEffLists);


}

/// <summary>
///
/// </summary>
/// <param name="jobT"></param>
private static void InitHeroEff(EnumDefine.JobType jobT)
{
m_EffLists = new ObjectPool[m_EffInfos.Count];

for (int i = 0; i < m_EffLists.Length; i++)
{
m_EffLists[i] = new ObjectPool();
}

for (int j = 0; j < m_EffInfos.Count; j++)
{
m_EffLists[j].Init(m_EffInfos[j].m_effObj, m_EffInfos[j].m_poolSize, 0);
}

m_EffTLists.Add(EnumDefine.EffT.HeroEffect, m_EffLists);

}


private static void LoadGameItemSources()
{

}

public static void InitInLobbyScene()
{
m_MainCamObj = GameObject.FindWithTag("MainCamObj");
LoadEffSourcesInLobbyScene();
InitEffInLobbyScene();
}

private static void LoadEffSourcesInLobbyScene()
{

}

public static void InitEffInLobbyScene()
{

}




/// <summary>
/// 初始化游戏物品
/// </summary>
public static void InitGameItem()
{
if(m_GameItemSources == null)
{
Debug.LogError("Not Set GameItemSources");
}
else
{
m_GameItemLists = new ObjectPool[m_GameItemSources.Length];
for (int i = 0; i < m_GameItemLists.Length; i++)
{
m_GameItemLists[i] = new ObjectPool();
}

for (int j = 0; j < m_GameItemSources.Length; j++)
{
m_GameItemLists[j].Init(m_GameItemSources[j], GameItemDefine.m_ItemPoolSizes[j], 1);
}
}
}

/// <summary>
/// 加载英雄资源
/// </summary>
private static void LoadHeroSources(EnumDefine.JobType jobT)
{
m_HeroSources = new GameObject[HeroDefine.m_HeroSrcPaths.Length];

for (int i = 0; i < m_HeroSources.Length; i++)
{
m_HeroSources[i] = Resources.Load(HeroDefine.m_HeroSrcPaths[i], typeof(GameObject)) as GameObject;
}
}

/// <summary>
/// 初始化英雄
/// </summary>
public static void InitHero(EnumDefine.JobType jobT)
{
if (m_HeroSources == null)
{

}
else
{
m_HeroLists = new ObjectPool[m_HeroSources.Length];

for (int i = 0; i < m_HeroLists.Length; i++)
{
m_HeroLists[i] = new ObjectPool();
}
for (int j = 0; j < m_HeroSources.Length; j++)
{
m_HeroLists[j].Init(m_HeroSources[j], HeroDefine.m_HeroPoolSize, HeroDefine.m_HeroReallocSize);
}

GameObject hero = m_HeroLists[(int)jobT].Pop();
m_EffManager = hero.GetComponent<EffectManager>();
m_EffInfos = m_EffManager.m_EffSources;
m_HeroLists[(int)jobT].Push(hero);
}
}

/// <summary>
/// 放出一个英雄
/// </summary>
/// <param name="jobT"></param>
/// <param name="name"></param>
/// <returns></returns>
public static GameObject IssueHero(EnumDefine.JobType jobT, string name)
{
int index = (int)jobT;
if (index > m_HeroSources.Length)
{
return null;
}
GameObject obj2 = null;
obj2 = m_HeroLists[index].Pop();
if (obj2 == null)
{

}
obj2.SendMessage("SetName", name);
obj2.SendMessage("SetId", index);
if (!m_Objects.ContainsKey(name))
{
m_Objects.Add(name, obj2); //加入键值对索引,key:name
}
else
{
m_Objects.Remove(name);
m_Objects.Add(name, obj2);
}
if (!m_Heros.Contains(obj2))
{
m_Heros.Add(obj2);
}

return obj2;

}


public static GameObject IssueEff(int effId,EnumDefine.EffT effT)
{
int index = effId;

//if (index >= m_EffSources.Length)
//{
// return null;
//}
//if (index >= m_EffInfos.Count)
// return null;

GameObject effObj = null;
DebugLog.Log("IssueEff:" + index + " effT:" + effT);
if (effT == EnumDefine.EffT.HeroEffect)
{
effObj = m_EffTLists[EnumDefine.EffT.HeroEffect][index].Pop();
}
else if (effT == EnumDefine.EffT.PublicEffect)
{
effObj = m_EffTLists[EnumDefine.EffT.PublicEffect][index].Pop();
}

if (effObj == null)
{
Debug.LogError("In use all Object:" + index.ToString());
}
effObj.SendMessage("SetId", effId);

return effObj;

}

/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
/// <param name="bObjMon"></param>
/// <returns></returns>
public static GameObject IssueMon(int id, string name, bool bObjMon)
{
// MobListData mob_data = FileManager.FindMobListFromId(id);
// MobstructDataSet.Mobstruct mob_data = MobstructDataSet.Instance.GetKey(id);
Mob_ref_tblstructDataSet.Mob_ref_tblstruct mob_data = Mob_ref_tblstructDataSet.Instance.GetKey(id);

if (mob_data == null)
return null;

// MobRefData mobRef_Data = FileManager.FindMobRefFromId(mob_data.RefTbl);
// if (mobRef_Data == null)
// return null;
//
// int modeling = mobRef_Data.Modeling;
string modeling = mob_data.getModeling();
if (!m_MonSources.ContainsKey(modeling))
return null;

GameObject mob = null;
mob = m_MonLists[modeling].Pop();

if (mob == null)
{
// Debug.LogError("In use all Object:" + modeling.ToString());
}

mob.SendMessage("SetName", name);
mob.SendMessage("SetId", id);
mob.SendMessage("SetStatus");
m_Objects.Add(name, mob);

if(bObjMon)
{
m_ObjMonsters.Add(mob);
return mob;
}
m_Monsters.Add(mob);
return mob;

}


/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="name"></param>
/// <returns></returns>
public static GameObject IssueBullet(int id, string name)
{
int index = id;
if (index >= m_BulletSources.Length)
{
Debug.LogError("Not Found srcIdx:" + index.ToString());
return null;
}
GameObject obj = null;
obj = m_BulletLists[index].Pop();
if (obj == null)
Debug.LogError("In use all Object:" + index.ToString());

obj.SendMessage("SetName", name);
obj.SendMessage("SetId", id);
m_Objects.Add(name, obj);
return obj;
}


/// <summary>
/// 是否已加载过怪物资源
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static bool IsLoadedMonSource(int id)
{
//MobListData mobData = FileManager.FindMobListFromId(id);
Mob_ref_tblstructDataSet.Mob_ref_tblstruct mobData = Mob_ref_tblstructDataSet.Instance.GetKey(id);

if (mobData == null)
return false;

// MobRefData mobRefData = FileManager.FindMobRefFromId(id);
// if (mobRefData == null)
// return false;

string modeling = mobData.getModeling();
return m_MonSources.ContainsKey(modeling);
}

/// <summary>
/// 加载怪物资源
/// </summary>
/// <param name="id"></param>
/// <param name="count"></param>
public static void LoadMonSource(int id, int count)
{
//MobListData data = FileManager.FindMobListFromId(id);

Mob_ref_tblstructDataSet.Mob_ref_tblstruct data = Mob_ref_tblstructDataSet.Instance.GetKey(id);

if (data == null)
{

}
else
{

string modeling = data.getModeling();
if (m_MonSources.ContainsKey(modeling))
{

}
else
{
GameObject mob = null;
//AssetBundleManager.BundleInfo bundleInfo = AssetBundleManager.GetBundleInfo("Monsters.unity3d");

//if (bundleInfo != null)
//{
// AssetBundle bundle = AssetBundleManager.GetAssetBundle(NetworkHelper.patchUrl + bundleInfo.name,bundleInfo.version);
// string name = MonsterDefine.m_MonSrcPaths[modeling];
// name = name.Replace("Monsters/", string.Empty);
// obj2 = bundle.Load(name, typeof(GameObject)) as GameObject;

//}
//else
//{
//}
//mob = (GameObject)Resources.Load(MonsterDefine.m_MonSrcPaths[modeling],typeof(GameObject));
mob = (GameObject)Resources.Load(modeling, typeof(GameObject));
m_MonSources.Add(modeling, mob);
ObjectPool pool = new ObjectPool();
pool.Init(mob, count, 1);
m_MonLists.Add(modeling, pool);
}

}

}

/// <summary>
/// 查找对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static GameObject FindObject(string name)
{
if (m_Objects.ContainsKey(name))
{
return m_Objects[name];
}
else
{
return GameObject.Find(name);
}
}


public static int GetUid()
{
return ++m_UidCount;
}

/// <summary>
///
/// </summary>
/// <returns></returns>
public static int GetMonUid()
{
return m_MonUidCount++;
}

/// <summary>
/// 回收特效
/// </summary>
/// <param name="effId"></param>
/// <param name="pbObj"></param>
public static void PayBackEff(int effId, GameObject pbObj,EnumDefine.EffT effT = EnumDefine.EffT.HeroEffect)
{
int index = effId;

pbObj.transform.parent = null;

if (effT == EnumDefine.EffT.HeroEffect)
{
if (!m_EffLists[index].Push(pbObj))
Debug.LogError("Hero Eff ->Error PayBack Eff:" + index.ToString());
}
else if (effT == EnumDefine.EffT.PublicEffect)
{
if (!m_MonEffLists[index].Push(pbObj))
DebugLog.LogError("Monster Eff -> Error PayBack Eff:" + index);
}
if (index >= m_EffInfos.Count)
{
//Debug.LogError("Not Found srcIdx:" + index.ToString());
}
else
{



}
}

/// <summary>
/// 回收怪物
/// </summary>
/// <param name="id"></param>
/// <param name="pbObj"></param>
/// <param name="bObjMon"></param>
public static void PayBackMob(int id, GameObject pbObj, bool bObjMon)
{

//MobListData mobList_data = FileManager.FindMobListFromId(id);

Mob_ref_tblstructDataSet.Mob_ref_tblstruct mobList_data = Mob_ref_tblstructDataSet.Instance.GetKey(id);

if (mobList_data != null)
{
string modeling = mobList_data.getModeling();
//MobRefData mobRef_data = FileManager.FindMobRefFromId(mobList_data.RefTbl);

// if (mobRef_data != null)
{

if (!m_MonSources.ContainsKey(modeling))
{
Debug.LogError("Not Found srcIdx:" + modeling.ToString());
}
else
{
BaseEff[] compsInChildren = pbObj.GetComponentsInChildren<BaseEff>();
if (compsInChildren != null)
{
for (int i = 0; i < compsInChildren.Length; i++)
{
//停止特效播放
FxManager.StopEff(compsInChildren[i].gameObject);
}

}
BaseCharacter character = pbObj.GetComponent<BaseCharacter>();
m_Objects.Remove(character.GetName());

if (bObjMon)
{
if (m_ObjMonsters.Exists(x => x == pbObj))
{
m_ObjMonsters.Remove(pbObj);
}
}
else if (m_Monsters.Exists(x => x == pbObj))
{
m_Monsters.Remove(pbObj);
}
BaseCtrl ctrl = character.GetCtrl();
if (ctrl != null)
{
CtrlManager.PayBackMobCtrl(ctrl.GetId(), ctrl.gameObject);
}
if (!m_MonLists[modeling].Push(pbObj))
{
Debug.LogError("Error PayBack:" + modeling.ToString());
}

}
}
}

}


/// <summary>
/// 回收游戏物品
/// </summary>
/// <param name="id"></param>
/// <param name="pbObj"></param>
public static void PayBackGameItem(int id, GameObject pbObj)
{
int index = id;
if (index >= m_GameItemSources.Length)
{
Debug.LogError("Not Found Index:" + index.ToString());
}
else
{
m_Objects.Remove(pbObj.GetComponent<_GameItem>().GetName());

if (!m_GameItemLists[index].Push(pbObj))
Debug.LogError("Error PayBackGameItem:" + index.ToString());
}
}

/// <summary>
/// 回收子弹
/// </summary>
/// <param name="id"></param>
/// <param name="pbObj"></param>
public static void PayBackBullet(int id, GameObject pbObj)
{
int index = id;
if (index >= m_BulletSources.Length)
{
Debug.LogError("Not Found srcIdx:" + index.ToString());
}
else
{
BaseBullet bullet = pbObj.GetComponent<BaseBullet>();
m_Objects.Remove(bullet.GetName());
BaseCtrl ctrl = bullet.GetCtrl();
if (ctrl != null)
{
CtrlManager.PayBackBulletCtrl(ctrl.GetId(), ctrl.gameObject);
}
if (!m_BulletLists[index].Push(pbObj))
Debug.LogError("Error PayBackBullet:" + index);
}
}

/// <summary>
/// 回收英雄
/// </summary>
/// <param name="id"></param>
/// <param name="pbObj"></param>
public static void PayBackHero(int id, GameObject pbObj)
{
int index = id;
if (index >= m_HeroSources.Length)
{
Debug.LogError("Not Found srcIdx:" + index.ToString());
}
else
{
BaseCharacter component = pbObj.GetComponent<BaseCharacter>();
m_Objects.Remove(component.GetName());
if (m_Heros.Exists(x => x == pbObj))
{
m_Heros.Remove(pbObj);
}
BaseCtrl ctrl = component.GetCtrl();
if (ctrl != null)
{
CtrlManager.PayBackHeroCtrl(ctrl.GetId(), ctrl.gameObject);
}

if (!m_HeroLists[index].Push(pbObj))
{
Debug.LogError("Error PayBackHero:" + index.ToString());
}
}
}

}
}

消息路由器

ObjMsgRouter.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using Whales.Game.BO;
using Whales.Game.Data.StructData;
using UnityEngine;

namespace Whales.Game.Manager
{
/// <summary>
/// 消息路由器
/// </summary>
public class ObjMsgRouter
{

private static Dictionary<int, RelayMsgHandler> m_RelayMsgHandlers = new Dictionary<int, RelayMsgHandler>();


/// <summary>
///
/// </summary>
public static void Init()
{
m_RelayMsgHandlers.Clear();


m_RelayMsgHandlers.Add(0, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_Idle));

m_RelayMsgHandlers.Add(1, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_Move));

m_RelayMsgHandlers.Add(2, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_MoveTo));

m_RelayMsgHandlers.Add(11, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_LevelUp));

m_RelayMsgHandlers.Add(12, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_Rush));

m_RelayMsgHandlers.Add(14, new RelayMsgHandler(ObjMsgRouter.OnRelayMsg_Attack));

}


/// <summary>
/// 发送对象消息
/// </summary>
/// <param name="cmd"></param>
/// <param name="data"></param>
/// <returns></returns>
public static bool SendObjMsg(eCmd cmd, IBaseMsgData data)
{
GameObject go = ObjectManager.FindObject(data.GetReceiver());

if (go == null)
return false;

BaseObject component = go.GetComponent<BaseObject>();
if (component != null)
{
component.OnObjMsg(cmd, data);
}
else
{

}
return true;

}

/// <summary>
///
/// </summary>
/// <param name="o"></param>
private static void OnRelayMsg_Attack(object o)
{
SendObjMsg(eCmd.Attack, (Attack_MsgData)o);
}

private static void OnRelayMsg_MoveTo(object o)
{
SendObjMsg(eCmd.MoveTo, (MoveTo_MsgData)o);
}

private static void OnRelayMsg_Move(object o)
{
SendObjMsg(eCmd.Move, (Move_MsgData)o);
}

private static void OnRelayMsg_Idle(object o)
{
SendObjMsg(eCmd.Idle, (Idle_MsgData)o);
}

/// <summary>
///
/// </summary>
/// <param name="o"></param>
private static void OnRelayMsg_Rush(object o)
{
SendObjMsg(eCmd.Rush, (Rush_MsgData)o);
}

private static void OnRelayMsg_LevelUp(object o)
{
SendObjMsg(eCmd.LevelUp, (LevelUp_MsgData)o);
}


/// <summary>
/// 转发消息委托处理
/// </summary>
/// <param name="o"></param>
private delegate void RelayMsgHandler(object o);
}
}

声音

设计思想

使用bool值去判断音乐是否需要加载。

首先建立一个数组,里面有很多bool值,true或false。在改动的过程中,音乐不需要的时候把true改成false,场景里就不会加载,需要的时候再改成true就可以使用。

音乐是根据索引进行的,用两个数组,一个数组存放表示音乐是否可用的bool值,另一个存放音乐的路径,可以去加载。音乐路径和bool值是对应起来的,如果不需要就设为false,需要就设为true。

架构设计没有好坏之分,只要用来方便,扩展方便,维护方便,修改方便就是OK的。

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
using System;
using System.Collections.Generic;

namespace Whales.Game.Systems
{
// 静态工具类
public static class SoundDefine
{
// 音乐是否需要加载
// 可能有100多个音乐
public static bool[] m_SndBundleData = new bool[]
{
true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,true,false,false,false
};

// 音乐路径,Resources目录下
public static string[] m_SndSrcPaths = new string[]
{
"Sound/Audio1",
// ...
"Sound/Audio100"
}

}
}

SoundManager.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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using Whales.Game.Systems;

namespace Whales.Game.Mananger
{
// 音乐播放管理器
public static class SoundManager
{
// 音乐剪辑片段集合
public static AudioClip[] m_AudioClips;

// 播放器
private static AudioSource m_BGM;

// 是否初始化,默认false
private static bool m_bInitAssetBundle = false;

// 背景音乐最大音量
private static float m_CurBGMVolumeMax;

//
private static Dictionary<int, Vector3> m_ReserveSndInfo = new Dictionary<int, Vector3>();

// 初始化
public static void Init()
{
m_AudioClips = new AudioClip[SoundDefine.m_SndSrcPaths.Length];

for (int i = 0; i < SoundDefine.m_SndSrcPaths.Length; i++)
{
m_AudioClips[i] = Resources.Load(SoundDefine.m_SndSrcPaths[i], typeof(AudioClip)) as AudioClip;
}

m_BGM = null;
m_CurBGMVolumeMax = 0.7f;
}
}

// 加载资源
public static void InitForAssetBundle(string bundleName, int version)
{
if (!m_bInitAssetBundle)
{
m_bInitAssetBundle = true;

AssetBundle bundle = AssetBundleManager.GetAssetBundle(bundleName, version);

for(int i = 0; i < SoundDefine.m_SndSrcPaths.Length; i++)
{
string name = SoundDefine.m_SndSrcPaths[i];
name = name.Replace("Sounds/", string.Empty);
m_AudioClips[i] = bundle.Load(name, typeof(AudioClip)) as AudioClip;
}
}
}

public static AudioSource GetBGM()
{
return m_BGM;
}

public static void InitBGM()
{
GameObject bgm = GameObject.Find("BGM");

if (bgm != null)
m_BGM = bgm.GetComponent<AudioSource>();

else
Debug.LogError("Not Found BGM Object");

m_CurBGMVolumeMax = m_BGM.volume;
m_BGM.mute = false;
}

public static void SetMuteBGM(bool bMute)
{
m_BGM.mute = bMute;
}

public static bool PlayBGM(int sndId)
{
if ((sndId < 0) || sndId >= m_AudioClips.Length)
return false;

if (GeneralDefine.m_BGMOff)
return false;

if (m_BGM.isPlaying)
m_BGM.Stop();

m_BGM.clip = m_AudioClips[sndId];
m_BGM.loop = true;

if (sndId == SoundDefine.m_Win)
{
m_BGM.loop = false;
}

m_BGM.Play();
RefreshBGMVolume();
return true;
}

// 刷新音量
public static void RefreshBGMVolume(float change = 1f)
{
SetBGMVolume(m_CurBGMVolumeMax);
}

public static void SetBGMVolume(float vol)
{
m_BGM.volume = vol;
}

public static bool PlaySnd(int sndId, GameObject srcObj)
{
if (GeneralDefine.m_SndOff)
return false;

if (sndId < 0 || sndId >= m_AudioClips.Length)
return false;

if (!m_ReserveSndInfo.ContainsKey(sndId))
{
m_ReserveSndInfo.Add(sndId, srcObj.transform.position);
if (m_BGM != null)
{
RefreshBGMVolume(0.1f);
}
return true;
}
}

public static bool PlaySnd(int sndId)
{
if (sndId < 0 || sndId >= m_AudioClips.Length)
return false;
}
}

功能函数

1
2


战斗架构介绍

0%