private IEnumerator DoOverlapPlay() { var fadeTime = GetFadeTime(); //wait for A to be almost done while (_a.time < Clip.length - fadeTime) { _a.volume = Volume; yield return(null); } //crossfade _b.volume = 0f; _b.Play(); for (var i = 0f; i < 1f; i += Time.deltaTime / fadeTime) { _a.volume = Volume * (DoEqualPowerCrossfade ? LunityMath.GetCrossfade(i * 2f - 1f, true) : 1f - i); _b.volume = Volume * (DoEqualPowerCrossfade ? LunityMath.GetCrossfade(i * 2f - 1f, false) : i); yield return(null); } //stop A _a.volume = 0f; _a.Stop(); //wait for B to be almost done while (_b.time < Clip.length - fadeTime) { _b.volume = Volume; yield return(null); } //crossfade _a.volume = 0f; _a.Play(); for (var i = 0f; i < 1f; i += Time.deltaTime / fadeTime) { _b.volume = Volume * (DoEqualPowerCrossfade ? LunityMath.GetCrossfade(i * 2f - 1f, true) : 1f - i); _a.volume = Volume * (DoEqualPowerCrossfade ? LunityMath.GetCrossfade(i * 2f - 1f, false) : i); yield return(null); } //stop A _b.volume = 0f; _b.Stop(); //start the whole thing again StartCoroutine(DoOverlapPlay()); }
/// Given a provided world-space direction, returns the point on the boundary of the rect that intersects with that line /// If the rect size is zero, returns the rect's center public static Vector2 GetPerimeterPoint(this Rect rect, Vector2 direction) { var mag = rect.size.magnitude * 2f; if (mag <= 0) { return(rect.center); } var line = rect.center + direction.normalized * mag; var corners = new[] { new Vector2(rect.xMin, rect.yMin), new Vector2(rect.xMin, rect.yMax), new Vector2(rect.xMax, rect.yMax), new Vector2(rect.xMax, rect.yMin), }; if (LunityMath.Get2DIntersectionBounded(rect.center, line, corners[0], corners[1], out var left)) { return(left); } if (LunityMath.Get2DIntersectionBounded(rect.center, line, corners[1], corners[2], out var up)) { return(up); } if (LunityMath.Get2DIntersectionBounded(rect.center, line, corners[2], corners[3], out var right)) { return(right); } if (LunityMath.Get2DIntersectionBounded(rect.center, line, corners[3], corners[0], out var down)) { return(down); } Debug.LogWarning("Somehow we failed to get a perimeter point for the provided rect?"); return(rect.center); }