Guest User

Untitled

a guest
Oct 7th, 2025
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.23 KB | Gaming | 0 0
  1. PrefabPreviewEditorWindow = EditorWindow:New({
  2.     Title = "Prefab Previewer",
  3.     Submenu = "Witchy Project" -- This tells the engine where to place it in the View menu
  4. })
  5.  
  6. function PrefabPreviewEditorWindow:Initialize()
  7.     -- the behavior of this window assumes the assets obtained here
  8.     -- won't be deleted while the window is active, so in a more
  9.     -- robust scenario the code would be a bit more complex
  10.     self.prefabs = AssetManager.GetAssetsOfType(AssetType.Prefab)
  11.  
  12.     -- this shows the relative path to the currently selected asset
  13.     self.pathLabel = self:CreateLabel("Select a Prefab")
  14.  
  15.     -- the MVP, it's a default SceneView so it has an empty scene
  16.     -- loaded in. One could also load an existing scene into it,
  17.     -- or create several scene views if desired, in the same window.
  18.     -- we provide an Update function which mimics the Update loop
  19.     -- in the engine proper
  20.     self.sceneView = self:CreateSceneView()
  21.     self.sceneView.Update = function(deltaTime)
  22.         self:SceneUpdate(deltaTime)
  23.     end
  24.  
  25.     -- the list box that will hold our prefabs
  26.     -- we will instantiate the selected prefab in the SceneView's
  27.     -- scene for previewing purposes.
  28.     self.prefabListBox = self:CreateListBox("Prefabs")
  29.     for i = 1, #self.prefabs do
  30.         self.prefabListBox:AddItem(self.prefabs[i].FileName)
  31.     end
  32.     self.prefabListBox.OnSelectionChanged = function(newIndex)
  33.         self:OnSelectedPrefabChanged(newIndex)
  34.     end
  35. end
  36.  
  37. function PrefabPreviewEditorWindow:SceneUpdate(deltaTime)
  38.     -- if we've instantiated a prefab, rotate the entity so it looks cool
  39.     if self.activeEntity ~= nil then
  40.         self.activeEntity:RotateRight(80.0 * deltaTime)
  41.     end
  42. end
  43.  
  44. function PrefabPreviewEditorWindow:OnSelectedPrefabChanged(newIndex)
  45.     if newIndex > 0 then
  46.         -- we only want at most one instantiated prefab in the scene
  47.         -- so delete the current one, if exists.
  48.         if self.activeEntity ~= nil then
  49.             self.sceneView:DestroyEntity(self.activeEntity)
  50.             self.activeEntity = nil
  51.         end
  52.  
  53.         self.pathLabel:SetText(self.prefabs[newIndex].FilePath)
  54.  
  55.         self.activeEntity = self.sceneView:InstantiatePrefab(self.prefabs[newIndex].Id)
  56.         self.activeEntity:SetWorldPosition(Vec3:new(0.0, 0.0, 0.0))
  57.         self.activeEntity:ClearRotation()
  58.  
  59.         -- this long named function travels the entity's hierarchy and tries to build a
  60.         -- bounding sphere that covers its, and its children's, bounding spheres.
  61.         local boundingSphere = self.activeEntity:GetBoundingSphereCoveringHierarchyInWorldSpace()
  62.         if boundingSphere then
  63.             local targetPos = boundingSphere.Origin + Vec3:new(
  64.                 boundingSphere.Radius + boundingSphere.Radius * 0.2,
  65.                 0.1,
  66.                 0.1
  67.             )
  68.             self.sceneView:SetCameraPosition(targetPos)
  69.             self.sceneView:SetCameraLookAt(boundingSphere.Origin)
  70.         else
  71.             self.sceneView:SetCameraPosition(Vec3:new(0.2, 0.0, 0.0))
  72.             self.sceneView:SetCameraLookAt(Vec3:new(0.0, 0.0, 0.0))
  73.         end
  74.     else
  75.         if self.activeEntity ~= nil then
  76.             self.sceneView:DestroyEntity(self.activeEntity)
  77.             self.activeEntity = nil
  78.         end
  79.         self.pathLabel:SetText("Select a Prefab")
  80.     end
  81. end
  82.  
  83. function PrefabPreviewEditorWindow:OnContentAreaResized(newWidth, newHeight)
  84.     -- Stuck with absolute positioning/sizing until I figure out what layouts should work like
  85.     local labelHeight = 20
  86.     local leftRightMargin = 10
  87.     local contentWidthWithoutMargins = newWidth - leftRightMargin - leftRightMargin
  88.     local sceneViewWidth = 0.8 * contentWidthWithoutMargins
  89.     local prefabListWidth = contentWidthWithoutMargins - sceneViewWidth
  90.     local sceneAndListHeight = newHeight - labelHeight
  91.  
  92.     self.pathLabel:SetPosition(leftRightMargin, 0)
  93.     self.pathLabel:SetSize(contentWidthWithoutMargins, labelHeight)
  94.  
  95.     self.sceneView:SetPosition(leftRightMargin, labelHeight)
  96.     self.sceneView:SetSize(sceneViewWidth, sceneAndListHeight)
  97.  
  98.     self.prefabListBox:SetPosition(leftRightMargin + sceneViewWidth, labelHeight)
  99.     self.prefabListBox:SetSize(prefabListWidth, sceneAndListHeight)
  100. end
  101.  
Advertisement
Add Comment
Please, Sign In to add comment