本篇配合教程食用
教你用虚幻引擎调用ImGui图形界面库_哔哩哔哩_bilibili
---------------------------------------------------------------------------------------------
ImGui是一个小巧灵活、简洁美观的图形界面库
首先我们直接参考Github
https://github.com/SLSNe/Unreal5-ImGui
把插件下载下来后
打开项目根目录
.../Plugins/ImGui/
如果没有Plugins文件夹就新建一个
把ImGui插件放里面,然后打开引擎,这里会提示rebuild,选择rebuild,
这里注意要 C++ 项目才行,否则会出错,如果是蓝图项目先转成C++项目
重新打开引擎,
在插件窗口搜索imgui就可以看到这个插件,那么就可以测试了
运行游戏后,按下左上角波浪号 ~
输入ImGui.ToggleDemo
显示ImGui Debug窗口就是加载调用成功
如果想取消窗口就再输入一遍
这只是一个简单的调用小示例,可以在cpp里写各种需要的功能然后在界面上显示
新建一个C++ Actor类
在Build.cs里添加模块ImGui
添加头文件 #include <imgui.h> , 如果这里显示波浪号就关闭VS和引擎重新生成文件
然后打开VS运行 创建我们的ImGui类
添加一个Cube
项目设置 Toggle Input 快捷键 为Tab键 , 这样在游戏中就可以快速显示鼠标进行交互
自定义一下自己的窗口
引擎右下角点击 热编译
把Cube拖入场景 , 运行游戏
优化界面和功能
// Fill out your copyright notice in the Description page of Project Settings.
#include "ImGuiActor.h"
#include <imgui.h>
// Sets default values
AImGuiActor::AImGuiActor()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AImGuiActor::BeginPlay()
{
Super::BeginPlay();
}
// Called every frame
void AImGuiActor::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
static ImVec4 textColor = ImVec4(1.0f, 1.0f, 1.0f, 1.0f);
static bool s1 = true;
static float Scale = 1.0f;
static float ScaleOld = Scale;
static float Local = 1.0f;
static float LocalOld = Local;
ImGui::StyleColorsDark();
ImGui::Begin("Star",nullptr);
ImGui::PushStyleColor(ImGuiCol_Text, textColor);
ImGui::Text("Hello Imgui~~~");
ImGui::NewLine();
ImGui::Checkbox("Show?", &s1);
SetActorHiddenInGame(!s1);
ImGui::NewLine();
ImGui::SliderFloat("Scale", &Scale, 0.1f, 20.0f);
if (Scale != ScaleOld)
{
ScaleOld = Scale;
SetActorScale3D(FVector3d{ Scale });
}
ImGui::NewLine();
ImGui::SliderFloat("Local", &Local, -20.0f, 20.0f);
if (Local != LocalOld)
{
LocalOld = Local;
AddActorWorldOffset(FVector3d{ Local });
}
ImGui::NewLine();
int CurrentIndex = -1;
const char* CurrentMap[] = { "StyleColorsDark", "StyleColorsLight", "StyleColorsClassic" };
if (ImGui::Combo("Theme", &CurrentIndex, CurrentMap, IM_ARRAYSIZE(CurrentMap)))
{
switch (CurrentIndex)
{
case 0:
ImGui::StyleColorsDark();
break;
case 1:
ImGui::StyleColorsLight();
break;
case 2:
ImGui::StyleColorsClassic();
break;
}
}
ImVec4 color = ImVec4(1.0f, 0.0f, 0.0f, 1.0f);
ImGui::NewLine();
if (ImGui::ColorEdit4("Text Color", (float*)&textColor)) {}
ImGui::PopStyleColor();
ImGui::End();
}