- Home /
audio array touch selection
I want to have a cube floating in space so that there is a floating gui in the center-front of it. I want to be able to drag the cube around the screen and when the gui button is pressed it plays an audio file from an array of audio files. I know how to separate animations and play random selections from an array of animations but am uncertain how to do it with audio files. This is what i have so far....
public var clipStorage : AudioClip[];
var sound : AudioClip; var customButton : GUIStyle; var materials : Material[]; var buttonTexture : Texture; //The texture to use on the button var offset = Vector3.up; //World space offset. 1 unit above object by default var clampToScreen = false; //Will the label be visible if object is off screen var clampBorderSize = .05; //screen space to leave when clamped var useMainCamera = true; //Use the camera tagged MainCamera var cameraToUse : Camera; //Camera to use if useMainCamera is false private var cam : Camera; //The camera we're using private var screenPos : Vector3; //The screen position of the button
function Start () { if(useMainCamera) cam = Camera.main; else cam = cameraToUse;
} function FixedUpdate(){ //////// screen orientation if ((Input.deviceOrientation ==Input.deviceOrientation.LandscapeLeft) && (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeLeft)){ iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeLeft; } if ((Input.deviceOrientation == Input.deviceOrientation.LandscapeRight) && (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.LandscapeRight)){ iPhoneSettings.screenOrientation = iPhoneScreenOrientation.LandscapeRight; } if ((Input.deviceOrientation== Input.deviceOrientation.Portrait) && (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.Portrait)){ iPhoneSettings.screenOrientation = iPhoneScreenOrientation.Portrait; } if ((Input.deviceOrientation ==Input.deviceOrientation.PortraitUpsideDown) && (iPhoneSettings.screenOrientation != iPhoneScreenOrientation.PortraitUpsideDown)){ iPhoneSettings.screenOrientation = iPhoneScreenOrientation.PortraitUpsideDown; }
}
function Update () { if(clampToScreen) { var relativePosition = cam.transform.InverseTransformPoint(transform.position); relativePosition.z = Mathf.Max(relativePosition.z, 1.0); screenPos= cam.WorldToScreenPoint(cam.transform.TransformPoint(relativePosition + offset)); screenPos= Vector3(Mathf.Clamp(screenPos.x, clampBorderSize,Screen.width-(clampBorderSize+buttonTexture.width)), Mathf.Clamp(screenPos.y, clampBorderSize,Screen.height-(clampBorderSize+buttonTexture.height)), screenPos.z); } else screenPos= cam.WorldToScreenPoint(transform.position + offset); }
function OnGUI() { if(GUI.Button(Rect(screenPos.x-30,Screen.height - screenPos.y+50,50,50),"drums"))
audio.PlayOneShot(clipStorage[Random.Range(0,clipStorage.Length)]);
}
Do you want only one single button (like you already have) to play a random audioclip out of many or do you want to display a button for each clip?
Only one.Actually, i have just solved it by putting this in
public var clipStorage : AudioClip[];
and then putting audio.PlayOneShot(clipStorage[Random.Range(0,clipStorage.Length)]); in GUI function.
but....any idea how I can get it to stop the playing clip ( or fade out ) when new clip starts ? I am getting too much noise as clips overlay.
Answer by Bunny83 · Jan 27, 2011 at 04:29 PM
Didn't you ask a similar question here! that got already answered?
Anyway, first of all if you have more than one audioclip you need to change
var sound : AudioClip;
into
var sound : AudioClip[];
Then you can assign as many AudioClips as you want in the inspector. Just increase the elementcount.
As long as you keep up the clips in same order as your animations, the index you randomly choose will be the same. If you have a lot animations or you add or remove clips very often that would be a pain to keep them in the right order. I would suggest using an "AnimSoundGroup" like skovacs1 told you in his post.
Because you talk about splitting animations into single AnimationClips, that's not possible for AudioClips. You have to import you audio files separately. If your audio is one single file you have to split it outside Unity with an audio editing program like Audacity (maybe not the best one, there are even simpler audiosplitter available).
Thanks Bunny ! I just dont know enough about scripting to understand how to adjust it without the combined animation....it is a similiar question...