3iX

Reliable $1 Web Hosting by 3iX

Saturday, September 25, 2010

Executing arbitrary browser code from Unity web player content

You don't even have to define functions in the embedding web page, instead you can use the Application.ExternalEval() function to execute arbitrary browser code from the web player content.
The following example checks that the page embedding the web player content is fetched from a certain host (unity3d.com), if that's not the case then it will redirect to another URL. This technique can be used to prevent deep linking to your web player content:
Application.ExternalEval(
    "if(document.location.host != 'unity3d.com') { document.location='http://unity3d.com'; }"
);

Calling web page functions from Unity web player content

In order to call a web page function from within your Unity web player content you must use the Application.ExternalCall() function. Using that function you can call any JavaScript function defined in the web page, passing any number of parameters to it. Here is an example Unity script that uses theApplication.ExternalCall() function to call a function named SayHello() found within the web page, passing a piece of string data as an argument:
Application.ExternalCall( "SayHello", "The game says hello!" );
The web page would need to define the SayHello() function, for example:

Calling Unity web player content functions from the web page

The Unity Web Player plugin and ActiveX Controls both have a function, SendMessage(), that can be called from a web page in order to call functions within Unity web player content. This function is very similar to the GameObject.SendMessage function in the Unity scripting API. When called from a web page you pass an object name, a function name and a single argument, and SendMessage() will call the given function in the given game object.

In order to call the Unity Web Player's SendMessage() function you must first get a reference to the Unity web player content object being displayed. You can use JavaScript's document object and its getElementById() function to obtain a reference to the content. Here is an example JavaScript function that would execute the SendMessage() function on the Unity web player content with an object/embed tag id value of UnityContent; in turn SendMessage() will then call the function MyFunction() on the game object named MyObject, passing a piece of string data as an argument:


Inside of the Unity web player content you need to have a script attached to the GameObject named MyObject, and that script needs to implement a function named MyFunction:

function MyFunction(param : String)
{
    Debug.Log(param);
}
A single string, integer or float argument must be passed when using SendMessage(), the parameter is required on the calling side. If you don't need it then just pass a zero or other default value and ignore it on the Unity side. Additionally, the game object specified by the name can be given in the form of a path name. For example, /MyObject/SomeChild where SomeChild must be a child of MyObject and MyObject must be at the root level due to the '/' in front of its name.

The default html file generated when you publish web player content includes both an object and embed tag in order to have the content load properly in all browsers. In order to allow browser-based JavaScript to distinguish between the two tag elements they each use a unique id value, UnityObject for the object tag and UnityEmbed for the embed tag. Because of this, the default html file also includes a JavaScript function, GetUnity(), that performs some simple browser detection and returns a reference to the tag element in use. Here is an example using that function:

Wednesday, May 12, 2010

Play Streaming Video in Unity 3D

Video can be streamed in Unity3D only when it is in ogg or ogv format. To get ogv file

  1. put any video file (mov, avi, mp4 ) inside unity 3d project window. 
  2. Right Click the movie file and choose export ogg file
  3. Save the file in Desktop or some known location
  4. setup your web server both remote and local web server will work fine. I'll go for local web server I'll get and install xampp from apache friends
  5. Copy paste the exported files i.e. the ogv files in the xampp htdocs folder such that it will be accessed from browser as http://localhost/filename.ogv
Now use the following code as cs file to play videos

using UnityEngine;
using System.Collections;

public class streaming : MonoBehaviour {

private MovieTexture  m;
private string playButtonString="play",url= "http://localhost/";
private string [] mediaSource = {"1.ogv","2.ogv","3.ogv","4.ogv","5.ogv"};
public AudioSource AS;
bool isMenu = true;
int tmp;
private WWW www;

// Use this for initialization

void Start () {
 
}
// Update is called once per frame
void Update () {
if(Input.GetKeyUp(KeyCode.Escape))

{
//~ Screen.showCursor = true;
Screen.fullScreen = false;
isMenu = true;
}
if(Screen.fullScreen == true) {
if(Input.mousePosition.x >=20 && Input.mousePosition.x<=(Screen.width -40) && Input.mousePosition.y>=0 && Input.mousePosition.y <= 80)
isMenu = true;
else
isMenu = false;
}
else
isMenu = true;
}
void OnGUI() {
if(m!=null)
GUI.DrawTexture (new Rect (0,0,Screen.width,Screen.height),m,ScaleMode.StretchToFill);
else
GUI.Label(new Rect(50,100,200,50), "Video is yet to Start");
if(isMenu) {
GUI.Box(new Rect(20, Screen.height - 60, Screen.width-40,60),"");
for ( int i=0;i < mediasource.length;i++) {
if(m!=null)
if (i == tmp && m.isPlaying) 
playButtonString="pause"; //it's playing so the button should pause.
else
playButtonString="play"; //it's not playing and the button should play the movie.
if (GUI.Button (new Rect (30 + (200 * i),Screen.height-40,100,30),playButtonString + i) == true)
{
if(m!=null)
if (m.isPlaying == true) {
m.Pause();
AS.Pause();
if (i == tmp)
return;
}
else if(m.isReadyToPlay && i == tmp)
{
m.Play();
AS.Play();
return;
}
www = new WWW(url+mediaSource[i]);
m = www.movie;
AS.clip = m.audioClip;
while(!m.isReadyToPlay) {
SomeCoroutine();
if (!m.isPlaying) {
m.Play();
if(!AS.isPlaying)
AS.Play();
}
}
Screen.fullScreen = true;
isMenu = false;
tmp = i;
}
if (GUI.Button (new Rect(130 + (200 *i),Screen.height-40,100,30),"Stop"+ i)==true && i==tmp) {
m.Stop();
AS.Stop();
}
}
}
else {
GUI.Label( new Rect(Screen.width - 200, 10,200,20), "Press Esc for Menu or");
if(GUI.Button( new Rect(Screen.width - 200, 30,100,20), "Click Here")) {
isMenu = true;
Screen.fullScreen = false;
}
}
}
IEnumerator SomeCoroutine () {
        // Wait for one frame
        yield return www;
      }
}