Beispiel #1
0
    /// <summary>
    /// Create a textual hint to show for the given action based on the devices we are currently using.
    /// </summary>
    /// <param name="action">Action to generate a hint for.</param>
    /// <param name="format">Format string. Use {0} where the active control name should be inserted.</param>
    /// <param name="devices">Set of currently assigned devices. The action will be searched for a bound control that sits
    /// on one of the devices. If none is found, an empty string is returned.</param>
    /// <returns>Text containing a hint for the given action or an empty string.</returns>
    private static string GetOrCreateUIHint(InputAction action, string format, ReadOnlyArray <InputDevice> devices)
    {
        InputControl control = null;

        // Find the first bound control that sits on one of the given devices.
        var controls = action.controls;

        foreach (var element in controls)
        {
            if (devices.ContainsReference(element.device))
            {
                control = element;
                break;
            }
        }

        if (control == null)
        {
            return(string.Empty);
        }

        // See if we have an existing hint.
        if (s_CachedUIHints != null)
        {
            foreach (var hint in s_CachedUIHints)
            {
                if (hint.action == action && hint.control == control && hint.format == format)
                {
                    return(hint.text);
                }
            }
        }

        // No, so create a new hint and cache it.
        var controlName = control.shortDisplayName;

        if (string.IsNullOrEmpty(controlName))
        {
            controlName = control.displayName;
        }
        var text = string.Format(format, controlName);

        if (s_CachedUIHints == null)
        {
            s_CachedUIHints = new List <CachedUIHint>();
        }
        s_CachedUIHints.Add(new CachedUIHint {
            action = action, control = control, format = format, text = text
        });

        return(text);
    }