4 [
Attribute(
"1",
desc:
"When enabled, preview prefab can be used also in runtime, not only when spawning from prefab.")]
5 protected bool m_bRuntime;
8 protected ResourceName m_PreviewPrefab;
11 protected ref array<ref SCR_BasePreviewEntry> m_aEntries;
15 ResourceName GetPreviewPrefab()
17 return m_PreviewPrefab;
24 int GetPreviewEntries(out notnull array<ref SCR_BasePreviewEntry> outEntries,
SCR_BasePreviewEntry entry)
29 for (
int i = 0; i < count; i++)
40 outEntries.Insert(entryCopy);
65 return prefabData.GetPreviewPrefab();
67 return ResourceName.Empty;
78 return prefabData.GetPreviewEntries(outEntries, entry);
90 return prefabData.IsRuntime();
101 static void GeneratePreviewEntries(WorldEditorAPI api, IEntitySource entitySource)
104 IEntityComponentSource componentSource;
105 for (
int i = 0, count = entitySource.GetComponentCount(); i < count; i++)
107 componentSource = entitySource.GetComponent(i);
108 if (componentSource.GetClassName().ToType().IsInherited(SCR_PreviewEntityComponent))
111 if (!componentSource)
115 BaseContainerList entriesList = componentSource.GetObjectArray(
"m_aEntries");
117 while (entriesList && entriesList.Count() >
index)
119 if (!api.RemoveObjectArrayVariableMember(entitySource, {ContainerIdPathEntry(
"SCR_PreviewEntityComponent")},
"m_aEntries", 0))
123 array<ref SCR_BasePreviewEntry> entries = {};
125 for (
int i = 0, count = entries.Count(); i < count; i++)
127 if (api.CreateObjectArrayVariableMember(entitySource, {ContainerIdPathEntry(
"SCR_PreviewEntityComponent")},
"m_aEntries",
"SCR_BasePreviewEntry", i))
130 entries[i].SaveToContainer(api, entitySource, {ContainerIdPathEntry(
"SCR_PreviewEntityComponent"), ContainerIdPathEntry(
"m_aEntries", i)});
140 void GeneratePreviewPrefab(WorldEditorAPI api, IEntitySource entitySource, IEntityComponentSource componentSource =
null)
143 if (!componentSource)
145 for (
int i = 0, count = entitySource.GetComponentCount(); i < count; i++)
147 componentSource = entitySource.GetComponent(i);
148 if (componentSource.GetClassName() ==
"SCR_PreviewEntityComponent")
153 ResourceName prefab, previewPrefab;
154 componentSource.Get(
"m_PreviewPrefab", previewPrefab);
159 Print(
"Entity is not a prefab!", LogLevel.WARNING);
167 string absolutePath, absolutePathPreview, ext;
168 Workbench.GetAbsolutePath(prefab.GetPath(), absolutePath,
false);
169 absolutePathPreview = FilePath.AppendExtension(FilePath.StripExtension(absolutePath, ext) +
FILE_SUFFIX, ext);
172 IEntitySource emptyEntitySource = api.CreateEntity(
"SCR_PrefabPreviewEntity",
string.Empty, 0,
null, vector.Zero, vector.Zero);
173 api.SetVariableValue(emptyEntitySource,
null,
"m_SourcePrefab", prefab);
174 api.CreateEntityTemplate(emptyEntitySource, absolutePathPreview);
175 api.DeleteEntity(emptyEntitySource);
178 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
179 MetaFile meta = resourceManager.GetMetaFile(absolutePathPreview);
180 previewPrefab = meta.GetResourceID();
181 api.SetVariableValue(entitySource, {ContainerIdPathEntry(
"SCR_PreviewEntityComponent")},
"m_PreviewPrefab", previewPrefab);
185 IEntitySource previewSource = api.CreateEntity(previewPrefab,
"", 0,
null, vector.Zero, vector.Zero);
187 previewEntity.CreatePrefabFromSource(api, prefab);
188 api.DeleteEntity(previewSource);
192 override void _WB_OnContextMenu(IEntity owner,
int id)
199 WorldEditorAPI api = genericOwner._WB_GetEditorAPI();
200 IEntitySource ownerSrc = api.EntityToSource(owner);
203 WBProgressDialog progress =
new WBProgressDialog(
"Processing...", Workbench.GetModule(WorldEditor));
205 api.BeginEntityAction();
206 api.BeginEditSequence(ownerSrc);
207 GeneratePreviewEntries(api, ownerSrc);
208 api.EndEditSequence(ownerSrc);
209 api.EndEntityAction();
216 WorldEditorAPI api = genericOwner._WB_GetEditorAPI();
217 IEntitySource ownerSrc = api.EntityToSource(owner);
219 api.BeginEntityAction();
220 GeneratePreviewPrefab(api, ownerSrc);
221 api.EndEntityAction();
227 override array<ref WB_UIMenuItem> _WB_GetContextMenuItems(IEntity owner)
229 return {
new WB_UIMenuItem(
"Generate preview entries", 0),
new WB_UIMenuItem(
"Generate preview prefab", 1) };