- Home /
Give Audio Clip From Array A Unique I.D.
Is there a way to give an audio clip from a specific array of audio clips a unique I.D. that I could reference from other clips or save to player prefs?
Answer by Glurth · Jan 12, 2017 at 10:07 PM
I like static classes for stuff like that (uncompiled/tested example). Note: the counter will reset to Zero every time you launch the program, so this won't do for SAVING the ID between sessions. But if you give them ID's onload or at runtime only, this should do.
static public class AutoClipCounter{
static int counter=0;
public static int GetNextID() {return counter++;}
}
Access with something like;
clipID=AudioClipCounter.GetNextID();
Thanks @Glurth, That might work, so far to track the last audio clip played I have been using something like this:
public void ThatReferenceEscapes$$anonymous$$e(){
AudioClip myClip = ThatReferenceEscapes$$anonymous$$eClips [Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length)];
source.PlayOneShot (myClip);
lastClipPlayed = myClip;
StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
}
And then using the: lastClipPlayed to access the name of the clip using .name
I had thought of saving the .name of the last clip played as a string into a temporary player prefs....?? $$anonymous$$aybe your idea might be better....?? Not sure??
Well, if your trying to get the "next clip" randomly, then I don't think my solution will help, since it will always give you the clips in order. I think in this case, you just need to make the lastClipPlayed as STATIC variable. (only one instance of a static variable, regardless of how many class instances exist.) You would also access it by class Name. The uniqueID number, will be equal to the index into the array.
public Class ClipPlayer{
public static int LastAudioIndex;
public void ThatReferenceEscapes$$anonymous$$e(){
int newIndex=Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length);
while(newIndex==LastAudioIndex) //example: don't repeat last clip code
newIndex=Random.Range (0, ThatReferenceEscapes$$anonymous$$eClips.Length);
AudioClip myClip = ThatReferenceEscapes$$anonymous$$eClips [newIndex];
LastAudioIndex=newIndex
source.PlayOneShot (myClip);
lastClipPlayed = myClip;
StartCoroutine (voiceBlocker (myClip.length + AdditionalDelay));
}
}
....
in a different class, you can access that variable via
ClipPlayer.LastAudioIndex
You can certainly save&load this value as a player pref, which will allow it to store the value between sessions. If you don't care what the lastClipPayed was in the previous session, then no need to save it as a player pref.