docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Use case: Modify the accepted values of field definitions in an organization

    You can use the Unity Cloud Assets package to add and remove the accepted values of field definitions of type Selection.

    Organization or Asset Manager Project role Update accepted values
    Asset Management Viewer no
    Asset Management Consumer no
    Asset Management Contributor no
    Organization Owner yes

    Before you start

    Before you start, you must:

    1. Set up a Unity scene in the Unity Editor with an Organization and Project browser. See Get started with Assets for more information.

    2. Have some assets in the cloud. There are several ways to do so:

      • You can create assets through the Get started with Assets.
      • You can create assets through the dashboard; see the Managing assets on the dashboard documentation.

    How do I...?

    List and select the field definitions in an organization

    To list the existing field definitions in an Organization, follow these steps:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public List<ISelectionFieldDefinition> FieldDefinitions { get; set; }
    public ISelectionFieldDefinition CurrentFieldDefinition { get; private set; }
    
    public bool IsLocked { get; private set; }
    
    public async Task GetFieldDefinitionsAsync()
    {
        FieldDefinitions = new List<ISelectionFieldDefinition>();
        CurrentFieldDefinition = null;
    
        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)
        {
            if (fieldDefinition.Type != FieldDefinitionType.Selection) continue;
    
            FieldDefinitions.Add(fieldDefinition.AsSelectionFieldDefinition());
        }
    }
    
    public void SetCurrentFieldDefinition(ISelectionFieldDefinition fieldDefinition)
    {
        CurrentFieldDefinition = fieldDefinition;
    }
    
    

    The code snippet does the following:

    • Populates a list of field definitions of type Selection 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:

    1. Open the AssetManagementBehaviour script you created.
    2. Add the following code to the end of the class:
    
    public async Task AddAcceptedValueAsync(params string[] values)
    {
        IsLocked = true;
        await CurrentFieldDefinition.AddSelectionValuesAsync(values, CancellationToken.None);
        Debug.Log("Added accepted values.");
        IsLocked = false;
    }
    
    public async Task RemoveAcceptedValuesAsync(params string[] values)
    {
        IsLocked = true;
        await CurrentFieldDefinition.RemoveSelectionValuesAsync(values, CancellationToken.None);
        Debug.Log("Removed accepted values.");
        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 an value from the selected field definition.

    Add the UI for listing and modifying field definitions

    To create UI for listing and managing field definitions, follow these steps:

    1. In your Unity Project window, go to Assets > Scripts.
    2. Select and hold the Assets/Scripts folder.
    3. Go to Create > C# Script. Name your script UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI.
    4. Open the UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI script you created 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() { }
    }
    
    
    1. In the same script, replace the OnGUI function 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.SetCurrentFieldDefinition(null);
            m_Behaviour.FieldDefinitions = null;
        }
    
        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.FieldDefinitions == null)
        {
            _ = m_Behaviour.GetFieldDefinitionsAsync();
        }
    
        GUILayout.EndVertical();
    
        GUILayout.BeginVertical();
    
        GUILayout.Label("Fields:");
        ListFieldDefinitions(m_Behaviour.FieldDefinitions?.ToArray() ?? Array.Empty<ISelectionFieldDefinition>());
    
        GUILayout.EndVertical();
    
        GUILayout.BeginVertical();
    
        if (m_Behaviour.CurrentFieldDefinition == null)
        {
            GUILayout.Label(" ! No field selected !");
        }
        else
        {
            DisplayFieldDefinition();
        }
    
        GUILayout.EndVertical();
    }
    
    void ListFieldDefinitions(IReadOnlyList<ISelectionFieldDefinition> fields)
    {
        if (fields.Count == 0)
        {
            GUILayout.Label(" ! No fields !");
        }
        else
        {
            m_FieldsScrollPosition = GUILayout.BeginScrollView(m_FieldsScrollPosition, GUILayout.MinWidth(Screen.width * 0.2f), GUILayout.Height(Screen.height * 0.8f));
    
            for (var i = 0; i < fields.Count; ++i)
            {
                GUILayout.BeginHorizontal();
    
                GUILayout.Label(fields[i].Descriptor.FieldKey);
    
                if (GUILayout.Button("Select", GUILayout.Width(60)))
                {
                    m_Behaviour.SetCurrentFieldDefinition(fields[i]);
                }
    
                GUILayout.EndHorizontal();
            }
    
            GUILayout.EndScrollView();
        }
    }
    
    string m_NewValue = string.Empty;
    
    void DisplayFieldDefinition()
    {
        GUI.enabled = !m_Behaviour.IsLocked;
    
        var acceptedValues = m_Behaviour.CurrentFieldDefinition.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;
    }
    
    
    1. Open the AssetManagementUI script you created and replace the contents of the Awake function 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. Each field definition has a UI button to select it.
    • When a field definition is selected, additional UI elements list the field definition's accepted values.
    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)