Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'WebGL' started by bigbrainz, Aug 18, 2021.

  1. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    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!
     
    tomekkie2 likes this.
  2. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    949
    Can't use php with Unity 2020.3.0f1.
     
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    You can have PHP in the .html file then rename it in your build process:
    Code (CSharp):
    1. using System.IO;
    2. // ...
    3. 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
     
    wetcircuit likes this.
  4. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    @polemical, BOOM--you're a genius. Thank you!!
     
    adamgolden likes this.
  5. tomekkie2

    tomekkie2

    Joined:
    Jul 6, 2012
    Posts:
    949
    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.
     
  6. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,494
    Sure - a quick example for WebGL. Drop this into an Editor folder, customize then hit Tools->Build The Project.
    Code (CSharp):
    1. using System.IO;
    2. using UnityEngine;
    3. using UnityEditor;
    4.  
    5. public class CustomBuildProcessExample : MonoBehaviour
    6. {
    7. #if UNITY_WEBGL
    8.   [MenuItem("Tools/Build The Project")]
    9.   static void BuildMyWebGL()
    10.   {
    11.     // specify the output path, i.e.
    12.     string path = "C:\\wamp64\\www\\yoursite\\yourproject\\yourbuildnumber"; // auto-generate your output path however
    13.  
    14.     // if you need to create a folder you can do it like this..
    15.     if (!Directory.Exists(path)) Directory.CreateDirectory(path); // note, CreateDirectory expects the parent folder to exist
    16.  
    17.     // define the scenes you want to include..
    18.     BuildPlayerOptions options = new BuildPlayerOptions
    19.     {
    20.       locationPathName = path,
    21.       scenes = new string[] {
    22.         "Assets/Scenes/WhateverScene.unity",
    23.         "Assets/Scenes/AnotherSceneToInclude.unity"
    24.       },
    25.       target = EditorUserBuildSettings.activeBuildTarget
    26.     };
    27.  
    28.     // if you want, pick the WebGL template (which you normally pick in Project Settings->Player->Resolution & Presentation)
    29.     // note: templates are in your Unity Editor installation, i.e. \Editor\Data\PlaybackEngines\WebGLSupport\BuildTools\WebGLTemplates\Default\
    30.     PlayerSettings.WebGL.template = "APPLICATION:Default"; // for example, replace "Default" with "MyCustomTemplate", to use WebGLTemplates\MyCustomTemplate\
    31.  
    32.     // if you're using Addressables, you could configure them here, then call:
    33.     // AddressableAssetSettings.BuildPlayerContent();
    34.  
    35.     // build your app/game..
    36.     BuildPipeline.BuildPlayer(options);
    37.  
    38.     // then post-process whatever, i.e.
    39.  
    40.     // if you're reusing a folder that might still contain previous index.php, you can delete it like this
    41.     if (File.Exists(path + "\\index.php")) File.Delete(path + "\\index.php");
    42.    
    43.     // and as noted in the previous post, rename whatever .html files to .php
    44.     if (File.Exists(path + "\\index.html")) File.Move(path + "\\index.html", path + "\\index.php");
    45.  
    46.     Debug.Log("Result: " + path);
    47.   }
    48. #endif
    49. }
     
    tomekkie2 likes this.
  7. bigbrainz

    bigbrainz

    Joined:
    Jul 21, 2015
    Posts:
    175
    @polemical, that's an excellent enhancement and worked perfectly for me. Thank you again!!
     
    adamgolden likes this.