少年修仙传客户端代码仓库
client_linchunjie
2018-09-06 619a37b0c8c78845a034d1e36a5d77654f88a23e
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
using TableConfig;
 
public static class ComponentExtersion
{
 
    public static T AddMissingComponent<T>(this Component _compoent) where T : Component
    {
        if (_compoent == null)
        {
            return null;
        }
 
        T component = _compoent.GetComponent<T>();
        if (component == null)
        {
            component = _compoent.gameObject.AddComponent<T>();
        }
 
        return component;
    }
 
    public static T AddMissingComponent<T>(this Transform _transform) where T : Component
    {
        if (_transform == null)
        {
            return null;
        }
 
        T component = _transform.GetComponent<T>();
        if (component == null)
        {
            component = _transform.gameObject.AddComponent<T>();
        }
 
        return component;
    }
 
    public static T AddMissingComponent<T>(this GameObject _gameObject) where T : Component
    {
        if (_gameObject == null)
        {
            return null;
        }
 
        T component = _gameObject.GetComponent<T>();
        if (component == null)
        {
            component = _gameObject.AddComponent<T>();
        }
 
        return component;
    }
 
    public static void AddListener(this Button _button, UnityAction _action)
    {
        if (_button == null)
        {
            return;
        }
        _button.onClick.AddListener(_action);
    }
 
    public static void RemoveAllListeners(this Button _button)
    {
        if (_button == null)
        {
            return;
        }
        _button.onClick.RemoveAllListeners();
    }
 
    public static void AddListener(this Toggle _toggle, UnityAction<bool> _action)
    {
        if (_toggle == null)
        {
            return;
        }
        _toggle.onValueChanged.AddListener(_action);
    }
 
    public static void RemoveAllListeners(this Toggle _toggle)
    {
        if (_toggle == null)
        {
            return;
        }
        _toggle.onValueChanged.RemoveAllListeners();
    }
 
    public static void AddListener(this Slider _slider, UnityAction<float> _action)
    {
        if (_slider == null)
        {
            return;
        }
        _slider.onValueChanged.AddListener(_action);
    }
 
    public static void RemoveAllListeners(this Slider _slider)
    {
        if (_slider == null)
        {
            return;
        }
        _slider.onValueChanged.RemoveAllListeners();
    }
 
    public static void AddListener(this InputField _inputField, UnityAction<string> _action)
    {
        if (_inputField == null)
        {
            return;
        }
        _inputField.onValueChanged.AddListener(_action);
    }
 
    public static void RemoveAllListeners(this InputField _inputField)
    {
        if (_inputField == null)
        {
            return;
        }
        _inputField.onValueChanged.RemoveAllListeners();
    }
 
    public static void SetEnable(this Button _btn, Text _btnTxt, bool _enable, EnableButtonConfig.EnableButtonType _type =
        EnableButtonConfig.EnableButtonType.Default)
    {
        EnableButtonConfig.SetEnable(_btn, _btnTxt, _enable, _type);
    }
 
    public static void SetInteractable(this Button _btn, Text _btnText, bool _interactable)
    {
        if (_btn != null)
        {
            _btn.interactable = _interactable;
            var imageEx = _btn.image as ImageEx;
            if (imageEx != null)
            {
                imageEx.gray = !_interactable;
            }
        }
        if (_btnText != null)
        {
            _btnText.color = UIHelper.GetUIColor(_interactable ? TextColType.NavyBrown : TextColType.White);
            _btnText.color = _btnText.color.SetA(_interactable ? 1 : 0.5f);
        }
    }
 
    public static void SetSprite(this Image _image, string _id)
    {
        if (_image == null)
        {
            return;
        }
 
        var sprite = UILoader.LoadSprite(_id);
        _image.overrideSprite = sprite;
    }
 
    public static void SetSprite(this TextImage _textImage, string _id)
    {
        if (_textImage == null)
        {
            return;
        }
 
        var sprite = UILoader.LoadSprite(_id);
        _textImage.sprite = sprite;
    }
 
}