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 Justinger · Oct 24, 2016 at 05:50 PM · c#audiosourceextension-methodsplayclipatpoint

How do I properly extend AudioSource.PlayClipAtPoint?

Hello, After lots of googling and attempts, I've hit a wall. I am unsure of how to make an extension method for AudioSource.PlayClipAtPoint.
My goal is to simply add a pitch variable (I would love to put in a simple random range to mix it up a bit).

I am successfully able to make my own extension PlayClipAtPointWithPitch, that modifies an existing audioSource.

 public static AudioSource PlayClipAtPointWithPitch(this AudioSource audioSource, AudioClip clip, Vector3 position, float volume, float pitch)
 {
      //Code here... 
      return audioSource;
  }
 
 //Usage Example:
  myAs.PlayClipAtPointWithPitch(hitClip, transform.position, 1.0f, 0.75f);

However, this isn't the same functionality as AudioSource.PlayClipAtPoint(). I would like to call it, and have it create the AudioSource itself, and apply the settings from the given params.

The syntaxes I've tried do not visibly add any extensions to PlayClipAtPoint that I can see. I know it is very likely that I am missing some very simple syntax.

 public static void PlayClipAtPoint(this AudioSource audioSource, AudioClip clip, Vector3 position, float volume, float pitch)
 {
      //Code here... 
 }

Desired usage example: AudioSource.PlayClipAtPoint(hitClip, transform.position, 1.0f, 0.75f);

I am unsure of what "this" in this circumstance is doing, because I am not calling this on any pre-existing object. All the similar examples I've found are a more traditional extension like my working first bit of code, or have left me scratching my head. I know that I can do something similar with a regular static method that I call from file to file, but I would really like to understand how to do this properly.

Any help or advice would be much appreciated. Thanks!

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 hexagonius · Oct 24, 2016 at 08:27 PM

what your trying to do is not possible because PlayClipAtPoint is a static method while extensions only work for instances of a class.
that's what the "this"is for. it refers to the instance you called the method on. it's necessary because you wouldn't have a reference in the method body, because your able to call e.g. clip.Play() without passing anything.
too bad the static version does not return anything. this links gets you the code to quickly redo the whole thing

http://answers.unity3d.com/questions/316575/adjust-properties-of-audiosource-created-with-play.html

Comment
Add comment · Show 2 · 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 Justinger · Oct 24, 2016 at 08:37 PM 0
Share

That was a very clear and helpful answer. I didn't want to commit to an approach until I understood, so now I can move forward. Also, the link is very relevant and handy. Thank you!

avatar image JoshuaMcKenzie · Oct 24, 2016 at 09:28 PM 2
Share

Fun Fact, you can run Extension $$anonymous$$ethods on null instances so with OP's class he can do something like this:

 AudioSource source = null;
 source.PlayClipAtPoint(clip,position,volume,pitch);


as long as the Extension method doesn't actually use the source (and simply creates it own) it'll run fine.

However ins$$anonymous$$d of Extending the AudioSource class I would extend an empty "marker" interface (and yes, you CAN extend interfaces) then you can have any class implement that interface or simply have the class contain an instance of that interface if they ever want to use it.

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

228 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Cannot play a disabled audio source? 1 Answer

How to make menu which is able to list audio files, then load chosen one into the AudioSource in the scene 1 Answer

Disabled audio source problem on a panel popup 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