Unity3D快速实现UI架构设计(一)

发表于2017-05-15
评论2 1.6k浏览

想免费获取内部独家PPT资料库?观看行业大牛直播?点击加入腾讯游戏学院游戏程序行业精英群

711501594

在使用Unity3D引擎做开发时,UI资源的加载,卸载,隐藏以及UI渐变动画等功能是UI架构设计必须要想到的。

考虑到每做一款游戏都需要将这些功能编写一遍非常的浪费时间,下面就给大家分享一种快速实现UI架构设计的方式。


PS:因为我们这个是通用的模块,所以必须要使用模板实现,而且我们的逻辑脚本是不挂接到对象上的。

接下来首先设计一个管理类Manager,代码如下所示:

?
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
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
///  
/// 抽象管理类 
///  
///  
///  
public class Manager""> : Singleton 
    where V : class ,IDisposable 
    where T : Singleton, new() 
    protected Dictionary""> mMap = new Dictionary"">(); 
    
    ///  
    /// 获取 对应实体 
    ///  
    ///  
    ///  
    public V Get(K key) 
    
        if (key == null) return null
        return mMap.ContainsKey(key) ? mMap[key] : null
    
   
    ///  
    /// 获取类型T的 Value 
    ///  
    ///  
    ///  
    ///  
    public U Get(K key) where U : class,V  
    
        V v = Get(key); 
        return v as U; 
    
   
    ///  
    /// 获取类型T的Value 
    ///  
    ///  
    ///  
    public T Get() where T : class,V  
    
        foreach(V value in mMap.Values) 
        
            if(value.GetType().Equals(typeof(T))) 
            
                return value as T; 
            
        
        return null
    
    ///  
    /// 添加对应实体 
    ///  
    ///  
    ///  
    public bool Put(K key, V value) 
    
        if (mMap.ContainsKey(key)) 
        
            if (value == mMap[key]) 
            
                return false
            
            V v = mMap[key]; 
            mMap[key] = value; 
            v.Dispose(); 
        
        else 
        
            mMap.Add(key, value); 
        
        return true
    
   
    ///  
    /// 删除 
    ///  
    ///  
    ///  
    public bool Remove(K key) 
    
        if (mMap.ContainsKey(key)) 
        
            V v = mMap[key]; 
            mMap.Remove(key); 
            v.Dispose(); 
        
        return true
    
   
   
    public Dictionary.ValueCollection Values 
    
        get { return mMap.Values; } 
    
    ///  
    /// 清除所有管理的对象 
    ///  
    public void Clear() 
    
        foreach (V value in mMap.Values) 
        
            value.Dispose(); 
        
        mMap.Clear(); 
    
   
public class ManagerT""> : Manager 
    where V : class ,IDisposable 
   
 

在这个类属于抽象类,它利用Dictionary实现了对象的管理操作,接下来需要实现UI的管理类了,先把代码给读者展示如下:

?
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
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using UnityEngine; 
   
///  
/// UI 服务类 
///  
public class UIService : Managerstring,="" uiservice.ui="">,IDisposable 
    public void Dispose() 
    
        DestroyAll(); 
    
   
    ///  
    /// 创建UI 
    ///  
    ///  
    ///  
    ///  
    private T _CreateUI(string name) where T : UI 
    
        T ui = Activator.CreateInstance(typeof(T), name) as T; 
        return ui; 
    
   
    public class UIHolder : MonoBehaviour 
    
        public UI ui { get; set; } 
    
    public class UI : IDisposable 
    
        public UI(string name) 
        
                _Init(name);   
        
   
        public enum UIStyle 
        
            Normal,//默认类型 
            HideByTapScene,//点击空白处隐藏类型,和黑底不冲突 
        
   
        UIStyle mStyle = UIStyle.Normal; 
   
        public UIStyle Style 
        
            get { return mStyle; } 
            set { mStyle = value; } 
        
   
        internal GameObject mPrefab = null
        protected UIPanel mPanel = null
        #region 面板动画处理 
        protected UITweener[] mTweens = null
        protected UITweener   mMainTween = null
        protected UITweener.ToggleStyle mMainToggleStyle = UITweener.ToggleStyle.normal; 
        private void _TweensInit() 
        
            mTweens = mPrefab.GetComponentsInChildren(); 
            if (mTweens != null
            
                UITweener tween; 
                for (int i = 0; i < mTweens.Length; ++i) 
                
                    tween = mTweens[i]; 
                    if (tween.toggleStyle == UITweener.ToggleStyle.OnShow) 
                    
                        mMainTween = tween; 
                        mMainToggleStyle = UITweener.ToggleStyle.OnShow; 
                    
                    if (tween.toggleStyle == UITweener.ToggleStyle.OnShowAndHide) 
                    
                        mMainTween = tween; 
                        mMainToggleStyle = UITweener.ToggleStyle.OnShowAndHide; 
                        break
                    
                
            
        
   
        private bool _HasTween() 
        
            return mMainTween != null && mMainTween.toggleStyle !=  UITweener.ToggleStyle.normal; 
        
        private void _TweenOnShow(EventDelegate.Callback call) 
        
            mMainTween.SetOnFinished(call); 
            UITweener tween; 
            for (int i = 0; i < mTweens.Length; ++i) 
            
                tween = mTweens[i]; 
                if(tween.toggleStyle != UITweener.ToggleStyle.normal) 
                
                    tween.ResetToStart(true); 
                    tween.PlayForward(); 
                
            
        
   
        private void _TweenOnHide(EventDelegate.Callback call) 
        
            if (mMainToggleStyle == UITweener.ToggleStyle.OnShow) 
            
                call(); 
                return
            
            mMainTween.SetOnFinished(call); 
            UITweener tween; 
            for (int i = 0; i < mTweens.Length; ++i) 
            
                tween = mTweens[i]; 
                if (tween.toggleStyle == UITweener.ToggleStyle.OnShowAndHide) 
                
                    tween.ResetToStart(!true); 
                    tween.PlayReverse(); 
                
            
        
        private void _Dummy() 
        
   
        
        private void _Hide() 
        
            mPrefab.SetActive(false); 
            mHiding = false
        
        #endregion 
   
        internal void _Init(string name) 
        
            try 
            
                GameObject prefab = Resource.LoadUI(name); 
                mPrefab = GameObject.Instantiate(prefab) as GameObject; 
                UIHolder uiholder = UtilGameObject.GetOrAddComponent(mPrefab); 
                uiholder.ui = this
                mPrefab.name = name; 
                mPanel = mPrefab.GetComponent(); //允许Panel为空 
                _TweensInit(); 
            
            catch (Exception e) 
            
                Looper.LogException(e); 
            
            UIService.Instance.InitUI(mPrefab); 
            UIService.Instance._OnShowUI(this, true); 
        
        #region <默认属性> 
    
           
        ///  
        /// 深度信息 
        ///  
        public int depth 
        
            get 
            
                if (mPanel==null
                
                    return -1; 
                
                return mPanel.depth; 
            
            set 
            
                if (mPanel == null) return
                mPanel.depth = value; 
            
        
        ///  
        /// 是否正在显示 
        ///  
        ///  
        public bool IsShowing() 
        
            if (mPrefab == null
                return false
            return mPrefab.activeSelf; 
        
        float mLastShowTime = Time.time; 
        ///  
        /// 最后一次显示的时间 
        ///  
        public float LastShowTime 
        
            get { return mLastShowTime; } 
            set { mLastShowTime = value; } 
        
        #endregion 
   
        ///  
        /// 名字(和预制件名称一样) 
        ///  
        public  string Name 
        
            get { return mPrefab != null ? mPrefab.name : ""; } 
        
        ///  
        /// 销毁对象 
        ///  
        public void Dispose() 
        
            UIService.Instance._OnHidUI(this); 
   
            try 
            
                OnClose(); 
                if (mPrefab != null
                
                    GameObject.DestroyImmediate(mPrefab); 
                    mPrefab = null
                
   
            
            catch (Exception e) 
            
                Looper.LogException(e); 
            
               
        
   
        ///  
        /// 刷新界面 
        /// 1 Grid 重排问题 
        ///  
        public void Refresh() 
        
              
                UIGrid[] grids = mPrefab.GetComponentsInChildren(); 
                foreach (UIGrid grid in grids) 
                
                    if (grid != null && !grid.animateSmoothly) 
                    
                        grid.Reposition(); 
                    
                
              
        
   
   
        public IEnumerator _Refresh() 
        
            
                UIGrid[] grids = mPrefab.GetComponentsInChildren(); 
                foreach (UIGrid grid in grids) 
                
                    if (grid != null && !grid.enabled) 
                    
                        grid.repositionNow = true
                        grid.Reposition(); 
                    
                
            
   
            yield return null
        
   
   
        //解决同一个UI动画隐藏还没结束,动画显示就开始了 状态不对的问题 
        bool mHiding = false
        ///  
        /// 是否显示 
        ///  
        public void Show(bool v) 
        
   
            if (v) 
            
                if (mHiding) 
                
                    _Hide(); 
                
                UIService.Instance._OnShowUI(this); 
            
            else 
            
                UIService.Instance._OnHidUI(this); 
            
   
            try 
            
   
                if (mPrefab != null
                
                       
                    if (_HasTween()) 
                    
                        if (v) 
                        
                            mPrefab.SetActive(true); 
                            _TweenOnShow(_Dummy); 
                        
                        else 
                        
                            mHiding = true
                            _TweenOnHide(_Hide); 
                        
                    
                    else 
                    
                        mPrefab.SetActive(v); 
                    
                    OnShow(v); 
                    if (v) 
                    
                        LastShowTime = Time.time; 
                        Refresh(); 
                        //(); 
                    
                
                   
            
            catch (Exception e) 
            
                Looper.LogException(e); 
            
   
   
        
   
   
        ///  
        /// 跟随场景中物体 
        ///  
        ///  
        public void ApplyHub(Transform target) 
        
            NGUIExt guiExt = PluginManager.Instance.Get(); 
            if (guiExt != null
            
                guiExt.ApplyHud(mPrefab, target); 
            
        
   
        public virtual void OnCreate() { } 
        public virtual void OnShow(bool v) { } 
        public virtual void OnClose() { } 
   
        ///  
        /// 点击事件 
        ///  
        /// 被点击的控件 
        public virtual void OnClick(GameObject obj) { } 
   
        ///  
        /// 双击事件 
        ///  
        /// 被双击的控件 
        public virtual void OnDoubleClick(GameObject obj) { } 
        ///  
        /// 按住事件 
        ///  
        /// 被Pressed的控件 
        ///  
        public virtual void OnPress(GameObject obj, bool isPressed) { } 
   
        ///  
        /// 拖动事件 
        ///  
        /// 拖动的控件 
        ///  
        public virtual void OnDrag(GameObject obj, Vector2 delta) { } 
   
        ///  
        /// 拖放事件 
        ///  
        /// 拖放的当前控件 
        /// 一直被拖住的控件 
        public virtual void OnDrap(GameObject obj, GameObject objSelected) { } 
   
        ///  
        /// 显示Tooltip事件 
        ///  
        ///  
        /// 显示或隐藏Tooltip 
        public virtual void OnTooltip(GameObject obj, bool isShow) { } 
   
        ///  
        /// 被选中事件 
        ///  
        /// 被选中的控件 
        /// 选中或取消被选中 
        public virtual void OnSelect(GameObject obj, bool isSelected) { } 
   
        ///  
        /// 光标划过事件 
        ///  
        ///  
        /// 光标进入或光标离开 
        public virtual void OnHover(GameObject obj, bool isHover) { } 
   
        ///  
        /// 根据类型获取对应名称控件; 
        ///  
        ///  
        ///  
        ///  
        protected T GetChild(string name,GameObject obj = null, int index = 0) where T : MonoBehaviour 
        
            if(obj == null
            
                obj = mPrefab; 
            
   
            Transform child = obj.transform.Find(name); 
            if (child == null
            
                T[] childs = obj.GetComponentsInChildren(); 
                foreach (T t in childs) 
                
                    if (t.gameObject.name == name) 
                    
                        return t; 
                    
                
            
            else 
            
                if (child.childCount == 0) 
                
                    return child.GetComponent(); 
                
   
                
                    int count = 0; 
                    T[] comps = child.GetComponents(); 
                    foreach (T t in comps) 
                    
   
                        if (t.gameObject.name == name && count == index) 
                        
                            return t; 
                        
                        count++; 
                    
                
                
                    int count = 0; 
                    T[] childs = child.GetComponentsInChildren(); 
                    foreach (T t in childs) 
                    
   
                        if (t.gameObject.name == name && count == index) 
                        
                            return t; 
                        
                        count++; 
                    
                
   
            
   
            Debug.LogError(obj.name + " hasn't Components :" + typeof(T).Name + " in children named:" + name); 
            return null
        
   
        protected GameObject FindChild(string name) 
        
            var child = mPrefab.transform.FindChild(name); 
            return child.gameObject; 
        
   
        protected T FindChild(string name) where T : Component 
        
            var child = mPrefab.transform.FindChild(name); 
            T cmp = child.GetComponent(); 
           
            return cmp; 
        
   
        ///  
        /// 获取对应名称子控件; 
        ///  
        ///  
        ///  
        protected GameObject GetChild(string name, GameObject obj = null
        
            if(obj == null
            
                obj = mPrefab; 
            
            Transform child = obj.transform.Find(name); 
            if (child == null
            
                Debug.LogError(obj.name + "is not find child of:" + name); 
                return null
            
            return child.gameObject; 
        
        ///  
        /// 用指定的对象替换子对象 
        ///  
        /// 子对象名字 
        /// 指定的对象 
        ///  
        protected bool ReplaceChild(string name, GameObject obj) 
        
            GameObject orginal = GetChild(name); 
   
            if (orginal == null) { 
                GameObject.DestroyImmediate (obj); 
                return false
            
            obj.transform.parent = orginal.transform.parent; 
            obj.transform.localPosition = Vector3.zero; 
            obj.transform.localRotation = Quaternion.identity; 
            obj.transform.localScale = Vector3.one; 
            obj.name = orginal.name; 
   
            orginal.transform.parent = null
            GameObject.DestroyImmediate (orginal); 
   
            return true
        
   
        ///  
        /// 用指定的对象替换子对象 
        ///  
        /// 原件 
        /// 指定的对象 
        ///  
        protected bool ReplaceChild(GameObject orginal, GameObject obj) 
        
            if (orginal == null) { 
                GameObject.DestroyImmediate (obj); 
                return false
            
            obj.transform.parent = orginal.transform.parent; 
            obj.transform.localPosition = Vector3.zero; 
            obj.transform.localRotation = Quaternion.identity; 
            obj.transform.localScale = Vector3.one; 
            obj.name = orginal.name; 
   
            orginal.transform.parent = null
            GameObject.DestroyImmediate (orginal); 
            return true
        
   
    
   
   
    NGUIExt mPlugin; 
    GameObject mBG; 
    UIPanel mBGPanel; 
    int mBGCount = 0; 
   
    private void _OnShowUI(UI t,bool init = false
    
        if (t.IsShowing()) 
        
            if(!init) 
                return
        
        if (t.IsShowBlackBG()) 
        
            mBGCount++; 
            if (mBGCount > 0 && mBG.activeSelf == false
            
                mBG.SetActive(true); 
            
            if (mBG.activeSelf) 
            
                mBGPanel.depth = t.depth-1; 
            
        
   
    
    private void _OnHidUI(UI t) 
    
        if (!t.IsShowing()) return
   
        if (t.IsShowing() && t.IsShowBlackBG()) 
        
            mBGCount--; 
            if(mBGCount <= 0) 
            
                mBGCount = 0; 
                if (mBG.activeSelf == true
                
                    mBG.SetActive(false); 
                
            
   
   
            if (mBG.activeSelf) 
            
                UI ui = _GetLastShowUI(true, t); 
                if (ui != null) mBGPanel.depth = ui.depth-1; 
            
        
   
    
   
    public bool IsFingerHoverGUI() 
    
        if(mPlugin == null
        
            return false
        
        return mPlugin.IsFingerHoverGUI; 
    
   
    public bool IsFingerHoverGUI3D() 
    
        if (mPlugin == null
        
            return false
        
        return mPlugin.IsFingerHoverGUI3D(); 
    
   
    public bool IsFingerHoverGUIWithout3D() 
    
        if (mPlugin == null
        
            return false
        
        return mPlugin.IsFingerHoverGUIWithout3D(); 
    
   
    public bool IsPrefab(GameObject ui_prefab) 
    
        return mPlugin.IsPrefab(ui_prefab); 
    
       
    ///  
    /// 初始化 继承与 Singleton 对象构建的时候被调用 
    ///  
    protected override void OnCreate() 
    
           
        mPlugin = PluginManager.Instance.Get(); 
        if (mPlugin != null
        
            mPlugin.SetEventHandler(this._HandlerUIEvent); 
        
        GameObject prefab = Resource.LoadUICommon("black_background"); 
        Looper.Assert(prefab != null , "默认黑底 black_background Prefab不存在 !!"); 
        if(prefab != null
        
            mBG = mPlugin.AddChild(prefab); 
            mBGPanel = mBG.GetComponent(); 
            mBG.transform.localScale = new Vector3(1, 1, 1); 
            mBG.transform.localPosition = new Vector3(mBG.transform.localPosition.x, mBG.transform.localPosition.y, Mathf.Clamp(mBG.transform.localPosition.z, -2f, 2f)); 
            if(mBG != null
            
                UISprite sprite = mBG.GetComponent(); 
                BoxCollider collider = mBG.GetComponent(); 
                if(sprite!= null
                
                    Vector2 size = mPlugin.GetSize(); 
                    sprite.SetDimensions((int)size.x, (int)size.y); 
                    collider.size = new Vector3(size.x, size.y, 0); 
                
            
               
            mBG.SetActive(false); 
        
        //EasyTouch.On_SimpleTap += On_SimpleTap; 
           
    
   
    public void InitUI(GameObject ui) 
    
        try 
        
            LayerUtils.SetLayer(ui.transform, (int)LayerUtils.ELayerIndex.ui); 
            ui.transform.parent = mPlugin.GetRoot().transform; 
               
            switch(ui.name) 
            
                default
                    ui.transform.localScale = new Vector3(1, 1, 1); 
                    ui.transform.localPosition = Vector3.zero; 
                    break
            
        
        catch (Exception e) 
        
            Looper.LogException(e); 
        
         
    
    ///  
    /// 创建UI 
    ///  
    ///  
    ///  
    ///  
    public T CreateUI(string name) where T: UI 
    
        UI  t = this.Get(name) as T; 
        if(t == null
        
            t = _CreateUI(name) as UI; 
           try 
           
               t.OnCreate(); 
           
           catch (Exception e) 
           
               Looper.LogException(e); 
           
              
           Put(name, t); 
        
        return t as T; 
    
    List mUIScene = new List(); 
    ///  
    /// 创建一个不加入管理队列的UI,主要用于世界地图上关卡界面的创建。 
    ///  
    ///  
    ///  
    ///  
    public T CreateUIWithoutManager(string name) where T : UI 
    
        T ui = _CreateUI(name); 
        try 
        
            ui.OnCreate(); 
        
        catch (Exception e) 
        
            Looper.LogException(e); 
        
        mUIScene.Add(ui); 
        return ui; 
    
       
    ///  
    /// 销毁不加入队列的UI 
    ///  
    ///  
    ///  
    public void CloseUIWithoutManager(T ui) where T : UI 
    
        try 
        
            ui.OnClose(); 
            ui.Dispose(); 
        
        catch (Exception e) 
        
            Looper.LogException(e); 
        
        mUIScene.Remove(ui); 
    
    ///  
    /// 显示UI 
    ///  
    ///  
    ///  
    public T ShowUI(string name) where T : UI 
    
        T t = CreateUI(name); 
        if(t != null
        
            t.Show(true); 
        
        return t; 
    
    ///  
    /// 显示UI 
    ///  
    ///  
    ///  
    public T HideUI(string name) where T : UI 
    
        T t = Get(name); 
        if (t != null
        
            t.Show(!true); 
   
        
        return t; 
    
   
    Stack mStack = new Stack(); 
   
    ///  
    /// 清除栈 
    ///  
    public void StackClean() 
    
        mStack.Clear(); 
    
    ///  
    /// UI隐藏压栈 
    ///  
    ///  
    ///  
    public UI Push(UI t) 
    
        if (t != null
        
            t.Show(false); 
            mStack.Push(t); 
        
        return t; 
    
    ///  
    /// UI隐藏压栈 
    ///  
    ///  
    ///  
    public T Push(string name) where T: UI 
    
        T t = Get(name); 
        if (t != null
        
            t.Show(false); 
            mStack.Push(t); 
        
        return t; 
    
    ///  
    /// UI显示出栈 
    ///  
    ///  
    ///  
    public UI Pop() 
    
        UI t = mStack.Pop(); 
        if (t != null
        
            t.Show(true); 
        
        return t; 
    
   
   
    ///  
    /// 销毁UI 
    ///  
    ///  
    public void Distroy(string name) 
    
        UI ui = Get(name); 
        if(ui != null
        
            ui.Dispose(); 
            Remove(name); 
        
    
   
    public void Distroy(string name) where T : UI 
    
        UI ui = Get(name); 
        if (ui != null
        
            ui.Dispose(); 
            Remove(name); 
        
    
    public void DestroyAll(bool includeScene= false
    
        StackClean(); 
        Clear(); 
        if(includeScene) 
        
            foreach (UI ui in mUIScene) 
            
                ui.Dispose(); 
            
            mUIScene.Clear(); 
        
   
    
    ///  
    /// 隐藏其他UI 
    ///  
    ///  
    public void HideOtherUI(string name) 
    
        foreach (UI ui in Values) 
        
            if (ui.Name.Equals(name)) 
                continue
            ui.Show(false); 
        
    
   
    UI _GetLastShowUI(bool showBG = false,UI except=null
    
        UI lastShowUI = null
        float lastShowTime = 0; 
        foreach (UI ui in Values) 
        
            if (ui == except) continue
            if (ui.IsShowing() && ui.LastShowTime > lastShowTime) 
            
                if (showBG) 
                
                    if (ui.IsShowBlackBG()) 
                    
                        lastShowUI = ui; 
                        lastShowTime = ui.LastShowTime; 
                    
                }else 
                
                    lastShowUI = ui; 
                    lastShowTime = ui.LastShowTime; 
                
            
        
        return lastShowUI; 
    
   
    ///  
    /// 隐藏最新显示的UI 
    ///  
    public void HideLastShow() 
    
        UI lastShowUI = _GetLastShowUI(); 
        if (lastShowUI != null
        
            lastShowUI.Show(false); 
        
    
    ///  
    /// 隐藏所有UI 
    ///  
    public void HideAllUI() 
    
        foreach(UI ui in Values) 
        
            if(ui != null)ui.Show(false); 
        
    
    ///  
    /// 获取UI跟面板 
    ///  
    ///  
    ///  
     GameObject _GetRootPanelExt(GameObject obj) 
    
        Transform parent = obj.transform.parent; 
        UIHolder uiHolder = null;// = root.GetComponent(); 
   
        while (parent != null
        
            if (parent.gameObject.GetComponent() != null
                break
   
            UIHolder tempPanel = parent.GetComponent(); 
            if (tempPanel != null
            
                uiHolder = tempPanel; 
            
            parent = parent.parent; 
        
        if (uiHolder == null
        
            return null
        
        return uiHolder.gameObject; 
    
    ///  
    /// NGUI事件处理器 
    ///  
    /// 事件名称 
    /// 发送事件的控件 
    /// 事件参数 
    private void _HandlerUIEvent(string eventName, GameObject sender, object arg) 
    
        try 
        
            if(sender == mBG) 
            
                //MainLooper.LogError("mBG, eventName:" + eventName + "!!"); 
                if (eventName.Equals("OnClick")) 
                
                      
                
                return
            
            GameObject root = _GetRootPanelExt(sender);//mPlugin.GetRootPanel(sender); 
            if (root == null) return
            UI ui = Get(root.name); 
            if(ui == null
            
                UIHolder uiHolder = root.GetComponent(); 
                if( uiHolder!= null) ui = uiHolder.ui; 
            
            if (ui == null
            
                Debug.LogWarning("UI:" + root.name + " not match prefab's name " + sender.name); 
                return
            
   
            if (eventName.Equals("OnClick")) 
            
                ui.OnClick(sender); 
            
            else if (eventName.Equals("OnPress")) 
            
                ui.OnPress(sender, (bool)arg); 
            
            else if (eventName.Equals("OnDrag")) 
            
                ui.OnDrag(sender, (Vector2)arg); 
            
            else if (eventName.Equals("OnDrop")) 
            
                ui.OnDrap(sender, (GameObject)arg); 
            
            else if (eventName.Equals("OnSelect")) 
            
                ui.OnSelect(sender, (bool)arg); 
            
            else if (eventName.Equals("OnHover")) 
            
                ui.OnHover(sender, (bool)arg); 
            
            else if (eventName.Equals("OnTooltip")) 
            
                ui.OnTooltip(sender, (bool)arg); 
            
            else if (eventName.Equals("OnDoubleClick")) 
            
                ui.OnDoubleClick(sender); 
            
        
        catch(Exception e) 
        
            Looper.LogException(e); 
        
   
    

该类实现了UI的创建,也就是我们说的实例化操作,以及UI的显示,隐藏,动画等效果。在代码的最后使用了点击的回调函数避免

将脚本挂接到对象上。以上类的实现基本上把UI的大部分功能都实现出来了,可以直接拿过来使用。

原文链接

著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引

游戏学院公众号二维码
腾讯游戏学院
微信公众号

提供更专业的游戏知识学习平台