x


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!

more ▼

asked Oct 09 '10 at 09:34 PM

Faskoona gravatar image

Faskoona
111 3 4 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

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();
    }
more ▼

answered Oct 10 '10 at 07:32 AM

Adam Rademacher gravatar image

Adam Rademacher
1.1k 1 3 19

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?

Oct 10 '10 at 10:03 PM Faskoona

It looks like they probably just forgot about this page.

Oct 11 '10 at 12:31 AM Adam Rademacher
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3674
x810
x525

asked: Oct 09 '10 at 09:34 PM

Seen: 1474 times

Last Updated: Oct 09 '10 at 09:34 PM