Use case: Modify the accepted values of field definitions in an organization
You can use the Unity Cloud Assets package to add and remove accepted values for field definitions of the Selection type.
Note
To update accepted values of field definitions in an organization, you need the Asset Manager Admin role at the organization level.
Before you start
Before you start, do the following:
Set up a Unity scene in the Unity Editor with an Organization and Project browser. Read more about setting up a Unity scene.
Create assets in Unity Cloud any of the following ways:
How do I...?
List and select the field definitions in an organization
To list the existing field definitions in an organization, follow these steps:
- Open the
AssetManagementBehaviourscript that you created as described in Get started with Asset SDK. - Add the following code to the end of the class:
public Dictionary<string, SelectionFieldDefinitionProperties> FieldDefinitionsProperties { get; } = new();
public string CurrentFieldDefinitionKey { get; set; }
public bool IsLocked { get; private set; }
public async Task GetFieldDefinitionsAsync()
{
var key = CurrentFieldDefinitionKey;
CurrentFieldDefinitionKey = null;
FieldDefinitionsProperties.Clear();
var searchFilter = new FieldDefinitionSearchFilter();
searchFilter.Deleted.WhereEquals(false);
var asyncList = PlatformServices.AssetRepository.QueryFieldDefinitions(CurrentOrganization.Id)
.SelectWhereMatchesFilter(searchFilter)
.ExecuteAsync(CancellationToken.None);
await foreach (var fieldDefinition in asyncList)
{
var properties = await fieldDefinition.GetPropertiesAsync(CancellationToken.None);
if (properties.Type != FieldDefinitionType.Selection) continue;
FieldDefinitionsProperties.Add(fieldDefinition.Descriptor.FieldKey,
await fieldDefinition.AsSelectionFieldDefinition().GetPropertiesAsync(CancellationToken.None));
if (fieldDefinition.Descriptor.FieldKey == key)
{
CurrentFieldDefinitionKey = key;
}
}
}
The code snippet does the following:
- Fills a list of field definitions of the
Selectiontype for the selected organization. - Holds a reference to the selected field.
Modify the accepted values of a field definition
To modify the accepted values of a field definition, follow these steps:
- Open the
AssetManagementBehaviourscript that you created as described in Get started with Asset SDK. - Add the following code to the end of the class:
public async Task AddAcceptedValueAsync(params string[] values)
{
IsLocked = true;
var fieldDefinition = await PlatformServices.AssetRepository.GetFieldDefinitionAsync(new FieldDefinitionDescriptor(CurrentOrganization.Id, CurrentFieldDefinitionKey), CancellationToken.None);
var selectionFieldDefinition = fieldDefinition.AsSelectionFieldDefinition();
await selectionFieldDefinition.AddSelectionValuesAsync(values, CancellationToken.None);
Debug.Log("Added accepted values.");
await selectionFieldDefinition.RefreshAsync(CancellationToken.None);
var properties = await selectionFieldDefinition.GetPropertiesAsync(CancellationToken.None);
FieldDefinitionsProperties[CurrentFieldDefinitionKey] = properties;
IsLocked = false;
}
public async Task RemoveAcceptedValuesAsync(params string[] values)
{
IsLocked = true;
var fieldDefinition = await PlatformServices.AssetRepository.GetFieldDefinitionAsync(new FieldDefinitionDescriptor(CurrentOrganization.Id, CurrentFieldDefinitionKey), CancellationToken.None);
var selectionFieldDefinition = fieldDefinition.AsSelectionFieldDefinition();
await selectionFieldDefinition.RemoveSelectionValuesAsync(values, CancellationToken.None);
Debug.Log("Removed accepted values.");
await selectionFieldDefinition.RefreshAsync(CancellationToken.None);
var properties = await selectionFieldDefinition.GetPropertiesAsync(CancellationToken.None);
FieldDefinitionsProperties[CurrentFieldDefinitionKey] = properties;
IsLocked = false;
}
The code snippet does the following:
- Exposes a method to add a new value to the selected field definition.
- Exposes a method to remove a value from the selected field definition.
Add a UI for listing and modifying field definitions
To create a UI for listing and managing field definitions, follow these steps:
- In your Unity Project window, go to Assets > Scripts.
- Select and hold the
Assets/Scriptsfolder. - Go to Create > C# Script.
- Name your script
UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI. - Open the
UseCaseFieldDefinitionsModifyAcceptedValuesExampleUIscript that you created in the previous step and replace the contents of the file with the following code sample:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Unity.Cloud.Assets;
using Unity.Cloud.Identity;
using UnityEngine;
public class UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI : IAssetManagementUI
{
readonly AssetManagementBehaviour m_Behaviour;
public UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI(AssetManagementBehaviour behaviour)
{
m_Behaviour = behaviour;
}
public void OnGUI() { }
}
- In the same script, replace the
OnGUIfunction with the following code:
IOrganization m_CurrentOrganization;
Vector2 m_FieldsScrollPosition;
public void OnGUI()
{
if (!m_Behaviour.IsOrganizationSelected) return;
if (m_CurrentOrganization != m_Behaviour.CurrentOrganization)
{
m_CurrentOrganization = m_Behaviour.CurrentOrganization;
_ = m_Behaviour.GetFieldDefinitionsAsync();
}
GUILayout.BeginVertical();
// Go back to select a different scene.
if (GUILayout.Button("Back"))
{
m_Behaviour.SetSelectedOrganization(null);
return;
}
if (GUILayout.Button("Refresh", GUILayout.Width(60)))
{
_ = m_Behaviour.GetFieldDefinitionsAsync();
}
GUILayout.EndVertical();
GUILayout.BeginVertical();
GUILayout.Label("Fields:");
ListFieldDefinitions();
GUILayout.EndVertical();
if (string.IsNullOrEmpty(m_Behaviour.CurrentFieldDefinitionKey))
{
GUILayout.Label(" ! No field selected !");
return;
}
GUILayout.BeginVertical();
DisplayFieldDefinition();
GUILayout.EndVertical();
}
void ListFieldDefinitions()
{
if (m_Behaviour.FieldDefinitionsProperties.Count == 0)
{
GUILayout.Label(" ! No fields !");
return;
}
m_FieldsScrollPosition = GUILayout.BeginScrollView(m_FieldsScrollPosition, GUILayout.MinWidth(Screen.width * 0.2f), GUILayout.Height(Screen.height * 0.8f));
foreach (var field in m_Behaviour.FieldDefinitionsProperties)
{
GUILayout.BeginHorizontal();
GUILayout.Label(field.Key);
GUI.enabled = field.Key != m_Behaviour.CurrentFieldDefinitionKey;
if (GUILayout.Button("Select", GUILayout.Width(60)))
{
m_Behaviour.CurrentFieldDefinitionKey = field.Key;
}
GUI.enabled = true;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
}
string m_NewValue = string.Empty;
void DisplayFieldDefinition()
{
if (!m_Behaviour.FieldDefinitionsProperties.TryGetValue(m_Behaviour.CurrentFieldDefinitionKey, out var properties))
{
GUILayout.Label(" ! Field definition properties not loaded !");
return;
}
GUI.enabled = !m_Behaviour.IsLocked;
var acceptedValues = properties.AcceptedValues.ToArray();
foreach (var value in acceptedValues)
{
GUILayout.BeginHorizontal();
GUILayout.Label(value);
if (GUILayout.Button("Remove"))
{
_ = m_Behaviour.RemoveAcceptedValuesAsync(value);
}
GUILayout.EndHorizontal();
}
GUILayout.Space(15f);
GUILayout.BeginHorizontal();
m_NewValue = GUILayout.TextField(m_NewValue);
GUI.enabled = !string.IsNullOrEmpty(m_NewValue) && !acceptedValues.Contains(m_NewValue);
if (GUILayout.Button("Add"))
{
_ = m_Behaviour.AddAcceptedValueAsync(m_NewValue.Split(',').Select(x => x.Trim()).ToArray());
m_NewValue = string.Empty;
}
GUILayout.EndHorizontal();
GUI.enabled = true;
}
- Open the
AssetManagementUIscript that you created as described in Get started with Asset SDK and replace the contents of theAwakefunction with the following code:
m_UI.Add(new OrganizationSelectionExampleUI(m_Behaviour));
m_UI.Add(new UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI(m_Behaviour));
The code snippet does the following:
- Displays a list of the selected organization's field definitions. Next to each field definition displays a Select UI button.
- When you select a field definition, additional UI elements list the field definition's accepted values.