【发布时间】:2021-08-16 22:19:42
【问题描述】:
在大量优化音频和纹理数据后,我的 Unity webGL 游戏在移动网络浏览器上运行。保存游戏数据在桌面环境中工作正常。但是当我在移动浏览器上玩同一个 webGl 游戏时,似乎在任何地方都没有保存游戏数据。下面给出了我在本地保存和加载数据的代码。
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
public class DataAccess
{
[DllImport ( "__Internal" )]
private static extern void SyncFiles ( );
[DllImport ( "__Internal" )]
private static extern void WindowAlert ( string message );
public static void Save ( Stats stats )
{
string dataPath = string.Format ( "{0}/Stats.dat", Application.persistentDataPath );
BinaryFormatter binaryFormatter = new BinaryFormatter ( );
FileStream fileStream;
try
{
if ( File.Exists ( dataPath ) )
{
File.WriteAllText ( dataPath, string.Empty );
fileStream = File.Open ( dataPath, FileMode.Open );
}
else
{
fileStream = File.Create ( dataPath );
}
binaryFormatter.Serialize ( fileStream, stats );
fileStream.Close ( );
if ( Application.platform == RuntimePlatform.WebGLPlayer )
{
SyncFiles ( );
}
}
catch ( Exception e )
{
PlatformSafeMessage ( "Failed to Save: " + e.Message );
}
}
public static Stats Load (Stats stats )
{
//Stats stats = null;
string dataPath = string.Format ( "{0}/Stats.dat", Application.persistentDataPath );
try
{
if ( File.Exists ( dataPath ) )
{
BinaryFormatter binaryFormatter = new BinaryFormatter ( );
FileStream fileStream = File.Open ( dataPath, FileMode.Open );
stats = ( Stats ) binaryFormatter.Deserialize ( fileStream );
fileStream.Close ( );
}
}
catch ( Exception e )
{
PlatformSafeMessage ( "Failed to Load: " + e.Message );
}
return stats;
}
private static void PlatformSafeMessage ( string message )
{
if ( Application.platform == RuntimePlatform.WebGLPlayer )
{
WindowAlert ( message );
}
else
{
Debug.Log ( message );
}
}
}
有谁知道这是否可以做到,如果可以怎么做?
【问题讨论】:
-
当您说 webGL 与 Desktop 相比时,您的意思是它可以在独立构建中运行吗?在编辑器中?在桌面浏览器上的 webGL 中?我还会附上您当前的保存代码,以帮助澄清您在做什么。
-
另外你的目标是什么? mobile 不支持 Unity WebGL。
-
对于客户端,我使用 Babylon JS 和 Unity 作为移动浏览器游戏的工具包框架:doc.babylonjs.com/extensions/Unity/Intro
-
@TEEBQNE,WebGl 游戏已构建并上传到服务器。当我们在计算机上的网络浏览器上玩游戏时,它可以完美运行。数据保存在本地没有麻烦。在移动平台上的网络浏览器上玩同一个游戏的那一刻,游戏会玩,但没有数据保存在本地。我上面的问题是在移动浏览器上播放时如何在本地保存数据。
标签: unity3d save unity-webgl