Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 richardzzzarnold · Jan 27, 2011 at 03:03 PM · arrayaudiotouchselection

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)]);


}

Comment
Add comment · Show 3
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 Bunny83 · Jan 27, 2011 at 04:05 PM 0
Share

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?

avatar image richardzzzarnold · Jan 27, 2011 at 04:20 PM 0
Share

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.

avatar image richardzzzarnold · Jan 27, 2011 at 04:22 PM 0
Share

Ive updated the scipt.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

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 richardzzzarnold · Jan 27, 2011 at 04:37 PM 0
Share

Thanks Bunny ! I just dont know enough about scripting to understand how to adjust it without the combined animation....it is a similiar question...

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

No one has followed this question yet.

Related Questions

Play sound from an array, not random, and only once 3 Answers

Select a boolean from an array 1 Answer

[URGENT] WebGL export completely different from editor playtesting. [C#] 1 Answer

Creating a matrix or some sort. 1 Answer

How to create an auido array (JavaScript) 1 Answer


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