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 /
  • Help Room /
avatar image
0
Question by GeneralKaiminus · Feb 27, 2018 at 10:36 PM · scripting problemsounds

Manage several sounds with one scripts

Hey everyone !! First thank you reading this !! I've open this question beceaus i really need helps

Well, i'm working on a game with a group, i care about sounds and the thing we want to do it's making an only script that offer specific methods wich the goal is macking songs. Like that, my group can use those methods on their script But about question of optimizations, we want to do this : instead of creatin methods that taking AudioSource and AudioClip in parameters , we want that ALL the AudioSource and AudioClip we need be located on the script carring sound methods ! In this way it avoids to allocate memory by copying in memory parameters of methods, by using directly them into the script source of those methods

BUT !!!! I've tryed again and again and i didn't achieve to make this real , they tried to help me but in vain... Here is really a way to handle this ? I'm now wondering if it's not possible to do this x) (i've tried creating a new class by using heritage that contained all AudioSource and AudioClip we need, i've tried make method/class statics and i noticed that there was a mess of conflict in Unity with the 'static' key word, i've tried too load clip directly from folders, ....)

So, i have to tell that i've often put the scripts that handle the AudioSource and AudioClip objects 'variaables' into a empty gameobject for allowing the "drag and drop" of AudioSource and AudioClip to the Scripts, but i start to think that we can't do this and that we can only put AudioClip/AudioSource into a script IN the gameobject wich those Audioclip/AudioSource will affect ...

SO ! Thanks for having reading this until here x) , i do not expect answers due to the specificity and nature of that question x) .... But thank you so much if you answer me

Here is the 'main' code in question after removed 'static' key word and befor all the mess of the changes i've try to make it work ( and do not care about the 'french' comments if it disturbs you i'm sorry x) )

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 // /!\  ATTENTION CE SCRIPT VA BEAUCOUP CHANGER, NOTTEMMENT LORSQU'ON VA METTRE PLUSIEURS ARME,JE VERRAI AVEC VOUS CAR IL FAUT ADAPTER LE SCRIPT EN FONCTION DES ARMES
 public class Sounds : MonoBehaviour
 {
 
     private AudioSource AK47;
     private AudioClip AK47shootSound;
     private AudioClip AK47reloadSound;
 
     #region AK47
 
     #region membres
     private float timeSound = 0.001f; // temps du son de tir joué au fur et a mesur que l'on presse le bouton
     private float cadence = 0f; // permet de cadencer les tirs de l'AK K7
     #endregion membres
 
     #region AK47shoot
     public void AK47shoot(AudioSource AK47, AudioClip AK47shootSound,int nmbreDeMunitions = 1, float volume = 0.3f) // int nmbreDeMunitions = 1 et float volume = 0.3f son des membre només, pour les appeler il faut faire Sounds.AK47shoot(AK47, AK47shoot, munitions, volume);
     {
         if (nmbreDeMunitions != 0) // Si il n'y a plus de munitions, alors le bruit de tir ne se fait plus car on ne sait plus tirer
         {
             if ((Input.GetButton("Fire1")) && (timeSound >= cadence)) // permet de s'assurer que le boutton est résté appuyé, ainsi que de cadencer les tirs
             {
                 AK47.Stop(); // permet de jouer le son de la prochaine balle qui arrivera
                 AK47.clip = AK47shootSound; // defini le son qu'emet l'AK47
                 AK47.volume = volume; // defini le volume de l'AK47
                 AK47.spatialBlend = 0.8f;
                 AK47.Play(); // joue le son de l'AK47
                 timeSound = timeSound + Time.deltaTime;  // permet de savoir le temps de son joué
                 cadence = cadence + 0.100f; // Permet de initialiser la cadence a 1 tir tout les 0,1s, soit la candence réelle d'un AK47 : https://fr.wikipedia.org/wiki/AK-47
             }
             else if (Input.GetButton("Fire1")) // Permet l'empechement d'une boucle de son infinie
             {
                 timeSound = timeSound + Time.deltaTime;
             }
             else if (!Input.GetButton("Fire1")) // permet de toujours initialiser ces variable a zero pour pouvoir retirer apres
             {
                 timeSound = 0.001f;
                 cadence = 0f;
             }
         }
     }
     #endregion AK47shoot
 
     #region AK47reload
     public void AK47reload(AudioSource AK47, AudioClip AK47reloadSound, float volume = 1f)
     {
         AK47.Stop();
         AK47.clip = AK47reloadSound;
         AK47.volume = volume;
         AK47.Play();
     }
 
     #endregion AK47reload
 
     #endregion AK47
     
     #region Movement
 
     #region FootSteeps
     public void FootSteepsSound(AudioSource personnage)
     {
         if (!personnage.isPlaying) personnage.PlayOneShot(personnage.clip);
 
     }
     #endregion FootSteeps
 
     #region jump
     public void JumpSound(AudioSource piedPersonnage)
     {
         if (!piedPersonnage.isPlaying) piedPersonnage.Play();
     }
     #endregion jump
 
 
     #endregion Movement
 }

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
1
Best Answer

Answer by Greg_lrgg · Mar 01, 2018 at 09:48 AM

Hey!
Well I find the overall idea of concatening all sounds in one script rather overkill to be honest, since you will still have to attach an AudioSource to every object susceptible of making sounds anyways.
Then, you could maybe declare an array of AudioClips which will hold all of your possible AudioClips.
Afterwards you either assign in your "Sounds" script which Audio clip to play in which Audio source and when, or make every objects having an audio source take a reference of your Audio clip array and select his own to play in desired condition (You will need a script in the said object then).
Honestly again, better to just build a sound script for each object, I think the guys who made Unity are smart enough so this don't take so many ressources to make your game lag :)

Comment
Add comment · Show 1 · 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
avatar image GeneralKaiminus · Mar 02, 2018 at 09:05 PM 0
Share

Hu you're totaly true, after some searchs i think it's the best way to provide what i want to do haha thank you !!!

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

188 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 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

On Off sound 0 Answers

How to compare audio sequences in unity 0 Answers

Is this script old o.0? 0 Answers

Vuforia (AR) Play animation on Target Image found (SCRIPT Help) 0 Answers

Why doesnt the value change? 2 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