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 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:

    1. Set up a Unity scene in the Unity Editor with an Organization and Project browser. Read more about setting up a Unity scene.

    2. Create assets in Unity Cloud any of the following ways:

      • Add assets using the Asset SDK.
      • Add single or multiple assets through the dashboard.

    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 that you created as described in Get started with Asset SDK.
    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:

    • Fills a list of field definitions of the Selection type 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 that you created as described in Get started with Asset SDK.
    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 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:

    1. In your Unity Project window, go to Assets > Scripts.
    2. Select and hold the Assets/Scripts folder.
    3. Go to Create > C# Script.
    4. Name your script UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI.
    5. Open the UseCaseFieldDefinitionsModifyAcceptedValuesExampleUI script 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() { }
    }
    
    
    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 that you created as described in Get started with Asset SDK 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. 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.
    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)