/
Button.cs
153 lines (131 loc) · 3.81 KB
/
Button.cs
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
using UnityEngine;
using System;
using System.Collections;
using Fungus.Script;
namespace Fungus
{
/**
* Button click handler class for making sprites clickable.
* When the user taps on the sprite, an Action delegate method is called
*/
[RequireComponent (typeof (SpriteRenderer))]
public class Button : MonoBehaviour
{
/**
* Delegate method to call when the player clicks the button.
*/
public Action buttonAction;
/**
* Automatically hides the button when displaying story text/options or waiting.
*/
[Tooltip("Automatically hides the button when displaying story text/options or waiting.")]
public bool autoHide = true;
/**
* Automatically hides the button when the named boolean variable is set using SetBoolean()
*/
[Tooltip("Automatically hides the button when the named boolean variable is set using the SetBoolean() command.")]
public string hideOnBoolean;
/**
* Sound effect to play when button is clicked.
*/
[Tooltip("Sound effect to play when button is clicked.")]
public AudioClip clickSound;
float targetAlpha;
bool showButton;
/**
* Set visibility of a button object and set the delegate method to call when clicked.
* If the object does not already have a Collider2D component, then a BoxCollider2D is added.
* @param _visible Setting this to true makes the button visible, unless overridden by property settings
* @param _buttonAction An Action delegate method to call when the player clicks on the button
*/
public void Show(bool _visible, Action _buttonAction)
{
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
if (spriteRenderer == null)
{
Debug.LogError("Sprite renderer must not be null");
return;
}
// Add a BoxCollider2d if none currently exists
if (collider2D == null)
{
gameObject.AddComponent<BoxCollider2D>();
}
showButton = _visible;
buttonAction = _buttonAction;
if (_visible)
{
targetAlpha = 1f;
}
else
{
targetAlpha = 0f;
}
UpdateTargetAlpha();
}
public void SetAlpha(float alpha)
{
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
Color color = spriteRenderer.color;
color.a = alpha;
spriteRenderer.color = color;
}
void UpdateTargetAlpha()
{
// Automatically display button when game is in idle state (not displaying story text/options or waiting)
if (autoHide)
{
if (showButton &&
Game.GetInstance().GetShowAutoButtons())
{
targetAlpha = 1f;
}
else
{
targetAlpha = 0f;
}
}
// Hide the button if the specified boolean variable is true
if (hideOnBoolean.Length > 0 &&
GlobalVariables.GetBoolean(hideOnBoolean))
{
targetAlpha = 0f;
}
}
void Update()
{
UpdateTargetAlpha();
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
float fadeSpeed = (1f / Game.GetInstance().buttonFadeDuration);
float alpha = Mathf.MoveTowards(spriteRenderer.color.a, targetAlpha, Time.deltaTime * fadeSpeed);;
// Set alpha for this sprite and any child sprites
SpriteRenderer[] children = spriteRenderer.gameObject.GetComponentsInChildren<SpriteRenderer>();
foreach (SpriteRenderer child in children)
{
Color color = child.color;
color.a = alpha;
child.color = color;
}
}
void OnMouseUpAsButton()
{
SpriteRenderer spriteRenderer = renderer as SpriteRenderer;
// Ignore button press if sprite is not fully visible or
// if the game is not in an idle state
if (spriteRenderer.color.a != 1f ||
!Game.GetInstance().GetShowAutoButtons())
{
return;
}
// Click sound effect
if (clickSound != null)
{
AudioSource.PlayClipAtPoint(clickSound, Vector3.zero);
}
/*
CommandQueue commandQueue = Game.GetInstance().commandQueue;
commandQueue.CallCommandMethod(buttonAction);
*/
}
}
}