Game.UI.Editor.AssetImportPanel
Assembly:
Assembly-CSharp (Game)
Namespace:
Game.UI.Editor
Type:
public class
Base:
EditorPanelSystemBase
Summary:
AssetImportPanel is the editor UI panel used by Cities: Skylines 2 to import art assets from an external project directory into the game's user asset database. It provides UI controls to pick a project root and an import subfolder, presents a file list, allows selecting a prefab template to apply to imported meshes, and runs the import pipeline (AssetImportPipeline) to create prefabs and add them to the game's prefab system. The panel persists the last-used project and import directories via SharedSettings and uses PrefabFactory (nested) to collect created prefabs during import. Import is performed asynchronously and supports parallel import settings.
Fields
-
private readonly List<(PrefabBase prefab, string source)> m_RootPrefabs
Holds root prefabs collected by the nested PrefabFactory. Each entry pairs a created PrefabBase and the original source path. -
private readonly List<PrefabBase> m_CreatedPrefabs
List of all prefabs created by the nested PrefabFactory during an import session. -
private string m_SelectedProjectRoot
Full path to the selected project root directory (persisted via EditorSettings). -
private string m_SelectedDirectory
Full path to the currently selected import directory/subfolder (persisted via EditorSettings). -
private bool m_ProjectRootSelected
Flag indicating whether a valid project root has been selected in the UI. -
private DirectoryPickerButton m_OpenProjectRootButton
UI button widget that opens the project-root directory browser. -
private DirectoryPickerButton m_OpenSelectedAssetPathButton
UI button widget that opens the selected asset subdirectory browser. -
private List<FileItem> m_Assets
Current list of FileItem entries representing detected importable assets in the selected directory. -
private List<FileItem> m_CachedAssets
Reserved for caching asset list (present in class but not actively used in this file). -
private FilePickerAdapter m_Adapter
Adapter used by the item picker UI to display m_Assets. -
private ItemPicker<FileItem> m_AssetList
UI picker that shows available assets to import. -
private ItemPickerFooter m_ItemPickerFooter
Footer widget for the asset item picker. -
private PrefabBase m_SelectedTemplate
Optional prefab template chosen by the user to apply to imported meshes. -
private Button m_ImportButton
UI button that triggers the import process. -
private bool m_Importing
Backing field used by the private importing property to track whether an import is in progress.
Properties
-
private bool importing { get; set; }
Encapsulates m_Importing. Setting this updates the import button display name between "Editor.IMPORT" and "Editor.IMPORTING" and influences IsImportDisabled. Used to prevent reentrancy while an import runs. -
public IReadOnlyList<(PrefabBase prefab, string source)> rootPrefabs => m_RootPrefabs
(PrefabFactory)
Exposes root prefabs created for reference after import. -
public IReadOnlyList<PrefabBase> prefabs => m_CreatedPrefabs
(PrefabFactory)
Exposes all prefabs created by the factory during import.
Constructors
-
public AssetImportPanel()
Default constructor. The panel initialization work is performed in OnStartRunning. -
public PrefabFactory()
(nested class PrefabFactory)
Default constructor for the nested factory used by the import pipeline to create/load prefab assets.
Methods
-
[Preserve] protected override void OnStartRunning()
Initializes the panel UI when the system starts running. Builds child widgets (directory pickers, asset list, template popup, import button), wires adapter events, loads last used editor settings (lastSelectedProjectRootDirectory and lastSelectedImportDirectory) and auto-selects directories when possible. Catches and logs exceptions while restoring selections. -
private void OnAssetSelected(FileItem item)
Callback invoked when the user selects an asset item in the list. Currently logs the selection. -
private void OpenDirectory()
Opens a DirectoryBrowserPanel to pick the project root. -
private void OpenAssetSubDirectory()
Opens a DirectoryBrowserPanel to pick the import subdirectory. Ensures the selected directory starts with the selected project root. -
private void CloseDirectoryBrowser()
Closes the directory browser subpanel. -
private void OnLoadAsset(Guid guid)
Placeholder callback invoked when an asset is loaded; closes subpanel. (Implementation in this file only closes the subpanel.) -
private void OnSelectProjectRoot(string directory)
Handler when a project root is selected. Updates button labels, persists the selection to EditorSettings, clears and reinitializes the asset adapter, and marks a project root as selected. -
private void OnSelectDirectory(string directory)
Handler when an import directory is selected. Updates UI labels, persists setting, collects assets from the selected directory and reinitializes adapters. -
private IEnumerable<FileItem> GetAssets()
Enumerates importable assets from the currently selected directory using AssetImportPipeline. It ensures post-processors and importer extensions are cached, calls AssetImportPipeline.CollectDataToImport and yields FileItem entries (path, displayName, tooltip) for each SourceAsset found. -
private bool IsImportDisabled()
UI helper to determine whether the Import button should be disabled. Returns true while import is in progress or when there are no assets to import. -
private bool IsSelectedAssetFolderDisabled()
UI helper to determine whether the "selected assets" button should be disabled. Button is enabled only when a project root has been chosen and a project root path is non-empty. -
private static bool ReportProgress(string title, string info, float progress)
Progress callback used during import. Logs completion when progress == 1.0. Returns false (the pipeline expects a bool return; usage here returns false). -
private async void ImportAssets()
UI-triggered async method. Sets importing = true, waits for the static ImportAssets helper to complete and finally sets importing = false even on exceptions. Passes base.World and base.log to the import routine. -
public static async Task<PrefabFactory> ImportAssets(string selectedProjectRoot, string selectedDirectory, PrefabBase selectedTemplate, World world, ILog log)
Core static import routine. Validates selected paths, checks for art-root path via AssetImportPipeline.IsArtRootPath, creates a PrefabFactory, configures AssetImportPipeline settings (parallel import, target database, texture compression override), calls AssetImportPipeline.ImportPath to perform import, and then integrates created prefabs into the game's PrefabSystem and optionally activates the prefab tool (ToolSystem). If a template is provided, it clones the template and attaches the imported mesh as m_Meshes[0] (for ObjectGeometryPrefab), strips out several components (ObjectSubObjects, subareas, lanes, nets, effects, obsolete identifiers), and saves/ registers the prefab in the target database. Errors are logged via the provided ILog. Returns the PrefabFactory that collected created prefabs, or null on failure.
Nested PrefabFactory methods:
-
public T CreatePrefab<T>(string sourcePath, string rootMeshName, int lodLevel) where T : PrefabBase
Called by the import pipeline to create or reuse a prefab asset for a mesh. Loads an existing prefab asset if it matches a generated sub-path, otherwise creates a new ScriptableObject instance of T. Adds created prefab to internal lists and marks root prefabs when lodLevel == 0. -
private T LoadOrCreateAsset<T>(string sourcePath, string rootMeshName) where T : PrefabBase
Attempts to find an existing PrefabAsset in the user AssetDatabase whose subPath and name match the expected path. If found and it loads to type T, returns it; otherwise creates a new ScriptableObject of type T. -
private static bool PathCompare(string subPath1, string subPath2)
Helper to compare two paths after FullPath and trimming trailing backslashes in a case-insensitive invariant culture manner.
Usage Example
// Example: Invoking the static import helper from your mod/system code.
// 'world' is the ECS World (e.g. base.World from a system) and 'log' is an implementation of ILog.
// 'selectedTemplate' may be null to keep the imported prefab as-is.
string projectRoot = @"C:\MyArtProject\";
string importSubDir = @"C:\MyArtProject\Assets\Props\";
PrefabBase selectedTemplate = myTemplate; // e.g., an ObjectGeometryPrefab or null
PrefabFactory factory = await AssetImportPanel.ImportAssets(
selectedProjectRoot: projectRoot,
selectedDirectory: importSubDir,
selectedTemplate: selectedTemplate,
world: World.DefaultGameObjectInjectionWorld,
log: LogManager.GetLogger("AssetImport")
);
// After completion, use factory.rootPrefabs / factory.prefabs to inspect created prefabs.
Notes and tips: - Import is asynchronous and can be long-running; the UI disables reentrant imports while one is in progress. - The import routine uses SharedSettings.instance.editor to read user preferences such as useParallelImport and texture compression settings. - When supplying a template, the code clones the template and sets mesh data so the template's other settings are used while replacing the visual mesh. - The method persists created prefabs into AssetDatabase.user (targetDatabase) and then registers them with the PrefabSystem so they are immediately available in the editor.