- Home /
How can I use www and yield in c#?
var url : String; function Start () { www=new WWW(url); audio.clip=www.audioClip; yield www; }
function Update () { if(!audio.isPlaying && audio.clip.isReadyToPlay) audio.Play(); }
How can I change this code to C#?
Answer by duck · Feb 07, 2010 at 03:20 PM
For detailed examples of using yield with WWW and WWWForm in C#, see the answer to this question:
How can I send a string or integer value to server side scripts or web services?
Answer by jashan · Feb 07, 2010 at 02:52 PM
I don't think the JavaScript you posted would work ... but ... anyways: In C#, I'd probably do it this way:
public String url; private WWW www;
public void Start() { WWW www = new WWW(url); StartCoroutine(WaitForAudioClip()); }
public IEnumerator WaitForAudioClip() { yield return www; audio.clip = www.audioClip; }
public void Update() { if (audio.clip != null && !audio.isPlaying && audio.clip.isReadyToPlay) audio.Play(); }
For a more accurate example in UnityScript, see also Unity Scripting Reference - WWW
In the code above, I believe the first line of the Start() routine should read:
www = new WWW(url);
Incidentally, these type's of typos can generally be avoided if the "_" prefix is used for private members (e.g. private WWW _www;
I can never understand why none of the Unity examples follow this standard.
Because that is no standard at all. It's a personal preference. There are many different coding conventions out there and most large software companies have their own set of rules. $$anonymous$$icrosoft doesn't cover the na$$anonymous$$g of private variables in their guildlines as private variables are only relevant to the creator of a class. It's good when you have a consistent code base, but there's no "standard". Read this SO question.
Personally i never use just an underscore prefix because it looks ugly. Depending on code base i either use "m_" as prefix for private member fields or nothing.In addition I usually prefix method parameters with "a" (for argument) which turns all parameters into "aPascalCase". For fields and local variable in general i use "camelCase" for properties "PascalCase".
So the underscore prefix is no standard whatsoever. If you read through all answers on the SO question you will even see people using "_camelCase" for local variables inside methods. The only important thing you should take away from the SO question is:
Choose one and be consistent! That's what matters - João Angelo
Of course it's best to follow those conventions which do define a standard, like the na$$anonymous$$g of public members. Other developers only care about the public interface of a class.
There are a lot different conventions for many things. For example i don't like having the opening curly bracket on the same line as the if / for / while / using statement for several reasons:
It's way more clear where a block starts and where it ends as opening and closing brackets are at the same level.
It generally helps to increase readability as the code isn't too tightly packed. I've seen Java programmers who always put the opening in the same line, even for classes, but in addition they add an empty line between the class statement and it's body for readability.
A missing opening bracket is easily to spot
You can better stack using statements for a block.
Example:
using (var stream = new System.IO.$$anonymous$$emoryStream(data))
using (var reader = new System.IO.StreamReader(stream))
{
// code
}
That's basically how microsoft has written almost all example code on $$anonymous$$SDN. It's a guidline you can follow but in the end, do what you want as long as it's consistent.
Oh, and using a private member field here is actually bad style. You should give "WaitForAudioClip" a WWW parameter and pass the www object along.
@Bunny83 I agree with your comments. I'm just saying that because (most) Unity examples do not distinguish private variables, this can lead to the issue I mentioned. Incidentally, I come from a c++ background and I used to have an issue with C# and _www at first, co$$anonymous$$g from an m_www background. But most C# examples I see use _www, so I thought, I can't beat them, so join them. It's like javascript and braces on the same line as an if(). That still bugs me, but most javascript examples I have seen do that, so I changed to conform.
Your answer
Follow this Question
Related Questions
JSON WebRequest works in 2018.2, throws 403 error in 2018.3 0 Answers
Using Video Player with URL in WebGL 0 Answers
How would I go about getting the results of a javascript variable to a string inside script? 3 Answers
How to access StartCoroutine in a static way 3 Answers
Once Unity loads (Web player) how do I prevent it from being *Focused*? 0 Answers