Use index.php instead of index.html in new WebGL template

We used to be able to use index.php files in our WebGL templates instead of just index.html. Right now I get an error “index.html doesn’t exist” if I try to use index.php though.

There’s an old thread that talks about some workarounds for this, but the .htaccess options I found didn’t work for us anymore.

Unity, can you please re-enable .php extensions on WebGL templates?

For now we’re just manually renaming index.html to index.php. Anyone have a better idea?

Thanks!

1 Like

Can’t use php with Unity 2020.3.0f1.

You can have PHP in the .html file then rename it in your build process:

using System.IO;
// ...
if (File.Exists(path + "\\index.html")) File.Move(path + "\\index.html", path + "\\index.php");

I do the same thing and it’s been working fine from 2019.x->2022.1

1 Like

@adamgolden , BOOM–you’re a genius. Thank you!!

1 Like

You mean OnPostprocessBuild?
Then it would be a good idea to rename it forth from index.php to index.html OnPreprocessBuild.
Do you have a complete solution for this? If yes, please share.

Sure - a quick example for WebGL. Drop this into an Editor folder, customize then hit Tools->Build The Project.

using System.IO;
using UnityEngine;
using UnityEditor;

public class CustomBuildProcessExample : MonoBehaviour
{
#if UNITY_WEBGL
  [MenuItem("Tools/Build The Project")]
  static void BuildMyWebGL()
  {
    // specify the output path, i.e.
    string path = "C:\\wamp64\\www\\yoursite\\yourproject\\yourbuildnumber"; // auto-generate your output path however

    // if you need to create a folder you can do it like this..
    if (!Directory.Exists(path)) Directory.CreateDirectory(path); // note, CreateDirectory expects the parent folder to exist

    // define the scenes you want to include..
    BuildPlayerOptions options = new BuildPlayerOptions
    {
      locationPathName = path,
      scenes = new string[] {
        "Assets/Scenes/WhateverScene.unity",
        "Assets/Scenes/AnotherSceneToInclude.unity"
      },
      target = EditorUserBuildSettings.activeBuildTarget
    };

    // if you want, pick the WebGL template (which you normally pick in Project Settings->Player->Resolution & Presentation)
    // note: templates are in your Unity Editor installation, i.e. \Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Default\
    PlayerSettings.WebGL.template = "APPLICATION:smile:efault"; // for example, replace "Default" with "MyCustomTemplate", to use WebGLTemplates\MyCustomTemplate\

    // if you're using Addressables, you could configure them here, then call:
    // AddressableAssetSettings.BuildPlayerContent();

    // build your app/game..
    BuildPipeline.BuildPlayer(options);

    // then post-process whatever, i.e.

    // if you're reusing a folder that might still contain previous index.php, you can delete it like this
    if (File.Exists(path + "\\index.php")) File.Delete(path + "\\index.php");
   
    // and as noted in the previous post, rename whatever .html files to .php
    if (File.Exists(path + "\\index.html")) File.Move(path + "\\index.html", path + "\\index.php");

    Debug.Log("Result: " + path);
  }
#endif
}
1 Like

@adamgolden , that’s an excellent enhancement and worked perfectly for me. Thank you again!!

1 Like