Tanoda
ScrollRectVolume.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using UnityEngine;
10using UnityEngine.UI;
11using System.Collections;
12
13namespace Leap.Unity.InputModule {
14 public class ScrollRectVolume : MonoBehaviour {
15 public AudioSource source;
16 public RectTransform content;
17 public float Volume = 1f;
18 RectTransform viewport;
19 float volumeScalar = 0f;
20 Vector2 currentPos = Vector3.zero;
21 Vector2 prevPos = Vector3.zero;
22 Vector2 viewportScale;
23 float TimeLastSlid = 0f;
24
25 void Start() {
26 viewport = content.parent.GetComponent<RectTransform>();
27 viewportScale = new Vector2(viewport.rect.size.x, viewport.rect.size.y);
28 }
29
30 void Update() {
31 Vector2 localPos = new Vector2(content.localPosition.x, content.localPosition.y);
32 localPos = new Vector2(localPos.x / viewportScale.x, localPos.y / viewportScale.y);
33
34 if (localPos != currentPos) {
35 prevPos = currentPos;
36 currentPos = localPos;
37
38 volumeScalar = Mathf.Lerp(volumeScalar, Mathf.Abs((currentPos - prevPos).magnitude) * 40f, 0.4f);
39
40 source.volume = Mathf.Clamp(volumeScalar * Volume, 0f, Volume);
41
42 if (!source.isPlaying) {
43 source.Play();
44 }
45
46 TimeLastSlid = Time.time;
47 } else {
48 if (Time.time - TimeLastSlid > Time.deltaTime * 5f) {
49 source.Stop();
50 } else {
51 volumeScalar = Mathf.Lerp(volumeScalar, 0f, 0.4f);
52 source.volume = volumeScalar * Volume;
53 }
54 }
55 }
56 }
57}