少年修仙传客户端代码仓库
hch
2025-03-19 6770e1eb64c4282def45adb824b14b2a407fdd30
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
using System.Collections.Generic;
using UnityEngine;
using System;
 
 
 
public class GameObjectPoolManager : SingletonMonobehaviour<GameObjectPoolManager>
{
#if UNITY_EDITOR
    private Dictionary<int, DebugItem> m_DebugInstIDDict = null;
#endif
    private Dictionary<int, GameObjectPool> m_PoolDict = null;
 
    // 不需要销毁的对象列表
    private List<int> dontDestoryGoInstIDList = null;
 
    private Transform m_TargetContainer;
 
    public void Initialize()
    {
        if (m_PoolDict == null)
        {
            m_PoolDict = new Dictionary<int, GameObjectPool>();
        }
 
        if (!m_TargetContainer)
        {
            m_TargetContainer = new GameObject("Container").transform;
            m_TargetContainer.SetParent(transform);
        }
 
        if (dontDestoryGoInstIDList == null)
        {
            dontDestoryGoInstIDList = new List<int>();
        }
 
#if UNITY_EDITOR
        if (m_DebugInstIDDict == null)
        {
            m_DebugInstIDDict = new Dictionary<int, DebugItem>();
        }
#endif
    }
 
    public void AddDontDestroyGoInstID(int id)
    {
        if (!dontDestoryGoInstIDList.Contains(id))
        {
            dontDestoryGoInstIDList.Add(id);
        }
    }
 
    public bool ExistPool(GameObject prefab)
    {
        if (prefab == null)
        {
            return false;
        }
 
        if (m_PoolDict.ContainsKey(prefab.GetInstanceID()))
        {
            return true;
        }
 
        return false;
    }
 
    public GameObjectPool RequestPool(GameObject prefab)
    {
        if (prefab == null)
        {
            return null;
        }
 
        int _prefabInstanceId = prefab.GetInstanceID();
 
        GameObjectPool _pool = null;
        if (m_PoolDict.TryGetValue(_prefabInstanceId, out _pool))
        {
            return _pool;
        }
 
        _pool = new GameObjectPool(prefab);
        m_PoolDict[_prefabInstanceId] = _pool;
 
        return _pool;
    }
 
 
    public void CacheGameObject(GameObject prefab, int count, bool _prefabActive)
    {
        if (prefab == null)
        {
            return;
        }
 
        RequestPool(prefab).Cache(count, _prefabActive);
    }
 
    public void CacheNpc(int npcID, int count, bool prefabActive)
    {
        string _assetName;
        string _assetBundleName;
 
        NPCConfig _m = NPCConfig.Get(npcID);
 
        if (_m == null || _m.MODE.Equals("0"))
        {
            return;
        }
 
        if (GAMgr.Instance.s_NpcID2Assetname.TryGetValue(npcID, out _assetName))
        {
            _assetBundleName = GAMgr.Instance.s_NpcID2BundleName[npcID];
        }
        else
        {
            _assetName = StringUtility.Contact(InstanceResourcesLoader.raceSuffix, _m.MODE);
            _assetBundleName = StringUtility.Contact(ResourcesPath.MOB_FOLDER_NAME, _assetName);
            GAMgr.Instance.s_NpcID2Assetname[npcID] = _assetName;
            GAMgr.Instance.s_NpcID2BundleName[npcID] = _assetBundleName;
        }
 
        GameObject _prefab = InstanceResourcesLoader.LoadMob(_assetBundleName, _assetName);
 
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        _pool.assetBundleName = _assetBundleName;
        _pool.assetName = _assetName;
 
        _pool.Cache(count, prefabActive);
    }
 
    public GameObject RequestGameObject(GameObject prefab)
    {
        if (prefab == null)
        {
            return null;
        }
 
        return RequestPool(prefab).Request();
    }
 
    public GameObject RequestEmptyJY()
    {
        var _prefab = InstanceResourcesLoader.LoadEmptyJY();
        if (!_prefab)
        {
            return null;
        }
 
        var _pool = RequestPool(_prefab);
        _pool.assetName = InstanceResourcesLoader.emptyJyName;
        _pool.assetBundleName = "gmodels/prefab_race_jy";
 
        return _pool.Request();
    }
 
    public void ReleaseEmptyJY(GameObject go)
    {
        var _prefab = InstanceResourcesLoader.LoadEmptyJY();
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        if (_pool != null)
        {
            _pool.Release(go);
        }
    }
 
    public GameObject RequestDefaultPet()
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[2][0],
                                                             GeneralDefine.ModeDefaultConfig[2][1]);
 
        if (!_prefab)
        {
            return null;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        _pool.assetName = GeneralDefine.ModeDefaultConfig[2][1];
        _pool.assetBundleName = GeneralDefine.ModeDefaultConfig[2][0];
 
        return _pool.Request();
    }
 
    public GameObject RequestDefaultFightNpc()
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[1][0],
                                                             GeneralDefine.ModeDefaultConfig[1][1]);
 
        if (!_prefab)
        {
            return null;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        _pool.assetName = GeneralDefine.ModeDefaultConfig[1][1];
        _pool.assetBundleName = GeneralDefine.ModeDefaultConfig[1][0];
 
        return _pool.Request();
    }
 
    public GameObject RequestDefaultFuncNpc()
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[0][0],
                                                             GeneralDefine.ModeDefaultConfig[0][1]);
 
        if (!_prefab)
        {
            return null;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        _pool.assetName = GeneralDefine.ModeDefaultConfig[0][1];
        _pool.assetBundleName = GeneralDefine.ModeDefaultConfig[0][0];
 
        return _pool.Request();
    }
 
    public void ReleaseDefaultPet(GameObject go)
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[2][0],
                                                             GeneralDefine.ModeDefaultConfig[2][1]);
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        if (_pool != null)
        {
            _pool.Release(go);
        }
    }
 
    public void ReleaseDefaultHorse(GameObject go)
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[3][0],
                                                             GeneralDefine.ModeDefaultConfig[3][1]);
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        if (_pool != null)
        {
            _pool.Release(go);
        }
    }
 
    public void ReleaseDefaultFightNPC(GameObject go)
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[1][0],
                                                             GeneralDefine.ModeDefaultConfig[1][1]);
 
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        if (_pool != null)
        {
            _pool.Release(go);
        }
    }
 
    public void ReleaseDefaultFuncNPC(GameObject go)
    {
        GameObject _prefab = InstanceResourcesLoader.LoadMob(GeneralDefine.ModeDefaultConfig[0][0],
                                                             GeneralDefine.ModeDefaultConfig[0][1]);
 
        if (!_prefab)
        {
            return;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        if (_pool != null)
        {
            _pool.Release(go);
        }
    }
 
    public GameObject RequestNpcGameObject(int npcID)
    {
        string _assetName;
        string _assetBundleName;
 
        NPCConfig _m = NPCConfig.Get(npcID);
 
        if (_m == null || _m.MODE.Equals("0"))
        {
            return null;
        }
 
        if (GAMgr.Instance.s_NpcID2Assetname.TryGetValue(npcID, out _assetName))
        {
            _assetBundleName = GAMgr.Instance.s_NpcID2BundleName[npcID];
        }
        else
        {
            _assetName = StringUtility.Contact(InstanceResourcesLoader.raceSuffix, _m.MODE);
            _assetBundleName = StringUtility.Contact(ResourcesPath.MOB_FOLDER_NAME, _assetName);
            GAMgr.Instance.s_NpcID2Assetname[npcID] = _assetName;
            GAMgr.Instance.s_NpcID2BundleName[npcID] = _assetBundleName;
        }
 
        GameObject _prefab = InstanceResourcesLoader.LoadMob(_assetBundleName, _assetName);
 
        if (!_prefab)
        {
            return null;
        }
 
        GameObjectPool _pool = RequestPool(_prefab);
 
        _pool.assetBundleName = _assetBundleName;
        _pool.assetName = _assetName;
 
        return _pool.Request();
    }
 
    public void ReleaseGameObject(GameObject prefab, GameObject go)
    {
        GameObjectPool _pool = RequestPool(prefab);
        if (_pool == null)
        {
            return;
        }
        _pool.Release(go);
    }
 
    public void Release(GameObject prefab)
    {
        int _prefabInstanceId = prefab.GetInstanceID();
 
        GameObjectPool _pool = null;
        if (m_PoolDict.TryGetValue(_prefabInstanceId, out _pool))
        {
            _pool.Clear();
 
            if (_pool.IsEmpty())
            {
                m_PoolDict.Remove(_pool.PrefabInstanceId);
            }
        }
    }
 
    public void UnLoadNPC(int npcID)
    {
        if (!AssetSource.mobFromEditor)
        {
            string _assetName;
            string _assetBundleName;
 
            if (!GAMgr.Instance.s_NpcID2Assetname.TryGetValue(npcID, out _assetName))
            {
                return;
            }
            _assetBundleName = GAMgr.Instance.s_NpcID2BundleName[npcID];
 
            if (!AssetBundleUtility.Instance.JudgeExistAssetBundle(_assetBundleName))
            {
                return;
            }
 
            GameObject _prefab = InstanceResourcesLoader.LoadMob(_assetBundleName, _assetName);
 
            if (!_prefab)
            {
                return;
            }
 
            if (!ExistPool(_prefab))
            {
                return;
            }
 
            GameObjectPool _pool = RequestPool(_prefab);
            _pool.Clear();
            m_PoolDict.Remove(_pool.PrefabInstanceId);
        }
    }
 
    public void UnLoadAll(bool force = false)
    {
        List<int> _removeList = new List<int>();
        foreach (var _pool in m_PoolDict.Values)
        {
            if (GAMgr.Instance.needDestroyPrefabList.Contains(_pool.Prefab))
            {
                _pool.Clear();
                _removeList.Add(_pool.PrefabInstanceId);
            }
        }
 
        for (int i = 0; i < _removeList.Count; ++i)
        {
            m_PoolDict.Remove(_removeList[i]);
        }
    }
 
    public class GameObjectPool
    {
        private GameObject m_Prefab = null;
        public GameObject Prefab
        {
            get
            {
                return m_Prefab;
            }
        }
        public int PrefabInstanceId
        {
            get; private set;
        }
 
        public List<GameObject> m_ActiveList = null;
        private List<GameObject> m_FreeList = null;
 
        Action<GameObject> releaseCallBack;
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
        private Transform m_DebugContainer;
        private Transform m_DebugActive;
        private Transform m_DebugFree;
#endif
 
        public string assetBundleName;
        public string assetName;
 
        internal protected GameObjectPool(GameObject prefab)
        {
            m_Prefab = prefab;
            PrefabInstanceId = m_Prefab.GetInstanceID();
            m_ActiveList = new List<GameObject>();
            m_FreeList = new List<GameObject>();
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
            m_DebugContainer = new GameObject(prefab.name).transform;
            m_DebugActive = new GameObject("Active").transform;
            m_DebugFree = new GameObject("Free").transform;
            m_DebugActive.SetParent(m_DebugContainer);
            m_DebugFree.SetParent(m_DebugContainer);
            m_DebugContainer.SetParent(Instance.transform);
#endif
        }
 
        public void AddReleaseListener(Action<GameObject> _callBack)
        {
            releaseCallBack = _callBack;
        }
 
        public void RemoveReleaseListener()
        {
            releaseCallBack = null;
        }
 
        public void Cache(int count, bool _prefabActive)
        {
            GameObject _go;
            for (int i = 0; i < count; ++i)
            {
                _go = Instantiate(m_Prefab, Constants.Special_Hide_Position, Quaternion.identity);
                m_FreeList.Add(_go);
                _go.transform.SetParent(Instance.m_TargetContainer);
                _go.transform.position = Constants.Special_Hide_Position;
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
 
                DebugItem _debugItem = new GameObject(_go.GetInstanceID().ToString()).AddMissingComponent<DebugItem>();
                _debugItem.transform.SetParent(m_DebugFree);
                _debugItem.target = _go;
                Instance.m_DebugInstIDDict[_go.GetInstanceID()] = _debugItem;
#endif
                var animator = _go.GetComponent<Animator>();
                if (animator != null)
                {
                    animator.enabled = false;
                }
                _go.SetActive(_prefabActive);
            }
        }
 
        public GameObject Request()
        {
            GameObject _go = null;
 
            while (m_FreeList.Count > 0)
            {
                if (m_FreeList[0] != null)
                {
                    _go = m_FreeList[0];
 
                    m_ActiveList.Add(_go);
                    m_FreeList.RemoveAt(0);
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                    if (Instance.m_DebugInstIDDict.ContainsKey(_go.GetInstanceID()))
                    {
                        DebugItem _debugItem = Instance.m_DebugInstIDDict[_go.GetInstanceID()];
                        _debugItem.transform.SetParent(m_DebugActive);
                    }
#endif
                    break;
                }
                else
                {
                    m_FreeList.RemoveAt(0);
                }
            }
 
            if (_go == null)
            {
                _go = Instantiate(m_Prefab, Constants.Special_Hide_Position, Quaternion.identity);
                m_ActiveList.Add(_go);
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                DebugItem _debugItem = new GameObject(_go.GetInstanceID().ToString()).AddMissingComponent<DebugItem>();
                _debugItem.transform.SetParent(m_DebugActive);
                _debugItem.target = _go;
                Instance.m_DebugInstIDDict[_go.GetInstanceID()] = _debugItem;
#endif
            }
 
            _go.transform.SetParent(null);
            _go.transform.localScale = Vector3.one;
 
            return _go;
        }
 
        public void Release(GameObject go)
        {
            if (go == null)
            {
                return;
            }
            if (m_ActiveList.Contains(go))
            {
                m_ActiveList.Remove(go);
            }
            if (m_FreeList.Contains(go))
            {
                return;
            }
 
            m_FreeList.Add(go);
            go.transform.SetParent(Instance.m_TargetContainer);
            go.transform.position = Constants.Special_Hide_Position;
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
            if (Instance.m_DebugInstIDDict.ContainsKey(go.GetInstanceID()))
            {
                DebugItem _debugItem = Instance.m_DebugInstIDDict[go.GetInstanceID()];
                _debugItem.transform.SetParent(m_DebugFree);
            }
#endif
 
            if (releaseCallBack != null)
            {
                releaseCallBack(go);
            }
        }
 
        public void ReleaseAll()
        {
            for (int i = m_ActiveList.Count - 1; i >= 0; i--)
            {
                var _go = m_ActiveList[i];
                m_ActiveList.Remove(_go);
 
                if (!m_FreeList.Contains(_go))
                {
                    m_FreeList.Add(_go);
                }
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                DebugItem _debugItem = Instance.m_DebugInstIDDict[_go.GetInstanceID()];
                _debugItem.transform.SetParent(m_DebugFree);
#endif
                _go.transform.SetParent(Instance.m_TargetContainer);
                _go.transform.position = Constants.Special_Hide_Position;
 
                if (releaseCallBack != null)
                {
                    releaseCallBack(_go);
                }
            }
        }
 
        public void Clear()
        {
            for (int i = m_ActiveList.Count - 1; i >= 0; --i)
            {
                if (m_ActiveList[i] == null
                || Instance.dontDestoryGoInstIDList.Contains(m_ActiveList[i].GetInstanceID()))
                    continue;
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                Destroy(Instance.m_DebugInstIDDict[m_ActiveList[i].GetInstanceID()].gameObject);
#endif
                Destroy(m_ActiveList[i]);
                m_ActiveList.RemoveAt(i);
            }
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
            if (m_ActiveList.Count != 0)
            {
                Debug.LogWarningFormat("{0} 池清理后, 还有 {1} 个活跃对象. ", m_Prefab.name, m_ActiveList.Count);
                for (int i = 0; i < m_ActiveList.Count; ++i)
                {
                    Debug.LogWarningFormat(" |-- {0}", m_ActiveList[i].name);
                }
            }
#endif
 
            for (int i = m_FreeList.Count - 1; i >= 0; --i)
            {
                if (m_FreeList[i] == null
                 || Instance.dontDestoryGoInstIDList.Contains(m_FreeList[i].GetInstanceID()))
                    continue;
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                Destroy(Instance.m_DebugInstIDDict[m_FreeList[i].GetInstanceID()].gameObject);
#endif
                Destroy(m_FreeList[i]);
                m_FreeList.RemoveAt(i);
            }
 
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
            if (m_FreeList.Count != 0)
            {
                Debug.LogWarningFormat("{0} 池清理后, 还有 {1} 个空闲对象. ", m_Prefab.name, m_FreeList.Count);
                for (int i = 0; i < m_FreeList.Count; ++i)
                {
                    Debug.LogWarningFormat(" |-- {0}", m_FreeList[i].name);
                }
            }
#endif
            if (IsEmpty())
            {
                m_Prefab = null;
#if UNITY_EDITOR && !UNITY_ANDROID && !UNITY_IOS
                Destroy(m_DebugContainer.gameObject);
#endif
            }
 
            if (!string.IsNullOrEmpty(assetBundleName) && !string.IsNullOrEmpty(assetName))
            {
                InstanceResourcesLoader.UnloadAsset(assetBundleName, assetName);
            }
        }
 
        public bool IsEmpty()
        {
            return m_ActiveList.Count == 0 && m_FreeList.Count == 0;
        }
    }
}