Unity embed helper for CakePHP

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)

  1. <?php
  2. /**
  3. * A helper for embedding unity into your site using javascript.
  4. * This helper is a wrapper for the javascript UnityObject vendor
  5. *
  6. * Install
  7. *
  8. * - Save this file to /app/views/helpers/unity.php
  9. * - Download the unityobject.js file and
  10. * - place the js file in /app/webroot/js/unityobject.js
  11. * - Add 'Unity' to helpers array either in AppController or just relevant controller
  12. *
  13. * Usage example 1:
  14. * $unity->init();
  15. * echo $unity->render('game',array('width'=>'100%','height'=>'100%'));
  16. *
  17. * Usage example 2:
  18. * echo $javascript->link('unityobject');
  19. * echo $unity->render('game',array('width'=>'400px','height'=>'400px'));
  20. *
  21. * Usage example 3:
  22. * echo $unity->init(array('width'=>'1000px','height'=>'600px','domId'=>'unityDiv'),true);
  23. * echo $html->div(null,null,array('id'=>'unityDiv', 'style'='border: 2px solid black;'));
  24. * echo $unity->render('game.unity3d');
  25. *
  26. * Usage example 4:
  27. * $unity->init(array('width'=>'100%','height'=>'100%'));
  28. * echo $unity->render('game');
  29. * echo $unity->render('control', array('domId'=>'controllerDiv'));
  30. *
  31. *
  32. * @link http://www.unifycommunity.com/wiki/index.php?title=UnityObject
  33. * @copyright illustrata.no
  34. * @license MIT
  35. * @author Ronny Vindenes
  36. * @author Alexander Morland aka alkemann
  37. * @version 1.3.1
  38. * @modified 12 aug. 2009 by alkemann
  39. */
  40. class UnityHelper extends AppHelper {
  41. // other helpers used within this helper
  42. public $helpers = array('Html','Javascript');
  43.  
  44. /**
  45.   * Location of unity3d files relative to webroot.
  46.   * Default 'unity3d' gives /app/webroot/unity/;
  47.   * @var string
  48.   */
  49. public $unity_folder = 'unity';
  50.  
  51. /**
  52.   * Default options for the render method
  53.   * @var array
  54.   * @access private
  55.   */
  56. private $options = array('width'=>640,'height'=>480,'param'=>array());
  57.  
  58. /**
  59.   * Optional initializing for setting default parameters, includes theunity library.
  60.   *
  61.   * @example echo $unity->init(array(),true);
  62.   * @example $unity->init(array('width'=>200,'height'=>100);
  63.   * @example $unity->init();
  64.   * @return mixed String if $inline = true, true/false depending on success of addint to view
  65.   */
  66. public function init($options = array(), $inline = false) {
  67. if (!empty($options)) {
  68. $this->options = am($this->options, $options);
  69. }
  70. if ($inline) {
  71. return $this->Javascript->link('unityobject');
  72. } else {
  73. $view = ClassRegistry::getObject('view');
  74. if (is_object($view)) {
  75. $view->addScript($this->Javascript->link('unityobject'));
  76. return true;
  77. } else {
  78. return false;
  79. }
  80. }
  81. }
  82.  
  83. /**
  84.   * Returns the scripts and noscripts html for embedding a unity file
  85.   *
  86.   * @param string $unityFile Name of unity file to render (optional extension)
  87.   * @param array $options
  88.   * @return string HTML to be echoed
  89.   */
  90. public function render($unityFile, $options = array()) {
  91. $options = am ($this->options, $options);
  92. if (!isset($options['domId'])) {
  93. $options['domId'] = uniqid('unity');
  94. }
  95. $objectDomId = $options['domId'].'Object';
  96. $embedDomId = $options['domId'].'Embed';
  97. if (substr($unityFile,-8) !== '.unity3d') {
  98. $unityFile .= '.unity3d';
  99. }
  100.  
  101. $html = '';
  102.  
  103. $unityLocation = $this->webroot.$this->unity_folder.'/'.$unityFile;
  104. $unityParams = '';
  105. $noscriptParams = '';
  106.  
  107. foreach ($options['param'] as $key => $value) {
  108. $unityParams .= 'tUnityObject.addParam("'.$key.'","'.$value.'");';
  109. $noscriptParams .= $this->Html->tag('param', null, array('name'=>$key, 'value'=>$value))."\n";
  110. }
  111.  
  112. $html .= $this->Javascript->codeBlock('var tUnityObject = new UnityObject("'.$unityLocation.'","'.
  113. $options['domId'].'","'.$options['width'].'","'.$options['height'].'");'.$unityParams.'tUnityObject.write();');
  114.  
  115. $html .= $this->Html->tag('noscript',
  116. $this->Html->tag('object',
  117. $this->Html->tag('param',null, array('name' => 'src','value' => $unityLocation))."\n"
  118. .$noscriptParams
  119. .$this->Html->tag('embed',null, array(
  120. 'id' => $embedDomId,
  121. 'src' => $unityLocation,
  122. 'width' => $options['width'],
  123. 'height' => $options['height'],
  124. 'type' => 'application/vnd.unity',
  125. 'pluginspage' => 'http://www.unity3d.com/unity-web-player-2.x'
  126. ))."\n"
  127. .$this->Html->tag('noembed',
  128. 'This content requires the Unity Web Player, please click the button
  129. below to visit the Unity Web Player download page.'.
  130. $this->Html->tag('br').$this->Html->tag('br').
  131. $this->Html->link($this->Html->image('http://webplayer.unity3d.com/installation/getunity.png'),
  132. 'http://www.unity3d.com/unity-web-player-1.x', array(), null, false)
  133. )."\n",
  134. array(
  135. 'id' => $objectDomId,
  136. 'classid' => 'clsid:444785F1-DE89-4295-863A-D46C3A781394',
  137. 'width' => $options['width'],
  138. 'height' => $options['height'],
  139. 'codebase' => 'http://webplayer.unity3d.com/download_webplayer-2.x/UnityWebPlayer.cab#version=2,0,0,0'
  140. )
  141. )
  142. );
  143.  
  144. return $html;
  145. }
  146. }
  147. ?>