Search Unity

WebGLTemplates folder in Packages

Discussion in 'Package Manager' started by De-Panther, May 15, 2019.

  1. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    Hi,

    Unity WebGL has a feature of web page template, and you use it by placing the files under a folder in Assets/WebGLTemplates
    https://docs.unity3d.com/Manual/webgl-templates.html

    I would like to place a template in a package that I'm working on.
    The current solution is similar to TextMeshPro with the unitypackage that comes with the package and can be unpacked from a menu.
    But are there other ideas for people here?
    Or is it possible to make the Editor check for WebGLTemplates in the Packages folder?

    Thanks
     
    onMouseUp likes this.
  2. mathieur

    mathieur

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    31
    Hello! You can always add your template as a package sample. This would allow users to add the template from the package manager UI's Import Sample action. Here is an example of what it looks like from the package manager UI: Screen Shot 2019-05-15 at 2.17.53 PM.png

    I have also attached an example package that you can inspect to see how to go about doing this.
     

    Attached Files:

  3. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    Thanks, I already tried it.
    The issue with Import Sample, is that it makes a copy in sub-sub-folder with the package name and version. The WebGLTemplates folder must be under Assets folder, and can't be under sub-folders
     
  4. mathieur

    mathieur

    Unity Technologies

    Joined:
    Jul 22, 2015
    Posts:
    31
    Ah! I see. Indeed, I can confirm that the WebGLTemplates folder name is hard-coded to check under assets, and there currently isn't a way to extend or modify the import sample action. So your are currently left with the proposed solution of adding your import mechanism in a special menu. I have added a task to see if we can fix both these issues.
     
    antoined73 and De-Panther like this.
  5. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
  6. bsawyer

    bsawyer

    Joined:
    May 6, 2014
    Posts:
    37
    I'm in the same boat as @De-Panther. Any update on this - either from Unity or a clever workaround by OP?

    Thanks.
     
  7. bsawyer

    bsawyer

    Joined:
    May 6, 2014
    Posts:
    37
    What I've ended up doing is perhaps not very future-proof, but I exported my WebGLTemplates into a .unitypackage file, and placed that .unitypackage file in a folder of my UPM package called "Optional Asset Packages" (in my use case, the user of my UPM package may or may not have need to run WebGL builds, hence I'm communicating to them that this is optional). So far in 2019.3.1 I'm still able (it hasn't broken yet, lol) to doubleclick the .unitypackage file and it imports under Assets as expected.

    I've also done the same thing with an Android mainTemplate.gradle file (again, the user of my package may or may not have need of Android builds)

    I said this approach probably isn't future-proof because it relies on the .unitypackage file convention, and I'm essentially mixing "old" and "new" Unity Package concepts. But for the time being it's a fairly clean and straightforward way to provide assets in my package that the user will need to import into their Assets folder in order to use, without simply having them be "loose" files that they need to copy over manually.

    upload_2020-2-21_15-51-50.png
     
  8. De-Panther

    De-Panther

    Joined:
    Dec 27, 2009
    Posts:
    589
    Unity QA replied that they won't fix this for now.

    I don't like the .unitypackage aproach. You can use folder names with "~" or "." at the start, and the editor would ignore them. Then you can create a menu item which will copy them to your selected folder.
    It's much more simple than making sure to pack each time you update your package. And you can use symlink in your project to the hidden folder, so you would be able to work on the content in your development project
     
  9. antoined73

    antoined73

    Joined:
    Feb 23, 2014
    Posts:
    24
    I managed to build WebGL apps using WebGLTemplates in my custom package on windows with this line of code before launching the build :
    Code (CSharp):
    1. // By default, unity will search the template in Assets/WebGLTemplates/
    2. PlayerSettings.WebGL.template = "PROJECT:../../Packages/com.company.mypackage/WebGLTemplates/MyTemplate";
    However, I does not work on linux..

    Any news on this ? Is there a hack to make the build still possible in linux ?
    Thanks

    EDIT:
    I managed to make it work anyway on linux, but not the way I wanted..

    First, I discovered that the template path was wrong on linux because the folder WebGLTemplates wasn't there (even if we don't care about this folder).
    So what I've done, is creating the folder in the Assets (if it was not existing before) and deleting it after the build was done.
    The build was going on and exited with success. But only the "Build" folder was exported in the final WebGLBuild (no index, no Template datas).

    So I ended up copying the WebGLTemplate located in my custom package to the Assets, launched the build and deleted the template in the assets just after.
    Its not ideal, but it does the work !
     
    Last edited: Jan 22, 2021
  10. aFeesh

    aFeesh

    Joined:
    Feb 12, 2015
    Posts:
    35
    Any update on the status of fixing these issues?
     
  11. UnityMaru

    UnityMaru

    Community Engagement Manager PSM

    Joined:
    Mar 16, 2016
    Posts:
    1,227
    Hey there. There's no further updates to provide at present other than to say it's an issue still in our backlog.
     
  12. george_playbite

    george_playbite

    Joined:
    Sep 27, 2018
    Posts:
    38
    Is there an issue we can track with updates for the support for using WebGLTemplates in a custom folder?
     
  13. george_playbite

    george_playbite

    Joined:
    Sep 27, 2018
    Posts:
    38
  14. george_playbite

    george_playbite

    Joined:
    Sep 27, 2018
    Posts:
    38
    For anyone looking for the workaround that @antoined73 mentioned, here's code that seems to be working:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor.Build;
    3. using UnityEditor.Build.Reporting;
    4. using UnityEditor;
    5. using System.IO;
    6.  
    7. #if UNITY_WEBGL
    8. namespace FooBar.Editor
    9. {
    10.     /// <summary>
    11.     /// Pre processor to set the right web gl template.
    12.     /// </summary>
    13.     public class WebGLTemplatePreProcessor : IPreprocessBuildWithReport
    14.     {
    15.         public int callbackOrder => 0;
    16.  
    17.         public void OnPreprocessBuild(BuildReport report)
    18.         {
    19.             Debug.Log("Starting to pre-process webl template...");
    20.  
    21.             var destinationFolder = Path.GetFullPath("Assets/WebGLTemplates/MyTemplateName");
    22.  
    23.             var sourceFolder = Path.GetFullPath("Packages/com.foo.bar/Assets/WebGLTemplates/MyTemplateName");
    24.  
    25.             Debug.Log($"Copying template from {sourceFolder}...");
    26.  
    27.             FileUtil.ReplaceDirectory(sourceFolder, destinationFolder);
    28.  
    29.             AssetDatabase.Refresh();
    30.  
    31.             Debug.Log($"Setting webgl template, old was = {PlayerSettings.WebGL.template}");
    32.  
    33.             PlayerSettings.WebGL.template = "PROJECT:MyTemplateName";
    34.  
    35.             Debug.Log($"Set webgl template to {PlayerSettings.WebGL.template}");
    36.  
    37.             Debug.Log("Done pre-processing webl template...");
    38.         }
    39.     }
    40. }
    41. #endif
     
    antoined73 likes this.
  15. thehen2

    thehen2

    Joined:
    Apr 1, 2014
    Posts:
    54
    Also having this issue
     
  16. Hellfim

    Hellfim

    Joined:
    Sep 11, 2014
    Posts:
    123
    I would also like to have this feature implemented