- Home /
Using WWW.LoadUnityWeb in C#
Hello,
I would like to use the GUI button to load a new game file in a webplayer. The documentation for WWW.LoadUnityWeb and most WWW is in Javascript only for some reason, and I could not find any good examples. I am a bit new at coding so be nice please :)
Below is the basic button I would be using:
using UnityEngine; using System.Collections;
public class interface1 : MonoBehaviour {
void OnGUI(){ if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 10, 100, 25), "Load Game 1")) Debug.Log("Clicked to load"); } }
Thank you!
Answer by Adam Rademacher · Oct 10, 2010 at 07:32 AM
Here is the example translated into C#. Instead of using "Start," It uses a coroutine "StartLoad". So in your GUI Button, call StartCoroutine(StartLoad);
The stuff with the guiTexture is not important, it's used to display a loading bar.
void OnGUI() { if (GUI.Button(new Rect(Screen.width / 2, Screen.height / 10, 100, 25), "Load Game 1")) { StartCoroutine("StartLoad"); Debug.Log("Clicked to load"); } }
// Streams a .unity3d file and displays the progress in a GUI texture. // You need to make sure the GUI texture is set up to have a pixel inset. IEnumerator StartLoad () { // Store the original pixel inset // and modify it from there. Rect originalPixelRect = guiTexture.pixelInset;
// Update the progress bar by scaling the gui texture
// until we reach the end
WWW stream =
new WWW ("http://www.unity3d.com/webplayers/Lightning/lightning.unity3d");
while (!stream.isDone) {
guiTexture.pixelInset.xMax = originalPixelRect.xMin
+ stream.progress * originalPixelRect.width;
yield return null;
}
// Update it one last time before loading
guiTexture.pixelInset.xMax = originalPixelRect.xMax;
stream.LoadUnityWeb();
}
Thank you very much! Appreciate the help. BTW do you think that Unity has forgotten to add the C# and Boo versions of the WWW documentation pages, or is this intentional?
It looks like they probably just forgot about this page.
Your answer
![](https://koobas.hobune.stream/wayback/20220613075229im_/https://answers.unity.com/themes/thub/images/avi.jpg)