Jump to content

Gui from the main Editor Viewport


photo

Recommended Posts

Hi,

I'm using Unigine 2.16.1, is there a way to get Gui from the main Editor Viewport to add Widgets (in my case, I need to use a  WidgetSpriteViewport)

I tried to use engine.window_manager.getMainWindow() with the following code :

EngineWindow test = engine.window_manager.getMainWindow();
Gui g = test.getGui();

but the resulting EngineWindow test is NULL.

Thanks in advance!

Pierre

Link to comment

Hi Pierre_Bultingaire,

If you use C++ and Editor's includes (write your plugin), you can use something like this:

#include <editor/UnigineViewportManager.h>
  
GuiPtr getEditorViewportGui()
{
	auto id = UnigineEditor::ViewportManager::getLastHoveredViewportWindow();
	return id ? UnigineEditor::ViewportManager::getViewportWindowGui(id) : nullptr;
}

I'm not sure you can access the editor's gui using UnigineScript..

Best regards,
Alexander

Link to comment

Hi alexander, thanks for your answer.

I would like to access it without hovering it.
I think a solution might be to set the Editor Viewport as main with the function setMain() so the result of getMainWindow() wouldn't be NULL, but i'm not sure how to do it.

Pierre

Link to comment

Hi Pierre_Bultingaire,

This is an implementation of the Editor plugin for creating an API for a UnigineScript.

bool EditorPlugin::init()
{
    int id = Interpreter::addGroup("EditorAPI");

    Unigine::Interpreter::addExternFunction("getNumViewportWindows", MakeExternObjectFunction(this, &EditorPlugin::getNumViewportWindows), id);
    Unigine::Interpreter::addExternFunction("getViewportWindowGui", MakeExternObjectFunction(this, &EditorPlugin::getViewportWindowGui), id);

    return true;
}

void EditorPlugin::shutdown()
{
    Interpreter::removeGroup("EditorAPI");
}

int EditorPlugin::getNumViewportWindows()
{
    return UnigineEditor::ViewportManager::getNumViewportWindows();
}

Unigine::GuiPtr EditorPlugin::getViewportWindowGui(int index)
{
    auto id = UnigineEditor::ViewportManager::getViewportWindowId(index);
    auto gui = UnigineEditor::ViewportManager::getViewportWindowGui(id);

    return gui;
}

And then using this API you can use UnigineScript.

WidgetSpriteViewport sprite_viewport;

int init() 
{
  	// Getting the GUI of the desired viewport using an API function
	Gui gui = getViewportWindowGui(0);
	
  	if(gui != NULL)
    	{
		sprite_viewport = new WidgetSpriteViewport(gui, 250, 250);
		gui.addChild(sprite_viewport, GUI_ALIGN_BOTTOM | GUI_ALIGN_RIGHT);
    	}
  
	return 1;
}

int update() 
{
	float time = engine.game.getTime();
	
	if(sprite_viewport != NULL)
		sprite_viewport.setCamera(engine.editor.getPlayer().getCamera());

	return 1;
}

 

Here some helpful links:

Editor API - https://developer.unigine.com/en/docs/2.17/api/editor/index.html

How to create Editor plugin - https://developer.unigine.com/en/docs/2.17/editor2/extensions/custom_plugin

  • Like 2
Link to comment
×
×
  • Create New...