在Unity中用EditorPrefs写两个方法,保存和读取下面这些数据 public string savePath; public string serverPath; public BuildAssetBundleOptions bundleOptions; public BuildTarget buildTarget; public bool copyToServerPath;
时间: 2023-05-28 09:07:12 浏览: 96
以下是示例代码:
```csharp
public static class EditorPrefsHelper
{
private const string SAVE_PATH_KEY = "SavePath";
private const string SERVER_PATH_KEY = "ServerPath";
private const string BUNDLE_OPTIONS_KEY = "BundleOptions";
private const string BUILD_TARGET_KEY = "BuildTarget";
private const string COPY_TO_SERVER_PATH_KEY = "CopyToServerPath";
public static void SaveData(string savePath, string serverPath, BuildAssetBundleOptions bundleOptions, BuildTarget buildTarget, bool copyToServerPath)
{
EditorPrefs.SetString(SAVE_PATH_KEY, savePath);
EditorPrefs.SetString(SERVER_PATH_KEY, serverPath);
EditorPrefs.SetInt(BUNDLE_OPTIONS_KEY, (int)bundleOptions);
EditorPrefs.SetInt(BUILD_TARGET_KEY, (int)buildTarget);
EditorPrefs.SetInt(COPY_TO_SERVER_PATH_KEY, copyToServerPath ? 1 : 0);
}
public static void LoadData(out string savePath, out string serverPath, out BuildAssetBundleOptions bundleOptions, out BuildTarget buildTarget, out bool copyToServerPath)
{
savePath = EditorPrefs.GetString(SAVE_PATH_KEY, "");
serverPath = EditorPrefs.GetString(SERVER_PATH_KEY, "");
bundleOptions = (BuildAssetBundleOptions)EditorPrefs.GetInt(BUNDLE_OPTIONS_KEY, 0);
buildTarget = (BuildTarget)EditorPrefs.GetInt(BUILD_TARGET_KEY, 0);
copyToServerPath = EditorPrefs.GetInt(COPY_TO_SERVER_PATH_KEY, 0) == 1;
}
}
```
使用方法:
```csharp
// 保存数据
EditorPrefsHelper.SaveData(
"Assets/StreamingAssets",
"/var/www/html",
BuildAssetBundleOptions.None,
BuildTarget.StandaloneWindows64,
true
);
// 读取数据
string savePath, serverPath;
BuildAssetBundleOptions bundleOptions;
BuildTarget buildTarget;
bool copyToServerPath;
EditorPrefsHelper.LoadData(out savePath, out serverPath, out bundleOptions, out buildTarget, out copyToServerPath);
```
阅读全文