回答編集履歴

1

値の連動機構について案を追記

2019/01/26 09:56

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -53,3 +53,147 @@
53
53
 
54
54
 
55
55
  ![スライダー](e383d8c878302959b2cba40ed0fcd64b.gif)
56
+
57
+
58
+
59
+ #追記
60
+
61
+ [UniRx](https://assetstore.unity.com/packages/tools/integration/unirx-reactive-extensions-for-unity-17276)を使っても構わなければ、連動機構のごちゃごちゃ感を軽減できるかもしれませんね。
62
+
63
+
64
+
65
+ ```C#
66
+
67
+ using UniRx;
68
+
69
+ using UnityEngine;
70
+
71
+ using UnityEngine.UI;
72
+
73
+
74
+
75
+ public class SliderTextController : MonoBehaviour
76
+
77
+ {
78
+
79
+ // 外部スクリプトから値を操作する場合、こちらのプロパティ経由で行う
80
+
81
+ public ReactiveProperty<float> Value1 => this.value1;
82
+
83
+ public ReactiveProperty<float> Value2 => this.value2;
84
+
85
+
86
+
87
+ [SerializeField] private FloatReactiveProperty value1 = new FloatReactiveProperty();
88
+
89
+ [SerializeField] private FloatReactiveProperty value2 = new FloatReactiveProperty();
90
+
91
+ [SerializeField] private Vector2 range1 = new Vector2(0.1f, 1.0f);
92
+
93
+ [SerializeField] private Vector2 range2 = new Vector2(300.0f, 22000.0f);
94
+
95
+ [SerializeField] private Slider value1Slider;
96
+
97
+ [SerializeField] private Slider value2Slider;
98
+
99
+ [SerializeField] private Text value1Text;
100
+
101
+ [SerializeField] private Text value2Text;
102
+
103
+
104
+
105
+ private void Awake()
106
+
107
+ {
108
+
109
+ // スライダー1の範囲を設定
110
+
111
+ this.value1Slider.minValue = this.range1.x;
112
+
113
+ this.value1Slider.maxValue = this.range1.y;
114
+
115
+ this.value1Slider.value = this.range1.x;
116
+
117
+
118
+
119
+ // スライダー1の値が操作されたらvalue1をその値に書き換える
120
+
121
+ this.value1Slider.OnValueChangedAsObservable().Subscribe(value => this.value1.Value = value);
122
+
123
+
124
+
125
+ // value1に変更があったらテキスト1を書き換えて...
126
+
127
+ this.value1.SubscribeToText(this.value1Text, value => value.ToString("F6"));
128
+
129
+ // スライダー1を更新する必要があれば更新し...
130
+
131
+ this.value1.Where(value => value != this.value1Slider.value)
132
+
133
+ .Subscribe(value => this.value1Slider.value = value);
134
+
135
+ // value2を更新する必要があれば更新する
136
+
137
+ this.value1.Select(value => MapValue(value, this.range1.x, this.range1.y, this.range2.x, this.range2.y))
138
+
139
+ .Where(value => value != this.value2.Value).Subscribe(value => this.value2.Value = value);
140
+
141
+
142
+
143
+ // スライダー2の範囲を設定
144
+
145
+ this.value2Slider.minValue = this.range2.x;
146
+
147
+ this.value2Slider.maxValue = this.range2.y;
148
+
149
+ this.value2Slider.value = this.range2.x;
150
+
151
+
152
+
153
+ // スライダー2の値が操作されたらvalue2をその値に書き換える
154
+
155
+ this.value2Slider.OnValueChangedAsObservable().Subscribe(value => this.value2.Value = value);
156
+
157
+
158
+
159
+ // value2に変更があったらテキスト2を書き換えて...
160
+
161
+ this.value2.SubscribeToText(this.value2Text, value => value.ToString("F0"));
162
+
163
+ // スライダー2を更新する必要があれば更新し...
164
+
165
+ this.value2.Where(value => value != this.value2Slider.value)
166
+
167
+ .Subscribe(value => this.value2Slider.value = value);
168
+
169
+ // value1を更新する必要があれば更新する
170
+
171
+ this.value2.Select(value => MapValue(value, this.range2.x, this.range2.y, this.range1.x, this.range1.y))
172
+
173
+ .Where(value => value != this.value1.Value).Subscribe(value => this.value1.Value = value);
174
+
175
+ }
176
+
177
+
178
+
179
+ private static float MapValue(float value, float fromMin, float fromMax, float toMin, float toMax)
180
+
181
+ {
182
+
183
+ return Mathf.Lerp(toMin, toMax, Mathf.InverseLerp(fromMin, fromMax, value));
184
+
185
+ }
186
+
187
+ }
188
+
189
+ ```
190
+
191
+
192
+
193
+ ![スライダー](6fe9c9546a20fabf7450b1dc104f022f.gif)
194
+
195
+
196
+
197
+ len_soukoさんがおっしゃるように、私も何かとpublicなフィールドを使いたがる悪癖があって耳が痛いところですが、外部に何を見せて何を隠すか十分検討しておくとプログラムの規模が大きくなっても混乱しにくいかと思います。
198
+
199
+ プロパティは優れた機能だと思いますが、確かにWeb上で見かけるコードでは本家.NETほど広まってはいない印象です。インスペクタとの連携を考えると、Unity側のサポートがまだ弱いような気がしますね...今後のバージョンアップで改善されるでしょうか?