private void DrawButton(Rect position, SerializedProperty prop) { if (prop.objectReferenceValue != null) { position.x -= 4; AudioClip clip = prop.objectReferenceValue as AudioClip; Rect buttonRect = new Rect(position); buttonRect.width = 20; Rect waveformRect = new Rect(position); waveformRect.x += 22; waveformRect.width -= 22; Texture2D waveformTexture = AssetPreview.GetAssetPreview(prop.objectReferenceValue); if (waveformTexture != null) { GUI.DrawTexture(waveformRect, waveformTexture); } bool isPlaying = AudioUtility.IsClipPlaying(clip) && (CurrentClip == prop.propertyPath); string buttonText = ""; Action <SerializedProperty, AudioClip> buttonAction; if (isPlaying) { EditorUtility.SetDirty(prop.serializedObject.targetObject); buttonAction = GetStateInfo(ButtonState.Stop, out buttonText); Rect progressRect = new Rect(waveformRect); float percentage = (float)AudioUtility.GetClipSamplePosition(clip) / AudioUtility.GetSampleCount(clip); float width = progressRect.width * percentage; progressRect.width = Mathf.Clamp(width, 6, width); GUI.Box(progressRect, "", "SelectionRect"); } else { buttonAction = GetStateInfo(ButtonState.Play, out buttonText); } if (GUI.Button(buttonRect, buttonText)) { AudioUtility.StopAllClips(); buttonAction(prop, clip); } } }
void Update() { float deltaTime = Time.realtimeSinceStartup - prevTime; prevTime = Time.realtimeSinceStartup; if (seekPosition != oldSeekPosition) { oldSeekPosition = seekPosition; if (!isPlaying) { if (!previewing && lsdTarget.clip != null) { AudioUtility.PlayClip(lsdTarget.clip); } previewing = true; stopTimer = 0.1f; prevTime = Time.realtimeSinceStartup; resetTime = seekPosition; } if (lsdTarget.clip) { AudioUtility.SetClipSamplePosition(lsdTarget.clip, (int)(seekPosition * AudioUtility.GetSampleCount(lsdTarget.clip))); } } if (previewing) { stopTimer -= deltaTime; if (lsdTarget.clip) { seekPosition = AudioUtility.GetClipPosition(lsdTarget.clip) / lsdTarget.length; oldSeekPosition = seekPosition; } if (stopTimer <= 0) { previewing = false; seekPosition = resetTime; oldSeekPosition = seekPosition; if (lsdTarget.clip != null) { AudioUtility.PauseClip(lsdTarget.clip); AudioUtility.SetClipSamplePosition(lsdTarget.clip, (int)(seekPosition * AudioUtility.GetSampleCount(lsdTarget.clip))); } } } if (isPlaying && lsdTarget.clip == null) { seekPosition += deltaTime / lsdTarget.length; oldSeekPosition = seekPosition; if (seekPosition >= 1) { isPlaying = false; seekPosition = 0; } } else if (isPlaying) { seekPosition = AudioUtility.GetClipPosition(lsdTarget.clip) / lsdTarget.length; oldSeekPosition = seekPosition; isPlaying = AudioUtility.IsClipPlaying(lsdTarget.clip); } if (isPlaying && visualPreview || previewing && visualPreview) { UpdatePreview(seekPosition); } }