Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by brd1378 · Jul 04, 2019 at 01:53 PM · audiobeginneraudioclipmusicaudio source

Dynamic Backgroundmusic

My worldshifting based Game should have 2 background themes. the one of them is a slightly different version of the other one with the same length. What I wanna do is, if I press a specific key, the playing theme should switch to the other one but continue right at that time code where the previous one stopped (for ex.: when 1st. track stops at 0:35, the 2nd track should continue right at that time), whereas in my case its still tarting from the beginning. Any help will be highly appreciated!

My current code:

public class AudioManager : MonoBehaviour {

 public AudioSource GTD;
 public AudioClip CuteGiana;
 public AudioClip DarkGiana;

 void Start()
 {
     GTD = GetComponent<AudioSource>();
     GTD.clip = CuteGiana;
     GTD.Play();
 }

 void Update()
 {
     if (Input.GetKeyDown(KeyCode.P))
     {
         if (GTD.clip == CuteGiana)
         {
             GTD.Stop();
             GTD.clip = DarkGiana;
             GTD.Play();
         }

         else
         {
             GTD.Stop();
             GTD.clip = CuteGiana;
             GTD.Play();
         }

     }
 }

} ,My Game should have 2 background themes, a "light" one and a "dark" one, which has the same length and uses the same melody, but with different samples. Anyway I made the playing theme switch after pressing a key, but I want the other theme to continue right at that time code, where the previous theme stopped (for example: previous theme stopped at 0:35, next theme should continue from there), whereas it's just starting from the beginning at the moment. Any help will be highly appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pinkuboxu · Jul 04, 2019 at 02:15 PM

There are a few ways to do this. Take a look at:

https://docs.unity3d.com/ScriptReference/AudioSource-time.html AND

https://docs.unity3d.com/ScriptReference/AudioSource-timeSamples.html

I think they can be used to set the time, but in case they don't, another method is to use the Audio Mixer and manipulate the Mixer channels volume in code smoothly using what are called Snapshots in the Mixer. This is a bit advanced and requires you to learn how the Mixer works in Unity. In fact you may just want to use this method as it allows you to transition more smoothly rather than just jump right in to another track.


https://docs.unity3d.com/Manual//AudioMixer.html

https://learn.unity.com/tutorial/audio-mixing

https://docs.unity3d.com/ScriptReference/Audio.AudioMixerSnapshot.html

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

C sharp cant play Audio 2 Answers

Audio Source wont play in if statement 3 Answers

Unity Unable to Reassign Audio Clip 1 Answer

What are the parameters to the AudioClip.create() function 0 Answers

Music will not change when an event triggers it 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges