Helpers. They are really helpful. After Behaviors, they are probably my favourite CakePHP asset to make. As we work with both CakePHP and Unity3d, we expect them to meet a lot. Embedding rich media in an HTML page is not always as easy as it sounds, therefore it's great that people take the time to build such assets as SWFObject and UnityObject. Included below is a Helper that takes advantage of the UnityObject javascript class and easily allows you to embed Unity files in your CakePHP views.
This Helper is largely modelled on FlashHelper, that in a similar fashion takes advantage of the swfobject mentioned above.
To use this helper, you need the code below and the unityobject.js that you can grab at unifycommunity.com or as part of the post process build package from unity3d.com. Then simply put your .unity3d files in /app/webroot/unity and use it like this:
$unity->init();
echo $unity->render('game',array('width'=>'100%','height'=>'100%'));
or
echo $unity->init(array('width'=>'1000px','height'=>'600px','domId'=>'unityDiv'),true);
echo $html->div(null,null,array('id'=>'unityDiv', 'style'='border: 2px solid black;'));
echo $unity->render('game.unity3d');
Download plain text (Updated 12th august 2009)
- <?php
- /**
- * A helper for embedding unity into your site using javascript.
- * This helper is a wrapper for the javascript UnityObject vendor
- *
- * Install
- *
- * - Save this file to /app/views/helpers/unity.php
- * - Download the unityobject.js file and
- * - place the js file in /app/webroot/js/unityobject.js
- * - Add 'Unity' to helpers array either in AppController or just relevant controller
- *
- * Usage example 1:
- * $unity->init();
- * echo $unity->render('game',array('width'=>'100%','height'=>'100%'));
- *
- * Usage example 2:
- * echo $javascript->link('unityobject');
- * echo $unity->render('game',array('width'=>'400px','height'=>'400px'));
- *
- * Usage example 3:
- * echo $unity->init(array('width'=>'1000px','height'=>'600px','domId'=>'unityDiv'),true);
- * echo $html->div(null,null,array('id'=>'unityDiv', 'style'='border: 2px solid black;'));
- * echo $unity->render('game.unity3d');
- *
- * Usage example 4:
- * $unity->init(array('width'=>'100%','height'=>'100%'));
- * echo $unity->render('game');
- * echo $unity->render('control', array('domId'=>'controllerDiv'));
- *
- *
- * @link http://www.unifycommunity.com/wiki/index.php?title=UnityObject
- * @copyright illustrata.no
- * @license MIT
- * @author Ronny Vindenes
- * @author Alexander Morland aka alkemann
- * @version 1.3.1
- * @modified 12 aug. 2009 by alkemann
- */
- class UnityHelper extends AppHelper {
- // other helpers used within this helper
- public $helpers = array('Html','Javascript');
-
- /**
- * Location of unity3d files relative to webroot.
- * Default 'unity3d' gives /app/webroot/unity/;
- * @var string
- */
- public $unity_folder = 'unity';
-
- /**
- * Default options for the render method
- * @var array
- * @access private
- */
- private $options = array('width'=>640,'height'=>480,'param'=>array());
-
- /**
- * Optional initializing for setting default parameters, includes theunity library.
- *
- * @example echo $unity->init(array(),true);
- * @example $unity->init(array('width'=>200,'height'=>100);
- * @example $unity->init();
- * @return mixed String if $inline = true, true/false depending on success of addint to view
- */
- public function init($options = array(), $inline = false) {
- if (!empty($options)) {
- $this->options = am($this->options, $options);
- }
- if ($inline) {
- return $this->Javascript->link('unityobject');
- } else {
- $view = ClassRegistry::getObject('view');
- if (is_object($view)) {
- $view->addScript($this->Javascript->link('unityobject'));
- return true;
- } else {
- return false;
- }
- }
- }
-
- /**
- * Returns the scripts and noscripts html for embedding a unity file
- *
- * @param string $unityFile Name of unity file to render (optional extension)
- * @param array $options
- * @return string HTML to be echoed
- */
- public function render($unityFile, $options = array()) {
- $options = am ($this->options, $options);
- if (!isset($options['domId'])) {
- $options['domId'] = uniqid('unity');
- }
- $objectDomId = $options['domId'].'Object';
- $embedDomId = $options['domId'].'Embed';
- if (substr($unityFile,-8) !== '.unity3d') {
- $unityFile .= '.unity3d';
- }
-
- $html = '';
-
- $unityLocation = $this->webroot.$this->unity_folder.'/'.$unityFile;
- $unityParams = '';
- $noscriptParams = '';
-
- foreach ($options['param'] as $key => $value) {
- $unityParams .= 'tUnityObject.addParam("'.$key.'","'.$value.'");';
- $noscriptParams .= $this->Html->tag('param', null, array('name'=>$key, 'value'=>$value))."\n";
- }
-
- $html .= $this->Javascript->codeBlock('var tUnityObject = new UnityObject("'.$unityLocation.'","'.
- $options['domId'].'","'.$options['width'].'","'.$options['height'].'");'.$unityParams.'tUnityObject.write();');
-
- $html .= $this->Html->tag('noscript',
- $this->Html->tag('object',
- $this->Html->tag('param',null, array('name' => 'src','value' => $unityLocation))."\n"
- .$noscriptParams
- .$this->Html->tag('embed',null, array(
- 'id' => $embedDomId,
- 'src' => $unityLocation,
- 'width' => $options['width'],
- 'height' => $options['height'],
- 'type' => 'application/vnd.unity',
- 'pluginspage' => 'http://www.unity3d.com/unity-web-player-2.x'
- ))."\n"
- .$this->Html->tag('noembed',
- 'This content requires the Unity Web Player, please click the button
- below to visit the Unity Web Player download page.'.
- $this->Html->tag('br').$this->Html->tag('br').
- $this->Html->link($this->Html->image('http://webplayer.unity3d.com/installation/getunity.png'),
- 'http://www.unity3d.com/unity-web-player-1.x', array(), null, false)
- )."\n",
- array(
- 'id' => $objectDomId,
- 'classid' => 'clsid:444785F1-DE89-4295-863A-D46C3A781394',
- 'width' => $options['width'],
- 'height' => $options['height'],
- 'codebase' => 'http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0'
- )
- )
- );
-
- return $html;
- }
- }
- ?>