UnityObject

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(Description)
Line 1: Line 1:
 
==Description==
 
==Description==
This wraps all of OTEE's browser detection and communication in one javascript file and allows you to use minimal code to embed .unityweb's in your web pages.
+
This wraps all of UT's browser detection and communication in one javascript file and allows you to use minimal code to embed .unityweb's in your web pages.
  
The code is based on the examples OTEE has given in the manual and the work Geoff Stearns has done with SWFObject
+
The code is based on the examples UT has given in the manual and the work Geoff Stearns has done with SWFObject
  
 
Now Unity Developers can embed in style & comfort ;)
 
Now Unity Developers can embed in style & comfort ;)

Revision as of 15:57, 29 April 2008

Description

This wraps all of UT's browser detection and communication in one javascript file and allows you to use minimal code to embed .unityweb's in your web pages.

The code is based on the examples UT has given in the manual and the work Geoff Stearns has done with SWFObject

Now Unity Developers can embed in style & comfort ;)

Tested in Safari 2.0.4, FireFox 2.0.0.2 and Opera 9.01

Compatible with CookieCutter

Usage

All it takes to embed is the following;

<script type="text/javascript" src="unityobject.js"></script>
<script type="text/javascript">
 var uniObj = new UnityObject("example.unityweb", "example", "640", "480", "000000", "000000");
 uniObj.write();
</script>

To send a message to unity all you need to do is <javascript>uniObj.msg("MyObject", "MyFunction", "Hello Unity!"); </javascript>

To use Pro Features;

<script type="text/javascript">
 var uniObj = new UnityObject("proExample.unityweb", "proExample", "640", "480");
uniObj.addParam("logoimage", "MyLogo.png");
uniObj.addParam("progressbarimage", "MyProgressBar.png");
uniObj.addParam("progressframeimage", "MyProgressFrame.png");
uniObj.write();
</script>

If Plugin Not Found Write Alternate HTML;

<script type="text/javascript">
 var uniObj = new UnityObject("example.unityweb", "example", "640", "480");
uniObj.setAttribute("altHTML", "<a href='http://unity3d.com/unitywebplayer.html' title='Go to unity3d.com to install the Unity Web Player'>Install the Unity Web Player</a>");
uniObj.write();
</script>

JavaScript - unityobject.js

<javascript>// Licensed Under the "I will buy milkytreat a beer" license. // version 0.3 last updated 16/03/07 // Special Thanks to OTEE and Geoff Stearns

if(typeof otee == "undefined") var otee = new Object(); otee.UnityObject = function (unityweb, id, w, h, bg, border, redirectUrl) {

   if (!document.getElementById) { return; }
   this.attributes = new Array();
   this.params = new Object();
   if(unityweb) { this.setAttribute('src', unityweb); }

if(id) { this.setAttribute('id', id); } if(w) { this.setAttribute('width', w); } if(h) { this.setAttribute('height', h); } if(bg) { this.addParam('backgroundcolor', bg); } if(border) { this.addParam('bordercolor', border); }

   this.setAttribute('redirectUrl', );

if(redirectUrl) { this.setAttribute('redirectUrl', redirectUrl); } }

otee.UnityObject.prototype = {

   detectUnityWebPlayer: function () {
    var tInstalled = false;
      if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) {
        document.write(" \n");

document.write('<scr' + 'ipt LANGUAGE="VBScript"> \n');

         document.write("on error resume next \n");
        document.write("set tControl = CreateObject('UnityWebPlayer.UnityWebPlayerAXCtrl.1') \n");
         document.write("if IsObject(tControl) then \n");
           document.write("tFound = 1 \n");
           document.write("else \n");
           document.write("tFound = 0 \n");
           document.write("end if \n");
        document.write("</scr"+"ipt> \n");
           document.write(" \n");
           tInstalled = (tFound == 1);
       } else {
           if (navigator.mimeTypes && navigator.mimeTypes["application/x-unity"] && navigator.mimeTypes["application/x-unity"].enabledPlugin) {
               if (navigator.plugins && navigator.plugins["Unity Web Player"]) {
                tInstalled = true;	
            }
        }	
       }
       return tInstalled;	
   },
   writeEmbedDOM: function() {
    var uniSrc = '<object classid="clsid:36D04559-44B7-45E0-BA81-E1508FAB359F" codebase="http://otee.dk/download_webplayer/UnityWebPlayer.cab" id="'+this.getAttribute('id')+'_object" width="'+this.getAttribute('width')+'" height="'+this.getAttribute('height')+'"><param name="src" value="'+this.getAttribute('src')+'" />'; 
   var params = this.getParams();
   for(var key in params) {
       uniSrc += '<param name="'+ key +'" value="'+ params[key] +'" />';
   }
    uniSrc += '<embed type="application/x-unity" pluginspage="http://otee.dk/getunityplayer.html" id="'+this.getAttribute('id')+'_embed" width="'+this.getAttribute('width')+'" height="'+this.getAttribute('height')+'" src="'+this.getAttribute('src')+'"';
    var params = this.getParams();
   for(var key in params){
       uniSrc += [key] +'="'+ params[key] +'" ';
   }
    uniSrc += ' /></object\>';
   return uniSrc;
   },
   addParam: function(name, value){

this.params[name] = value; }, getParams: function(){ return this.params; },

   setAttribute: function(name, value){

this.attributes[name] = value; }, getAttribute: function(name){ return this.attributes[name]; },

   write: function (elementId) {
       if(this.detectUnityWebPlayer()) {
      document.write(this.writeEmbedDOM());
      this.findEar();
       return true;
       } else {
           if(this.getAttribute('altHTML') != "") {
               document.write(this.getAttribute('altHTML'));
           } else if(this.getAttribute('redirectUrl') != "") {
               document.location.replace(this.getAttribute('redirectUrl'));
           }
       }
   return false;
   },
   findEar: function () {
       this.unityEar = "";

if (navigator.appVersion.indexOf("MSIE") != -1 && navigator.appVersion.toLowerCase().indexOf("win") != -1) { this.unityEar = document.getElementById(this.getAttribute('id')+"_object"); } else if (navigator.appVersion.toLowerCase().indexOf("safari") != -1) { this.unityEar = document.getElementById(this.getAttribute('id')+"_object") } else { this.unityEar = document.getElementById(this.getAttribute('id')+"_embed"); }

   document.Unity = this.unityEar;
   },
   msg: function (unObj, unFunc, unVar) {
       this.unityEar.SendMessage(unObj, unFunc, unVar);
   }    

} if (!document.getElementById && document.all) { document.getElementById = function(id) { return document.all[id]; }} var UnityObject = otee.UnityObject;</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox