Skip to content

Commit

Permalink
feat: Add methods to start and end batching mode & update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
SolarianZ committed Oct 20, 2024
1 parent a53a3e7 commit 9c2783d
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Editor/Batching.cs → Editor/BatchingScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

namespace GBG.EditorUserSettings.Editor
{
public struct Batching : IDisposable
public struct BatchingScope : IDisposable
{
private IEditorUserSettingsStorage _storage;

internal Batching(IEditorUserSettingsStorage storage)
internal BatchingScope(IEditorUserSettingsStorage storage)
{
_storage = storage;
Assert.IsTrue(_storage.BatchingCounter > 0);
Expand Down
File renamed without changes.
29 changes: 28 additions & 1 deletion Editor/CustomEditorUserSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static IEditorUserSettingsStorage GetStorage(bool isSharedAcrossProjects

private static void TryDestroy(this IEditorUserSettingsStorage storage, bool isSharedAcrossProjects)
{
if (isSharedAcrossProjects && storage.BatchingCounter == 0)
if (isSharedAcrossProjects && storage != null && storage.BatchingCounter == 0)
{
storage.Destroy();
}
Expand Down Expand Up @@ -165,5 +165,32 @@ public static void Clear(bool isSharedAcrossProjects = false)
storage.Clear();
storage.TryDestroy(isSharedAcrossProjects);
}


/// <summary>
/// 开始批量编辑模式。
/// 批量编辑模式下,修改配置时不会立即将配置写入文件,而是等到批量编辑模式结束时才写入。
/// </summary>
/// <param name="isSharedAcrossProjects">配置是否在项目间共享。若为true,则在<see cref="UnityEditorInternal.InternalEditorUtility.unityPreferencesFolder"/>/UserSettings文件夹中查找;否则在项目<see cref="Application.dataPath"/>/../UserSettings文件夹下查找。</param>
/// <returns>批处理区域对象。可配合using语句块使用。</returns>
public static BatchingScope StartBatching(bool isSharedAcrossProjects = false)
{
IEditorUserSettingsStorage storage = GetStorage(isSharedAcrossProjects);
BatchingScope scope = storage.StartBatching();
storage.TryDestroy(isSharedAcrossProjects);

return scope;
}

/// <summary>
/// 结束批量编辑模式。
/// </summary>
/// <param name="isSharedAcrossProjects">配置是否在项目间共享。若为true,则在<see cref="UnityEditorInternal.InternalEditorUtility.unityPreferencesFolder"/>/UserSettings文件夹中查找;否则在项目<see cref="Application.dataPath"/>/../UserSettings文件夹下查找。</param>
public static void EndBatching(bool isSharedAcrossProjects = false)
{
IEditorUserSettingsStorage storage = GetStorage(isSharedAcrossProjects);
storage.EndBatching();
storage.TryDestroy(isSharedAcrossProjects);
}
}
}
4 changes: 2 additions & 2 deletions Editor/EditorUserSettingsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ public void Clear()
}


public Batching StartBatching()
public BatchingScope StartBatching()
{
BatchingCounter++;
return new Batching(this);
return new BatchingScope(this);
}

public void EndBatching()
Expand Down
2 changes: 1 addition & 1 deletion Editor/IEditorUserSettingsStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ internal interface IEditorUserSettingsStorage
{
uint BatchingCounter { get; }

Batching StartBatching();
BatchingScope StartBatching();
void EndBatching();


Expand Down
50 changes: 40 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
# EditorUserSettings

TODO
存储用于Unity Editor环境的自定义配置数据,可以控制是否在项目间共享这些配置数据。<br/>Store custom settings data for Unity Editor environment, and control whether to share these settings data between projects.


## Features
## 支持的Unity版本<br/>Supported Unity Versions

TODO
Unity 2019.4及更新版本。<br/>Unity 2019.4 or higher.


## Supported Unity Versions
## 安装方式<br/>Installation

Unity 2021.3 or higher.
[![openupm](https://img.shields.io/npm/v/com.greenbamboogames.editorusersettings?label=openupm&registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.greenbamboogames.editorusersettings/)

使用 [OpenUPM](https://openupm.com/packages/com.greenbamboogames.editorusersettings) 安装此包,或者直接克隆此仓库到项目中。<br/>Install this package via [OpenUPM](https://openupm.com/packages/com.greenbamboogames.editorusersettings), or clone this repository directly into the Packages folder of your project.

## Installation

[![openupm](https://img.shields.io/npm/v/com.greenbamboogames.editorusersettings?label=openupm&registry_uri=https://package.openupm.com)](https://openupm.com/packages/com.greenbamboogames.editorusersettings/)
## 如何使用<br/>How to Use

### API

```csharp
static class CustomEditorUserSettings
{
bool Has<T>(string key, bool isSharedAcrossProjects = false);

T Get<T>(string key, T defaultValue, bool isSharedAcrossProjects = false);
bool TryGet<T>(string key, out T value);

void Set<T>(string key, T value, bool isSharedAcrossProjects = false);

bool Remove<T>(string key, bool isSharedAcrossProjects = false);
bool RemoveAll<T>(bool isSharedAcrossProjects = false);

void Clear(bool isSharedAcrossProjects = false);

BatchingScope StartBatching(bool isSharedAcrossProjects = false);
void EndBatching(bool isSharedAcrossProjects = false);
}
```


### 菜单项<br/>Menu Items

Install this package via [OpenUPM](https://openupm.com/packages/com.greenbamboogames.editorusersettings), or clone this repository directly into the Packages folder of your project.
- Tools/Bamboo/Editor User Settings
- 检视项目配置存储对象:Inspect Storage Object for Project
- 在文件夹中显示项目配置存储对象:Show Storage Object for Project in Folder
- 检视跨项目配置存储对象:Inspect Storage Object Shared Across Projects
- 在文件夹中显示跨项目配置存储对象:Show Storage Object Shared Across Projects in Folder


## How to Use
## 已知问题<br/>Known Issues

TODO
1. 读写跨项目配置数据时,没有添加文件锁机制,同时读写时,可能会导致数据丢失。<br/>When reading and writing data across projects, there is no file lock mechanism added, and data may be lost when reading and writing at the same time.
2. 读写跨项目配置数据时,低版本Unity可能无法读取某些高版本Unity存储的数据。<br/> When reading and writing data across projects, lower versions of Unity may not be able to read some data stored by higher versions of Unity.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "com.greenbamboogames.editorusersettings",
"version": "1.0.0-preview",
"version": "1.0.0",
"displayName": "Editor User Settings!",
"description": "Store user settings in Unity Editor.",
"unity": "2022.3",
"unity": "2019.4",
"documentationUrl": "https://github.com/SolarianZ/EditorUserSettings",
"changelogUrl": "https://github.com/SolarianZ/EditorUserSettings/releases",
"licensesUrl": "https://github.com/SolarianZ/EditorUserSettings/blob/main/LICENSE",
Expand Down

0 comments on commit 9c2783d

Please sign in to comment.