Game.UI.Editor.ResourcePanelSystem
Assembly: Game
Namespace: Game.UI.Editor
Type: class
Base: EditorPanelSystemBase
Summary:
ResourcePanelSystem provides the Editor UI panel used to manage terraforming resource tools in the editor. It gathers terraformable prefabs that represent resources (ore, oil, fertile land, groundwater), builds the UI (tool icons and import/clear buttons), and implements texture import and clear operations that write into the game's cell maps (resource and groundwater maps). It supports importing textures from the project's "Heightmaps" folder and samples texture values into underlying CellMapData structures, completing any outstanding jobs before writing.
Fields
-
private static readonly string kTextureImportFolder = "Heightmaps"
Constant subpath used to filter image assets for texture import. Only ImageAssets whose meta subPath starts with this value are listed for import. -
private PrefabSystem m_PrefabSystem
Reference to the game's PrefabSystem, used to enumerate and retrieve terraformable prefabs. -
private ToolSystem m_ToolSystem
Reference to the ToolSystem used to activate/deactivate prefab tools when the user selects resource tools in the UI. -
private NaturalResourceSystem m_NaturalResourceSystem
Reference to the NaturalResourceSystem (cell map system) used for ore, oil and fertility maps. -
private GroundWaterSystem m_GroundWaterSystem
Reference to the GroundWaterSystem used for the groundwater map. -
private EntityQuery m_PrefabQuery
EntityQuery used to find UI-enabled terraform prefabs (PrefabData + TerraformingData + UIObjectData), excluding Deleted and Temp entities. -
private IconButtonGroup m_ToolButtonGroup
UI widget that hosts the resource tool icon buttons. -
private EditorSection m_TextureImportButtons
EditorSection that hosts the import/clear button rows for each resource type. -
private List<PrefabBase> m_ToolPrefabs = new List<PrefabBase>()
Cached list of terraform prefabs that represent resources. Used to check whether the currently active prefab tool belongs to this panel.
Properties
- None (there are no public properties declared on this type).
Constructors
public ResourcePanelSystem()
Default constructor. Marked with [Preserve] in the source to prevent stripping; performs no additional runtime initialization (initialization happens in OnCreate).
Methods
-
protected override void OnCreate()
Initializes the panel: obtains required systems (PrefabSystem, ToolSystem, NaturalResourceSystem, GroundWaterSystem), creates the EntityQuery to find terraformable UI objects, sets the panel title, and constructs the UI skeleton (an EditorSection for resource tools with an IconButtonGroup and an EditorSection for texture import buttons). This method sets up fields but does not populate the tool list — that occurs when a game is loaded. -
protected override void OnGameLoaded(Context serializationContext)
Called when a save or game is loaded. Enumerates all UI terraform prefabs via UIObjectInfo.GetSortedObjects(m_PrefabQuery). For each prefab that IsResourceTerraformingPrefab returns true, it: - Adds the prefab to m_ToolPrefabs,
- Creates an IconButton entry to toggle the prefab tool (uses ImageSystem.GetIcon and localized tooltip),
-
Creates a ButtonRow containing an "Import" and "Clear" button wired to ImportTexture and Clear respectively. After enumeration it assigns the icon buttons to m_ToolButtonGroup.children and the import/clear rows to m_TextureImportButtons.children.
-
private static bool IsResourceTerraformingPrefab(TerraformingPrefab prefab)
Returns true if the prefab's TerraformingTarget is a resource target (GroundWater, Ore, Oil, FertileLand). It excludes Height and Material targets and treats None as non-resource. -
protected override void OnStopRunning()
When the editor panel stops running, if the currently active prefab tool is one of this panel's resource prefabs, deactivates it via m_ToolSystem.ActivatePrefabTool(null). -
protected override bool OnCancel()
Handles cancel (e.g., ESC). If the active tool belongs to this panel's prefabs, deactivate it and return false (indicating the cancel was handled). Otherwise delegates to base.OnCancel(). -
private void ImportTexture(TerraformingTarget target)
Opens a LoadAssetPanel that lists textures returned by GetTextures() and calls OnLoadTexture on selection. The panel title is "Import " + target. The delegate passed to the LoadAssetPanel will call OnLoadTexture with the selected asset GUID and the resource target. -
private static IEnumerable<AssetItem> GetTextures()
Enumerates ImageAsset assets from AssetDatabase.global filtered by meta.subPath starting with kTextureImportFolder. For each matching ImageAsset yields an AssetItem with guid, fileName, displayName and image URI. This is used to populate the LoadAssetPanel. -
private void OnLoadTexture(Colossal.Hash128 guid, TerraformingTarget target)
Closes the sub-panel and attempts to load the selected ImageAsset from the AssetDatabase. If found, loads the Texture2D (srgb: true) and passes it to ApplyTexture for the requested target. The ImageAsset is disposed after use (using block ensures proper release). -
private void ApplyTexture(Texture2D texture, TerraformingTarget target)
Dispatches to the appropriate ApplyTexture overload, depending on the target: - GroundWater -> ApplyTexture(texture, m_GroundWaterSystem, ApplyGroundWater, null)
- Ore -> ApplyTexture(texture, m_NaturalResourceSystem, ApplyResource, ApplyOre)
- Oil -> ApplyTexture(texture, m_NaturalResourceSystem, ApplyResource, ApplyOil)
-
FertileLand -> ApplyTexture(texture, m_NaturalResourceSystem, ApplyResource, ApplyFertile)
-
private void ApplyTexture<TCell>(Texture2D texture, CellMapSystem<TCell> cellMapSystem, Action<Texture2D, CellMapData<TCell>, int, int, Func<TCell, ushort, TCell>> applyCallback, Func<TCell, ushort, TCell> resourceCallback) where TCell : struct, ISerializable
Generic method that gets writable CellMapDatavia cellMapSystem.GetData(readOnly: false, out JobHandle dependencies). It completes any returned dependencies before writing. Iterates over every cell (x,y) of the cell map texture size and calls applyCallback to update the cell using sampled texture data.
Notes: - It calls dependencies.Complete() — this will synchronize and wait for any jobs that need to finish before modifying the cell buffer. - The applyCallback is responsible for sampling and writing into data.m_Buffer at index computed from x,y.
-
private void ApplyResource<TCell>(Texture2D texture, CellMapData<TCell> data, int x, int y, Func<TCell, ushort, TCell> applyCallback) where TCell : struct, ISerializable
Generic helper used for natural-resource-like maps (ore/oil/fertility). Samples texture at (x,y) into an integer amount (scaled by 10000), casts to ushort, and replaces data.m_Buffer[index] by applying applyCallback(originalCell, sampledAmount). -
private void ApplyGroundWater(Texture2D texture, CellMapData<GroundWater> data, int x, int y, Func<GroundWater, ushort, GroundWater> _)
Samples texture into a short value (range up to 10000) and writes a GroundWater struct setting both m_Amount and m_Max to that value. -
private NaturalResourceCell ApplyOre(NaturalResourceCell cell, ushort amount)
Sets cell.m_Ore.m_Base to the sampled amount (wrapped in NaturalResourceAmount) and returns the modified cell. -
private NaturalResourceCell ApplyOil(NaturalResourceCell cell, ushort amount)
Sets cell.m_Oil.m_Base to the sampled amount and returns the modified cell. -
private NaturalResourceCell ApplyFertile(NaturalResourceCell cell, ushort amount)
Sets cell.m_Fertility.m_Base to the sampled amount and returns the modified cell. -
private int Sample<TCell>(Texture2D texture, CellMapData<TCell> data, int x, int y, int max) where TCell : struct, ISerializable
Samples the red channel of the texture using Texture2D.GetPixelBilinear at normalized coordinates corresponding to the data's texture size, clamps it (math.saturate), multiplies by max and rounds to nearest int. Used to produce the resource/groundwater amount from texture grayscale values.
Note: This method uses floating bilinear sampling for smoother mapping between texture and cell map.
private void Clear(TerraformingTarget target)
Dispatches to ClearMap depending on the target:- GroundWater -> ClearMap(m_GroundWaterSystem, ClearGroundWater)
- Ore -> ClearMap(m_NaturalResourceSystem, ClearOre)
- Oil -> ClearMap(m_NaturalResourceSystem, ClearOil)
-
FertileLand -> ClearMap(m_NaturalResourceSystem, ClearFertile)
-
private void ClearMap<TCell>(CellMapSystem<TCell> cellMapSystem, Func<TCell, TCell> clearCallback) where TCell : struct, ISerializable
Gets writable CellMapData(completing any dependencies) and then iterates over the entire data.m_Buffer clearing each cell using clearCallback. -
private NaturalResourceCell ClearOre(NaturalResourceCell cell)
Resets cell.m_Ore to default(NaturalResourceAmount) and returns the cell. -
private NaturalResourceCell ClearOil(NaturalResourceCell cell)
Resets cell.m_Oil to default(NaturalResourceAmount) and returns the cell. -
private NaturalResourceCell ClearFertile(NaturalResourceCell cell)
Resets cell.m_Fertility to default(NaturalResourceAmount) and returns the cell. -
private GroundWater ClearGroundWater(GroundWater _)
Returns default(GroundWater), effectively clearing groundwater data.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// obtain references to the systems used by the panel
m_PrefabSystem = base.World.GetOrCreateSystemManaged<PrefabSystem>();
m_ToolSystem = base.World.GetOrCreateSystemManaged<ToolSystem>();
m_NaturalResourceSystem = base.World.GetOrCreateSystemManaged<NaturalResourceSystem>();
m_GroundWaterSystem = base.World.GetExistingSystemManaged<GroundWaterSystem>();
// build UI skeleton (title, tool group, texture import section)
title = "Editor.RESOURCES";
m_ToolButtonGroup = new IconButtonGroup();
m_TextureImportButtons = new EditorSection
{
displayName = "Editor.RESOURCE_TEXTURE_LABEL",
tooltip = "Editor.RESOURCE_TEXTURE_LABEL_TOOLTIP",
expanded = true
};
children = new IWidget[] { Scrollable.WithChildren(new IWidget[] {
new EditorSection {
displayName = "Editor.RESOURCE_TOOLS",
tooltip = "Editor.RESOURCE_TOOLS_TOOLTIP",
expanded = true,
children = new IWidget[] { m_ToolButtonGroup }
},
m_TextureImportButtons
})};
}
Notes and tips: - Import textures should be placed in the project's "Heightmaps" asset folder (kTextureImportFolder) to be discoverable by the import dialog. - ApplyTexture and ClearMap force-completion of any outstanding job dependencies returned by CellMapSystem.GetData; these operations are synchronous and may be costly for large maps. - The texture sampling scales the red channel to the range [0, max] using bilinear sampling and saturate; the code uses a max of 10000 for resource/groundwater amounts. - When loading ImageAsset via AssetDatabase.global.TryGetAsset, the ImageAsset is disposed after Load is called to avoid leaks.