原理和目的:resPath.txt写有服务器内网地址,它放在服务器存有Unity WebGL包里三个文件的同级目录下(“三个文件”指Build、TemplateData、index),在Unity里写脚本读取它——这样做的目的是,再去部署新服务器时,只需更改resPath.txt里服务器地址,省去了在Unity代码里修改服务器地址,又得重新打包的麻烦。
1、resPath.txt文件
(注意最后的竖杠是光标,实际是没有东西的)
2、以上txt文件的在服务器中的位置
下图品红色框
3、Unity里读取以上txt文件的脚本
("Application.pathData"路径此处指向,存放webGL包三个文件的父文件夹,也可认为是Unity打出WebGL包)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using System;
public class WebUtil : MonoBehaviour
{
public static WebUtil Instance = null;
/// <summary>
/// 从服务器获取到的资源路径
/// </summary>
private string path_Resource = string.Empty;
private void Awake() {
Instance = this;
}
// Start is called before the first frame update
void Start()
{
StartCoroutine(readAddress());
}
public string GetResourcePath() {
return path_Resource;
}
/// <summary>
/// 从网页Index的同层级获取到tex配置文件 得到文件中的网址
/// </summary>
/// <returns></returns>
private IEnumerator readAddress() {
string _path = Application.dataPath + "/resPath.txt";
WWW www = new WWW(_path);
yield return www;
if (www.isDone)
{
path_Resource = Encoding.UTF8.GetString(www.bytes);
Debug.Log("获取到的地址为:"+ path_Resource);
}
}
————————————————人肉分割线——————————————————
以下是我这边的WebGL包在服务器的部署,只做自己备忘,无参考价值
1/服务器inetpub文件夹
2/wwwroot文件夹
3/DR和WebGL文件夹
4/DR文件夹
5/WebGL文件夹