wangguan
2020-12-29 452c75675679c44cc39b04bdb7d330d7c5c14d5c
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
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.UI;
 
namespace MoreMountains.NiceVibrations
{    
    /// <summary>
    /// A class to add to an Image or SpriteRenderer to have it act like a button with a different sprite for on and off states
    /// </summary>
    public class MMSpriteReplace : MonoBehaviour 
    {
 
        [Header("Sprites")]
       
        /// the sprite to use when in the "on" state
        public Sprite OnSprite;
        /// the sprite to use when in the "off" state
        public Sprite OffSprite;
 
        [Header("Start settings")]
        /// if this is true, the button will start if "on" state
        public bool StartsOn = true;
        
        /// the current state of the button
        public bool CurrentValue { get { return (_image.sprite == OnSprite); } }
        
        protected Image _image;
        protected SpriteRenderer _spriteRenderer;
        protected MMTouchButton _mmTouchButton;
 
        /// <summary>
        /// On Start we initialize our button
        /// </summary>
        protected virtual void Start()
        {
            Initialization ();
        }
 
        /// <summary>
        /// On init, we grab our image component, and set our sprite in its initial state
        /// </summary>
        protected virtual void Initialization()
        {
            // grabs components
            _image = GetComponent<Image> ();
            _spriteRenderer = GetComponent<SpriteRenderer>();
 
            // grabs button
            _mmTouchButton = GetComponent<MMTouchButton> ();
            if (_mmTouchButton != null)
            {
                _mmTouchButton.ReturnToInitialSpriteAutomatically = false;
            }
 
            // handles start
            if ((OnSprite == null) || (OffSprite == null))
            {
                return;
            }
 
            if (_image != null)
            {
                if (StartsOn)
                {
                    _image.sprite = OnSprite;
                }
                else
                {
                    _image.sprite = OffSprite;
                }
            }
 
            if (_spriteRenderer != null)
            {
                if (StartsOn)
                {
                    _spriteRenderer.sprite = OnSprite;
                }
                else
                {
                    _spriteRenderer.sprite = OffSprite;
                }
            }            
        }
 
        /// <summary>
        /// A public method to change the sprite 
        /// </summary>
        public virtual void Swap()
        {
            if (_image != null)
            {
                if (_image.sprite != OnSprite)
                {
                    SwitchToOnSprite();
                }
                else
                {
                    SwitchToOffSprite();
                }
            }
 
            if (_spriteRenderer != null)
            {
                if (_spriteRenderer.sprite != OnSprite)
                {
                    SwitchToOnSprite();
                }
                else
                {
                    SwitchToOffSprite();
                }
            }            
        }
 
        /// <summary>
        /// a public method to switch to off sprite directly
        /// </summary>
        public virtual void SwitchToOffSprite()
        {
            if ((_image == null) && (_spriteRenderer == null))
            {
                return;
            }
            if (OffSprite == null)
            {
                return;
            }
 
            SpriteOff ();
        }
 
        /// <summary>
        /// sets the image's sprite to off
        /// </summary>
        protected virtual void SpriteOff()
        {
            if (_image != null)
            {
                _image.sprite = OffSprite;
            }
            if (_spriteRenderer != null)
            {
                _spriteRenderer.sprite = OffSprite;
            }            
        }
 
        /// <summary>
        /// a public method to switch to on sprite directly
        /// </summary>
        public virtual void SwitchToOnSprite()
        {
            if ((_image == null) && (_spriteRenderer == null))
            {
                return;
            }
            if (OnSprite == null)
            {
                return;
            }
 
            SpriteOn ();
        }    
 
        /// <summary>
        /// sets the image's sprite to on
        /// </summary>
        protected virtual void SpriteOn()
        {
            
            if (_image != null)
            {
                _image.sprite = OnSprite;
            }
            if (_spriteRenderer != null)
            {
                _spriteRenderer.sprite = OnSprite;
            }
        }
    }
}