gpt4 book ai didi

python - 在 Flask 中加载 Unity WebGL

转载 作者:太空宇宙 更新时间:2023-11-03 20:54:55 26 4
gpt4 key购买 nike

我是 Web 和 Unity 方面的新手。我想做的是在我的 Flask 服务器中运行我的 Unity WebGL 构建。

这是我的文件夹层次结构:

web\
templates\
Build\
UnityLoader.js
WebBuild.json
TemplateData\
style.css
TemplateData.js
index.html
app.py

app.py 有 3 个根,用于加载 index.html 请求的所有 js 文件:

app = Flask(__name__, static_url_path='/templates')
APP_ROOT = os.path.dirname(os.path.abspath(__file__))

@app.route("/")
def index():
return render_template("index.html")

@app.route('/templates/Build/<path:path>')
def send_Buildjs(path):
return send_from_directory('templates/Build', path)

@app.route('/templates/TemplateData/<path:path>')
def send_Templatejs(path):
return send_from_directory('templates/TemplateData', path)

if __name__ == "__main__":
app.run(port=4555, debug=True)

index.html 是 Unity 为 WebGL 应用程序默认生成的文件,稍作修改:

<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Unity WebGL Player | VisualNN</title>

<link rel="shortcut icon" href="{{ url_for('static', filename='TemplateData/favicon.ico') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='TemplateData/style.css') }}">
<script src="{{ url_for('static', filename='TemplateData/UnityProgress.js') }}"></script>
<script src="{{ url_for('static', filename='Build/UnityLoader.js') }}"></script>
<script>
var gameInstance = UnityLoader.instantiate("gameContainer", "{{ url_for('static', filename='Build/WebBuild.json') }}", {onProgress: UnityProgress});

</script>
</head>
<body>
<div class="webgl-content">
<div id="unityContainer" style="width: 960px; height: 600px"></div>
<div class="footer">
<div class="webgl-logo"></div>
<div class="fullscreen" onclick="unityInstance.SetFullscreen(1)"></div>
<div class="title">VisualNN</div>
</div>
</div>
</body>
</html>

在我的 Firefox 浏览器中,图标已加载,所有 js 文件也已加载。我收到图标和 js 文件的 202 和 304 响应消息。但是unity游戏没有加载,没有盒子只是空的。 Firefox 控制台中没有显示任何内容。我猜 Build/WebBuild.json 没有加载?任何人都知道问题是什么?

编辑

当我从 Flask Server 控制台加载页面时,一些额外的信息:

127.0.0.1 - - [10/May/2019 18:34:55] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [10/May/2019 18:34:55] "GET /templates/TemplateData/style.css HTTP/1.1" 304 -
127.0.0.1 - - [10/May/2019 18:34:55] "GET /templates/TemplateData/UnityProgress.js HTTP/1.1" 304 -
127.0.0.1 - - [10/May/2019 18:34:55] "GET /templates/Build/UnityLoader.js HTTP/1.1" 304 -
127.0.0.1 - - [10/May/2019 18:34:55] "GET /templates/TemplateData/webgl-logo.png HTTP/1.1" 304 -
127.0.0.1 - - [10/May/2019 18:34:55] "GET /templates/TemplateData/fullscreen.png HTTP/1.1" 304 -

最佳答案

这是一个最简单的解决方案,您只需更改 app.py 上的静态文件夹和模板文件夹路径,这是一个示例:

`app = Flask(__name__, static_folder="./", template_folder="./")

@app.route('/')
def home():
return render_template("index.html"), 200





if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(debug=True, host='0.0.0.0', port = port)`

有了这个,您不需要在每次发布时更改 index.html 上的任何内容。P.D:您可以硬编码任何您想要的路径,而不仅仅是静态文件夹,在我的例子中,它是我的根项目

enter image description here

关于python - 在 Flask 中加载 Unity WebGL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56081543/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com