chenxin
2020-12-03 513df7ede07913b22f3091fec5221e2bc2eadd74
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Core.Utilities;
using System;
 
/**
 * 选择buff脚本
 * @Author: chenxin
 * @Date: 2020-10-19 15:55:46
 */
namespace KTGMGemClient
{
    public class EndlessBuffSelect : Singleton<EndlessBuffSelect>
    {
        public List<SelectBuffIcon> IconList;
 
        public event Action<int> BuffSelectCompleted;
 
        /// <summary>
        /// 当前选择的索引,-1表示还未选中其中一个
        /// </summary>
        private int SelectedIndex = -1;
 
        public Button SelectBtn;
 
        public GameObject EffectPrefabObject;
 
        private GameObject effectObj;
 
        private Vector3[] effectPosArr = { new Vector3(-21.6f, 0, 6.6f), new Vector3(0, 0, 6.6f), new Vector3(21.6f, 0, 6.6f) };
 
        // Start is called before the first frame update
        private void Start()
        {
            for (int i = 0; i < IconList.Count; ++i)
            {
                IconList[i].OnSelectBuffCompleted += OnClick;
            }
 
            SelectBtn.onClick.AddListener(OnClickSelectBtn);
        }
 
        private void OnEnable()
        {
            SelectedIndex = -1;
 
            for (int i = 0; i < IconList.Count; ++i)
            {
                IconList[i].Reset();
            }
 
            AudioSourceManager.Ins?.Play(AudioEnum.BuffAppear);
        }
 
        private void OnDisable()
        {
            if (effectObj != null)
                Destroy(effectObj);
        }
 
        public void OnClick(int index)
        {
            // 如果点击同一个直接返回
            if (index == SelectedIndex) return;
 
            // 如果已经选择过了,设置一个之前选择的item
            if (SelectedIndex != -1)
                IconList[SelectedIndex].SetState(false);
 
            SelectedIndex = index;
            IconList[index].SetState(true);
            //PlayEffect();
        }
 
 
        private void PlayEffect()
        {
            if (effectObj == null)
            {
                effectObj = Instantiate(EffectPrefabObject);
                GameObject particleSystemObject = GameObject.Find("ParticleSystemObject");
                effectObj.transform.SetParent(particleSystemObject.transform, false);
                effectObj.transform.localScale = new Vector3(1.5f, 1.5f, 1.5f);
            }
            effectObj.transform.localPosition = effectPosArr[SelectedIndex];
            ParticleSystem ps = effectObj.transform.GetChild(0).GetComponent<ParticleSystem>();
            ps.Play();
        }
 
        private void StopEffect()
        {
            if (effectObj != null)
            {
                ParticleSystem ps = effectObj.transform.GetChild(0).GetComponent<ParticleSystem>();
                ps.Stop();
                ps.Clear();
            }
        }
 
        /// <summary>
        /// 点击了选择按钮
        /// </summary>
        public void OnClickSelectBtn()
        {
            if (SelectedIndex == -1) return;
 
            int index = SelectedIndex;
 
            AudioSourceManager.Ins.Play(AudioEnum.UI);
 
            HideBuffUI();
 
            if (BuffSelectCompleted != null)
                BuffSelectCompleted(index);
        }
 
        public void ShowBuffUI()
        {
            EndlessMaskUI.instance.Show();
            gameObject.SetActive(true);
        }
 
        public void HideBuffUI()
        {
            EndlessMaskUI.instance.Hide();
            StopEffect();
            gameObject.SetActive(false);
        }
 
        public void Refresh(List<EndlessBuffConfig> list)
        {
            for (int i = 0; i < IconList.Count; ++i)
            {
                IconList[i].SetIcon(list[i].Config.image);
                IconList[i].SetQuality(list[i].Config.rare);
                IconList[i].SetName(list[i].Config.name);
                IconList[i].SetEffect(list[i].Config.brief);
                IconList[i].SetRare(list[i].Config.rare);
            }
        }
    }
}