Skip to content

Game.UI.Editor.EditorPrefabUtils

Assembly: Game (inferred)
Namespace: Game.UI.Editor

Type: static class

Base: none

Summary:
Utility helpers used by the in-game editor for working with PrefabBase objects and associated assets (icons, localization, asset IDs, type/tags). Provides methods to query prefab IDs, construct type names and tags for prefabs, save user-prefabs, enumerate locale assets, enumerate icon references used by a prefab, produce display labels for prefabs and enumerate user image assets. Includes small caching for prefab type/tag lookups. Note: two GetPrefabByID overloads are present but are implemented as stubs (return null) in this file.


Fields

  • public struct IconInfo
    Holds information about an icon referenced from a prefab component:
  • m_Asset: ImageAsset instance (the loaded image asset)
  • m_URI: string URI used in the prefab field
  • m_Field: FieldInfo for the field that stores the URI
  • m_Component: ComponentBase instance owning the field

  • private static readonly Dictionary<Type, string[]> s_PrefabTypes
    Cache mapping a Type to an array of fully-qualified prefab type names (from the type up the inheritance chain until PrefabBase). Used by GetPrefabTypes to avoid recomputing the type list repeatedly.

  • private static readonly Dictionary<Type, string[]> s_PrefabTags
    Cache mapping a Type to an array of prefab tags (short names derived from the type names, trimmed of a "Prefab" suffix and lowercased). Used by GetPrefabTags.

  • public static readonly LocalizedString kNone
    LocalizedString constant for representing a "None" value in editor UI. Initialized from LocalizedString.Id("Editor.NONE_VALUE").

Properties

  • None (no public properties on the static class)

Constructors

  • (static) none — class is static, no instance constructor.

Methods

  • public static string GetPrefabTypeName(Type type)
    Returns the fully-qualified name (Type.FullName) of the provided Type. Used when building prefab type lists.

  • [CanBeNull] public static PrefabBase GetPrefabByID([CanBeNull] string prefabID)
    Intended to return a PrefabBase given a prefab GUID/ID string. In this file the method is a stub and always returns null. Caller should handle null.

  • [CanBeNull] public static T GetPrefabByID<T>([CanBeNull] string prefabID) where T : PrefabBase
    Generic overload intended to return a prefab of type T by ID. Also a stub here and returns null. Use only if implemented in your runtime or replace with custom lookup logic.

  • [CanBeNull] public static string GetPrefabID(PrefabBase prefab)
    If prefab is non-null and AssetDatabase.global.resources.prefabsMap.TryGetGuid(prefab, out id) succeeds, returns the guid string for the prefab; otherwise returns null. Useful to get a persistent ID for saving or referencing the prefab.

  • public static string[] GetPrefabTypes(Type type)
    Returns a list of fully-qualified type names for the provided type and its base types up to (but excluding) PrefabBase. Uses s_PrefabTypes cache. Example output for a subclass hierarchy: ["Game.Prefabs.BuildingPrefab", "Game.Prefabs.PrefabBaseSubType", ...] depending on types.

  • public static string[] GetPrefabTags(Type type)
    Returns a list of tag strings built from the type names walking up to PrefabBase. For each type name: if it ends with "Prefab", that suffix is removed, then the result is lowercased. Uses s_PrefabTags cache. Example: BuildingPrefab -> "building".

  • public static void SavePrefab(PrefabBase prefab)
    Saves the prefab to the user asset database if it does not already have an asset. Implementation details:

  • If prefab.asset is null, a new asset is created/added to AssetDatabase.user via AssetDatabase.user.AddAsset(AssetDataPath.Create("StreamingData~/" + prefab.name, prefab.name ?? ""), prefab)
  • Calls Save() on the resulting asset. Useful for persisting changes to prefabs from editor UI.

  • public static IEnumerable<LocaleAsset> GetLocaleAssets(PrefabBase prefab)
    Yields LocaleAsset instances that match the prefab asset's subPath, but only if prefab.asset is non-null and prefab.asset.database != AssetDatabase.game. Uses AssetDatabase.global.GetAssets with a SearchFilter to match LocaleAsset.subPath == prefab.asset.subPath.

  • public static IEnumerable<IconInfo> GetIcons(PrefabBase prefab)
    Scans all ComponentBase instances on the prefab. For each component, it inspects public fields (FieldInfo) and selects fields of type string that have a CustomFieldAttribute whose Factory == typeof(UIIconField). For each such field, it reads the string value (URI) and attempts to resolve it to an ImageAsset using UIExtensions.TryGetImageAsset. On success yields an IconInfo with the resolved ImageAsset, the URI string, the FieldInfo and the ComponentBase. Use to discover image/icon usage in prefabs and to present or replace icons in editor.

  • public static LocalizedString GetPrefabLabel(PrefabBase prefab)
    Produces a display label appropriate for editor lists:

  • If prefab is null: returns kNone.
  • If prefab.asset != null:
    • If asset.database == AssetDatabase.instance: includes platformID from SourceMeta as "name - (platformID)"
    • If asset.database == AssetDatabase.user: if meta.packaged true uses meta.packageName, otherwise uses prefab.asset.name -> "name - (packagename or asset name)"
  • Otherwise returns the prefab.name wrapped in LocalizedString.Value. Useful to show a friendly, informative label in lists.

  • public static IEnumerable<AssetItem> GetUserImages()
    Yields AssetItem entries for user images (screenshots). The sequence:

  • First yields a default "none" AssetItem with empty guid and displayName == kNone.
  • Then enumerates ImageAsset instances in AssetDatabase.global whose meta.subPath starts with ScreenUtility.kScreenshotDirectory. For each image asset yields an AssetItem with guid, fileName, displayName and image (asset.ToUri()). Note: each ImageAsset is disposed via using(asset) during iteration.

Notes, caveats and implementation details: - The class relies heavily on the game's AssetDatabase, AssetItem/ImageAsset/LocaleAsset and PrefabBase APIs. - s_PrefabTypes and s_PrefabTags caches are static but not synchronized. If called concurrently from different threads, consider adding locking if your mod uses background threads. - Two GetPrefabByID methods are annotated with [CanBeNull] but are implemented to return null. They must be implemented or replaced depending on game/runtime version; callers must handle nulls. - GetIcons uses reflection and CustomFieldAttribute + UIIconField factory checks to find icon references; this follows the game's custom-field-driven UI architecture.

Usage Example

// Example: enumerate icons on a prefab and save the prefab if modified
PrefabBase prefab = /* obtain prefab somehow, e.g. from selection */;
if (prefab != null)
{
    // Show label for UI
    LocalizedString label = EditorPrefabUtils.GetPrefabLabel(prefab);
    Debug.Log(label.Value);

    // Enumerate icon references
    foreach (var iconInfo in EditorPrefabUtils.GetIcons(prefab))
    {
        Debug.Log($"Icon URI: {iconInfo.m_URI}, asset id: {iconInfo.m_Asset?.id}");
        // You could replace iconInfo.m_Field on iconInfo.m_Component, then call SavePrefab
    }

    // Save prefab to user assets (adds asset if missing)
    EditorPrefabUtils.SavePrefab(prefab);
}