Class AchievementDefinitionCreator
Writes, copies, edits and removes one authored achievement — the four things the Achievements Studio's New, Duplicate, Delete and detail pane do, with every UnityEditor.AssetDatabase call on this side of the seam so the window stays pure presentation and the whole flow is testable without IMGUI.
Inherited Members
Namespace: Serenity.Achievements.Infrastructure.Editor.Builder
Assembly: Serenity.UnityAchievements.Infrastructure.Editor.dll
Syntax
public static class AchievementDefinitionCreator
Remarks
Sibling of AchievementSettingsCreator and deliberately shaped like it — static class, Input/Result DTOs, ValidateIdentity(string, string)/Validate(Input), a Serenity.Global.Infrastructure.Editor.SerenityAssetCreationTransaction whose every mutation is rolled back on failure. What it authors is different in kind, though: that creator configures the module once, this one writes the hundreds of by-Id assets the module then discovers by the ACHIEVEMENTS label.
Create creates; Update edits. An occupied target path is an error, never an overwrite, and so is another achievement in the same folder already answering to this Id — the caller that meant to edit that asset calls Update(UnityAchievementDefinition, Input) with it. The split is what makes a failed Create(Input) always safe to roll back by deletion: nothing it touches was in the project beforehand.
Update does not rename the asset file. Changing an achievement's Id in place leaves it in the file it was authored in, exactly as editing the Id field in the inspector does. Renaming an asset a scene, a recipe or another definition may reference is a separate decision, and one this engine refuses to make silently on the author's behalf.
Fields
DefaultEditUndoName
Undo entry name Update(UnityAchievementDefinition, Input) and BeginEdit(UnityAchievementDefinition, string) register under.
Declaration
public const string DefaultEditUndoName = "Update Achievement"
Field Value
| Type | Description |
|---|---|
| string |
DefaultTargetFolder
Folder a brand-new achievement lands in when the caller names none.
Declaration
public const string DefaultTargetFolder = "Assets/Game/Media/Achievements"
Field Value
| Type | Description |
|---|---|
| string |
Methods
BeginEdit(UnityAchievementDefinition, string)
Puts an achievement's current state on the undo stack, before a caller edits it directly.
Declaration
public static void BeginEdit(UnityAchievementDefinition definition, string undoName = "Update Achievement")
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAchievementDefinition | definition | The achievement about to be edited. Null is ignored. |
| string | undoName | The label the undo entry carries in the Edit menu. |
Remarks
Update(UnityAchievementDefinition, Input) is the whole story for a caller that hands over an AchievementDefinitionCreator.Input: it calls this itself. This is the entry point for the caller that does NOT — a detail pane editing the live condition tree through a managed-reference drawer, which mutates the very objects an AchievementDefinitionCreator.Input only points at. Registration has to happen BEFORE the first mutation, because the undo system stores what it can see at the moment it is called; registering afterwards snapshots the edit and undoes nothing.
UnityEditor.Undo.RegisterCompleteObjectUndo(UnityEngine.Object,System.String) rather than UnityEditor.Undo.RecordObject(UnityEngine.Object,System.String): the
condition tree is stored through [SerializeReference], and a diff-based record is not a
dependable way to capture a managed-reference graph. Recording the whole object costs one snapshot of
a small asset and makes the undo cover the tree as well as the fields.
Create(Input)
Validates the request and writes one achievement asset, rolling back on any failure.
Declaration
public static AchievementDefinitionCreator.Result Create(AchievementDefinitionCreator.Input input)
Parameters
| Type | Name | Description |
|---|---|---|
| AchievementDefinitionCreator.Input | input | What to author. |
Returns
| Type | Description |
|---|---|
| AchievementDefinitionCreator.Result | The outcome, never null; check Serenity.Global.Infrastructure.Editor.SerenityCreationResult.Success. |
Delete(UnityAchievementDefinition)
Removes an achievement from the project, recoverably.
Declaration
public static AchievementDefinitionCreator.DeleteResult Delete(UnityAchievementDefinition definition)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAchievementDefinition | definition | The achievement to remove. Must not be null. |
Returns
| Type | Description |
|---|---|
| AchievementDefinitionCreator.DeleteResult | The outcome, never null; check Serenity.Global.Infrastructure.Editor.SerenityCreationResult.Success and AwaitingSafeDeleteConfirmation. |
Remarks
UnityEditor.AssetDatabase.MoveAssetToTrash(System.String) rather than DeleteAsset, so an achievement removed
by a single toolbar click is still in the OS trash afterwards. The deletion goes through the normal
safe-delete hook on purpose — an achievement a settings asset, a view or a recipe still points at must
produce the referencer warning a Project-window delete would produce, and suppressing that here would
make the Studio the one place in the project where a reference can be broken silently.
Duplicate(UnityAchievementDefinition, string, string, bool)
Copies an existing achievement into a new one, condition tree included.
Declaration
public static AchievementDefinitionCreator.Result Duplicate(UnityAchievementDefinition source, string newId, string targetFolder = null, bool registerAsAddressables = true)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAchievementDefinition | source | The achievement to copy. Must not be null. |
| string | newId | The copy's Id. Must differ from every achievement already in the target folder. |
| string | targetFolder | Where the copy lands. Blank keeps it beside |
| bool | registerAsAddressables | Whether the copy is registered under the achievements label. |
Returns
| Type | Description |
|---|---|
| AchievementDefinitionCreator.Result | The outcome, never null; check Serenity.Global.Infrastructure.Editor.SerenityCreationResult.Success. |
FolderOf(Object)
The project folder an achievement asset lives in, in the forward-slash form Unity expects.
Declaration
public static string FolderOf(Object asset)
Parameters
| Type | Name | Description |
|---|---|---|
| Object | asset | The asset to locate. May be null. |
Returns
| Type | Description |
|---|---|
| string | Its folder, or DefaultTargetFolder when it has none. |
Remarks
Never answers with nothing. An object that is not an asset — a CreateInstance that was never
written, or a destroyed one — has no folder, so DefaultTargetFolder stands in: that is
where Create(Input) would put it, which makes this the same answer a window can show the
author before they commit. Public because a Studio asks the same question about the same assets, and
two copies of this would eventually disagree about the blank case.
Update(UnityAchievementDefinition, Input)
Applies input to an achievement that already exists, in place and undoably.
Declaration
public static AchievementDefinitionCreator.Result Update(UnityAchievementDefinition definition, AchievementDefinitionCreator.Input input)
Parameters
| Type | Name | Description |
|---|---|---|
| UnityAchievementDefinition | definition | The achievement to edit. Must not be null. |
| AchievementDefinitionCreator.Input | input | What it should become. |
Returns
| Type | Description |
|---|---|
| AchievementDefinitionCreator.Result | The outcome, never null; check Serenity.Global.Infrastructure.Editor.SerenityCreationResult.Success. |
Remarks
The asset keeps its Guid and its file name; see the type doc for why an Id change does not rename it. The undo entry is registered through BeginEdit(UnityAchievementDefinition, string), so this path and a pane editing the tree by hand land on the undo stack the same way.
Validate(Input)
Full validation of an achievement about to be created.
Declaration
public static SerenityCreationValidationResult Validate(AchievementDefinitionCreator.Input input)
Parameters
| Type | Name | Description |
|---|---|---|
| AchievementDefinitionCreator.Input | input | What would be authored. |
Returns
| Type | Description |
|---|---|
| SerenityCreationValidationResult | The findings; see AchievementDefinitionValidator for the rules. |
Validate(Input, UnityAchievementDefinition)
Full validation of an edit applied to an achievement that already exists.
Declaration
public static SerenityCreationValidationResult Validate(AchievementDefinitionCreator.Input input, UnityAchievementDefinition assetBeingEdited)
Parameters
| Type | Name | Description |
|---|---|---|
| AchievementDefinitionCreator.Input | input | What it would become. |
| UnityAchievementDefinition | assetBeingEdited | The achievement being edited, exempt from the rules that would otherwise report it as its own duplicate. |
Returns
| Type | Description |
|---|---|
| SerenityCreationValidationResult | The findings; see AchievementDefinitionValidator for the rules. |
Validate(Input, UnityAchievementDefinition, IReadOnlyCollection<AchievementPlatformKind>)
Full validation of an edit, judged against storefronts the caller already holds.
Declaration
public static SerenityCreationValidationResult Validate(AchievementDefinitionCreator.Input input, UnityAchievementDefinition assetBeingEdited, IReadOnlyCollection<AchievementPlatformKind> enabledPlatforms)
Parameters
| Type | Name | Description |
|---|---|---|
| AchievementDefinitionCreator.Input | input | What would be authored. |
| UnityAchievementDefinition | assetBeingEdited | The achievement being edited, or null when validating a creation. |
| IReadOnlyCollection<AchievementPlatformKind> | enabledPlatforms | The storefronts the project ships on. Empty or null silences the platform rule. |
Returns
| Type | Description |
|---|---|
| SerenityCreationValidationResult | The findings; see AchievementDefinitionValidator for the rules. |
Remarks
The three-argument form exists partly because Validate(input, null) cannot compile: a bare
null fits both two-argument overloads and the compiler refuses to guess. Name the achievement
being edited (or null typed as one) here and the ambiguity disappears.
Validate(Input, IReadOnlyCollection<AchievementPlatformKind>)
Full validation of a creation, judged against storefronts the caller already holds.
Declaration
public static SerenityCreationValidationResult Validate(AchievementDefinitionCreator.Input input, IReadOnlyCollection<AchievementPlatformKind> enabledPlatforms)
Parameters
| Type | Name | Description |
|---|---|---|
| AchievementDefinitionCreator.Input | input | What would be authored. |
| IReadOnlyCollection<AchievementPlatformKind> | enabledPlatforms | The storefronts the project ships on. Empty or null silences the platform rule. |
Returns
| Type | Description |
|---|---|
| SerenityCreationValidationResult | The findings; see AchievementDefinitionValidator for the rules. |
Remarks
For the caller validating more than one achievement: reading which storefronts the project ships on walks every settings asset in it, and the answer cannot change while a window is open, so a Studio resolves it once with EnabledPlatformsInProject() and passes it here instead of paying for the scan per row.
ValidateIdentity(string, string)
Id-only validation, usable from a "New Achievement" prompt before anything else is filled in.
Declaration
public static SerenityCreationValidationResult ValidateIdentity(string achievementId, string targetFolder)
Parameters
| Type | Name | Description |
|---|---|---|
| string | achievementId | The Id being typed. |
| string | targetFolder | The folder the achievement would land in. |
Returns
| Type | Description |
|---|---|
| SerenityCreationValidationResult | The findings; see AchievementDefinitionValidator for the rules. |