This package allows you to use the [Searchable] attribute that you can use the pop-up window and the search field to select the desired element.
To add this package to the project, follow these steps:
- Open PackageManager;
- Select "Add package from get URL";
- Insert links to packages that are dependencies of this package:
https://github.com/Paulsams/MiscUtilities.git
- Insert this link
https://github.com/Paulsams/SearchableAttribute.git
- Is using:
- MicsUtilities: https://github.com/Paulsams/MiscUtilities.git
- Works with enums without any additional configuration.
[SerializeField, Searchable] private KeyCode _keyCode;
- Allows you to write your custom converter to array string from which you can select and assign a value to a field via SerializeProperty.
More detailed:
when importing this package, you will need to create folders along the path: Utilities/SearchableAttribute
. The file "NamesConvertersToArrayString" (Runtime/Names Converters ToArray String.cs
) is needed in order not to hardcode class names when writing them as an argument to an attribute. The Editor/Custom Converters
folder can simply serve as a folder for creating your custom converters, but this does not prohibit you from creating them anywhere in the project. There is an example of a converter below. It is better to give the name of your castor converter with the suffix ToArrayString
, because on the side of my code it is this phrase that ends.
[SerializeField, Searchable(NamesConvertersToArrayString.ItemTypes)] private string _itemType;
using System.Linq;
using UnityEditor;
using Paulsams.MicsUtils;
using UnityEngine;
public class ItemTypesToArrayString : IConvertToArrayString
{
private readonly IConvertToArrayString.Element[] _items;
public ItemTypesToArrayString()
{
string pathToJson = $"{Application.dataPath}/Samples/SearchableAttribute/Example Enum and One Custom Converter/Runtime/ItemTypes/ItemsTypes.json";
if (JsonSerializerUtility.TryDeserialize(out ItemType[] itemTypes, pathToJson))
{
_items = itemTypes.Select((itemType) => new IConvertToArrayString.Element(itemType.Name, itemType.Category)).ToArray();
}
else
{
Debug.LogError($"The file with item types was not found on the path: {pathToJson}");
}
}
public IConvertToArrayString.Element[] Convert(SerializedProperty property) => _items;
public int GetIndex(SerializedProperty property) => System.Array.FindIndex(_items, (element) => element.Name == property.stringValue);
public void SetIndex(SerializedProperty property, int index) => property.stringValue = _items[index].Name;
}
SearchableAttribute(string converterTypeName = null)
To download the examples for this package:
- Select this package in PackageManager;
- Expand the "Samples" tab on the right;
- And click the "Import" button on the example you are interested in.