wangguan
2020-10-23 f08d4cf99c556eee7ba06f99015bf87a601dad50
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
 
/// <summary>
/// 新手引导UI脚本
/// </summary>
public class GuidePanel : MonoBehaviour
{
    GameObject tipsUI;//显示文字的UI
    CharForeach charForeach;//动态显示文字
 
    GuideCtrl m_Ctrl;//控制类
 
    /// <summary>
    /// 遮罩对象
    /// </summary>
    private RectGuidance maskObj;
 
    private RectTransform image_Rim_Rect;//边框
    private RectTransform image_Tip_Rect;//可以移动的提示框
    private Text text_Tip;//可以移动的提示框文字
 
    private Image image_SkillRim;//技能释放区域
    Vector2 rimOffset = new Vector2(30, 30);//边框要比按钮大一些
 
 
    // Start is called before the first frame update
    void Awake()
    {
        tipsUI = transform.Find("Tips").gameObject;
        charForeach = tipsUI.GetComponent<CharForeach>();
        m_Ctrl = transform.GetComponent<GuideCtrl>();
        transform.Find("Button").GetComponent<Button>().onClick.AddListener(m_Ctrl.OnFinishOneStep);
        GameObject go = Instantiate(Resources.Load<GameObject>("GuideFile/RectGuidance_Panel"), this.transform);
        if (go != null)
        {
            go.transform.SetSiblingIndex(1);
            maskObj = go.GetComponent<RectGuidance>();
            maskObj.gameObject.SetActive(false);
        }
        else
        {
            Debug.LogError("遮罩生成失败了");
        }
 
        image_Rim_Rect = transform.Find("Image_Rim").GetComponent<RectTransform>();
        image_Tip_Rect = transform.Find("Image_Tip").GetComponent<RectTransform>();
        text_Tip = image_Tip_Rect.transform.Find("Text").GetComponent<Text>();
        image_SkillRim = transform.Find("Image_SkillRim").GetComponent<Image>();
 
        image_Rim_Rect.gameObject.SetActive(false);
        image_Tip_Rect.gameObject.SetActive(false);
        image_SkillRim.gameObject.SetActive(false);
 
    }
 
    /// <summary>
    /// 设置引导框
    /// </summary>
    /// <param name="active"></param>
    public void SetGuideUI(bool active)
    {
        tipsUI.SetActive(active);
    }
 
    //开始打印文字
    public void StartShowWord(string str, Action cb)
    {
        charForeach.StartShowWord(str, cb);
    }
 
    /// <summary>
    /// 立刻完成打印
    /// </summary>
    public void ShowWordImmediately()
    {
        charForeach.ShowWordImmediately();
    }
 
    /// <summary>
    /// 第二步,购买宝石
    /// </summary>
    /// <param name="str"></param>
    /// <param name="currentIndex">当前第几步</param>
    public void Step1(string str, int currentIndex, Action ac)
    {
        if (currentIndex == 0)
        {
            image_Rim_Rect.gameObject.SetActive(true);
            image_Tip_Rect.gameObject.SetActive(true);
            //设置位置
            GameObject go = GameObject.Find("MainUI/TowerBuyBtn");
            AddButtonListener(go, ac);
 
            Image btnImg = go.GetComponent<Image>();
            InitRectGuidance(btnImg);
            SetRimPos(btnImg);
            maskObj.ShowImmediately();
 
        }
        else if (currentIndex == 1)
        {
 
        }
        text_Tip.text = str;//动态改变长度
        StartCoroutine(ShowRimTip(str));
 
    }
 
 
    public void Step2(int currentIndex)
    {
        image_Rim_Rect.gameObject.SetActive(false);
        image_Tip_Rect.gameObject.SetActive(false);
 
        CloseMask();
    }
 
    #region 按钮以及提示
 
    /// <summary>
    /// 设置边框的大小和位置
    /// </summary>
    /// <param name="target"></param>
    private void SetRimPos(Image target)
    {
        RectTransform rt = target.GetComponent<RectTransform>();
        image_Rim_Rect.anchoredPosition = rt.anchoredPosition;
        image_Rim_Rect.sizeDelta = (rt.sizeDelta * rt.localScale) + rimOffset;
        image_Tip_Rect.anchoredPosition = new Vector2(image_Rim_Rect.anchoredPosition.x, image_Rim_Rect.anchoredPosition.y + 74 / 2 + image_Rim_Rect.sizeDelta.y / 2);
 
    }
 
    IEnumerator ShowRimTip(string str)
    {
        yield return 10;
        SetTipRect(str);//动态修改text长度需要等待计算完成
    }
    /// <summary>
    /// 设置显示文字的长度
    /// </summary>
    /// <param name="str"></param>
    private void SetTipRect(string str)
    {
        RectTransform textRect = text_Tip.GetComponent<RectTransform>();
        image_Tip_Rect.sizeDelta = new Vector2(textRect.sizeDelta.x + 64, image_Tip_Rect.sizeDelta.y);
    }
 
    #endregion
 
    #region 遮罩,以及按钮添加删除事件
 
    /// <summary>
    /// 抠出来一个区域
    /// </summary>
    /// <param name="target"></param>
    void InitRectGuidance(Image target)
    {
        if (!maskObj.gameObject.activeSelf)
        {
            maskObj.gameObject.SetActive(true);
        }
        maskObj.Init(target);
    }
 
    /// <summary>
    /// 关闭遮罩
    /// </summary>
    public void CloseMask()
    {
        maskObj.gameObject.SetActive(false);
    }
 
    /// <summary>
    /// 给按钮添加事件
    /// </summary>
    private void AddButtonListener(GameObject go, Action ac)
    {
        EventTriggerListener.GetListener(go).onClick += (go1) =>
    {
        ac();
    };
    }
 
    /// <summary>
    /// 移除事件
    /// </summary>
    /// <param name="go"></param>
    private void RemoveButtonListener(GameObject go)
    {
        EventTriggerListener.GetListener(go).onClick -= null;
 
    }
 
    #endregion
 
    #region 
 
    #endregion
 
    #region 
 
    #endregion
 
    #region 
 
    #endregion
 
 
}