psydack / uimgui

UImGui (Unity ImGui) is an UPM package for the immediate mode GUI library using ImGui.NET. This project is based on RG.ImGui project.
MIT License
295 stars 50 forks source link

[BUG] Custom font and Icon font crashes Unity Editor #44

Open Starpelly opened 1 year ago

Starpelly commented 1 year ago

Describe the bug Unity Editor crashes on play, expected font to load. Even if I load it the traditional way, the Icon font doesn't load in the texture. image

To Reproduce Steps to reproduce the behavior:

  1. Create new Font Atlas Config
  2. Assign a font to it
  3. Assign it to UImGui component
  4. Create new script to add "icon font"
  5. Add function below
  6. Add function to `Font Custom Initializer'
  7. Play the game

Version/Branch of UImGui: Version: 4.1.0

Unity Version 2021.3.15f1

Render pipeline (Built-in)

Expected behavior Load icon font with normal font

Screenshots image image

Code

// "Traditional" way -------------------------------------------------------------------------------------
public void AddFont(ImGuiIOPtr io)
{
    io.Fonts.AddFontFromFileTTF(Application.streamingAssetsPath + "\\Questrial-Regular.ttf", 14);
}
// --------------------------------------------------------------------------------------------------------

public unsafe void AddIconFont(ImGuiIOPtr io)
{
    ImFontConfig fontConfig = default;
    ImFontConfigPtr fontConfigPtr = new ImFontConfigPtr(&fontConfig);
    fontConfigPtr.MergeMode = true;
    fontConfigPtr.PixelSnapH = true;
    int[] icons = { FontIcon.IconMin, FontIcon.IconMax, 0 };
    fixed (void* iconsPtr = icons)
    {
        io.Fonts.AddFontFromFileTTF(Application.streamingAssetsPath + "\\fa-solid-900.ttf", 13.0f, fontConfigPtr, (System.IntPtr)iconsPtr);
    }
}

Crash Report

Obtained 28 stack frames
0x00007ffb40a4252f (cimgui) igValue_Int
0x000002327e7d370a (Mono JIT Code) (wrapper managed-to-native) ImGuiNET.ImGuiNative:ImFontAtlas_Build (ImGuiNET.ImFontAtlas*)
0x000002327e7d3623 (Mono JIT Code) ImGuiNET.ImFontAtlasPtr:Build ()
0x000002327e7d13ab (Mono JIT Code) [TextureManager.cs:162] UImGui.Texture.TextureManager:BuildFontAtlas (ImGuiNET.ImGuiIOPtr,UImGui.Assets.FontAtlasConfigAsset&,UnityEngine.Events.UnityEvent`1<ImGuiNET.ImGuiIOPtr>) 
0x000002327e79f18b (Mono JIT Code) [UImGui.cs:168] UImGui.UImGui:OnEnable () 
0x000002327e945b68 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007ffb0fcbfeb4 (mono-2.0-bdwgc) [mini-runtime.c:3445] mono_jit_runtime_invoke 
0x00007ffb0fbfe764 (mono-2.0-bdwgc) [object.c:3066] do_runtime_invoke 
0x00007ffb0fbfe8fc (mono-2.0-bdwgc) [object.c:3113] mono_runtime_invoke 
0x00007ff6359b1104 (Unity) scripting_method_invoke
0x00007ff6359909a4 (Unity) ScriptingInvocation::Invoke
0x00007ff635979ab4 (Unity) MonoBehaviour::CallMethodIfAvailable
0x00007ff635978396 (Unity) MonoBehaviour::AddToManager
0x00007ff635978af9 (Unity) MonoBehaviour::AwakeFromLoad
0x00007ff635a66b9f (Unity) AwakeFromLoadQueue::InvokePersistentManagerAwake
0x00007ff635a6760c (Unity) AwakeFromLoadQueue::PersistentManagerAwakeFromLoad
0x00007ff635a67773 (Unity) AwakeFromLoadQueue::PersistentManagerAwakeFromLoad
0x00007ff63688b956 (Unity) ActivateSceneAfterReset
0x00007ff6368956a7 (Unity) EditorSceneManager::ResetOpenScenes
0x00007ff63689679a (Unity) EditorSceneManager::RestoreSceneBackups
0x00007ff63659f185 (Unity) PlayerLoopController::EnterPlayMode
0x00007ff6365aefe7 (Unity) PlayerLoopController::SetIsPlaying
0x00007ff6365b23e9 (Unity) Application::TickTimer
0x00007ff636a0619a (Unity) MainMessageLoop
0x00007ff636a0aa5b (Unity) WinMain
0x00007ff637dacade (Unity) __scrt_common_main_seh
0x00007ffba7c655a0 (KERNEL32) BaseThreadInitThunk
0x00007ffba942485b (ntdll) RtlUserThreadStart
trapazza commented 1 year ago

@Starpelly Hey, did you manage to find a workaround for this issue?

Daily999 commented 9 months ago

Use Font Custom Initializer Event

        public void InitializeFont(ImGuiIOPtr io) 
        {
            var fontPath = $"{Application.streamingAssetsPath}/YOUR_FONT.ttf";
            io.Fonts.AddFontFromFileTTF(fontPath, 32, null, io.Fonts.GetGlyphRangesDefault());
        }
GenesisAN commented 2 months ago

It doesn't seem to work for me, I'm having the same problem, is this problem solved so far, I'm not quite sure what step I'm missing.

Starpelly commented 2 months ago

It doesn't seem to work for me, I'm having the same problem, is this problem solved so far, I'm not quite sure what step I'm missing.

Since this question was asked, I've just decided to not use ImGui and role out my own thing. Sorry, I don't think this was ever fixed, sadly.

GenesisAN commented 2 months ago

它似乎对我不起作用,我遇到了同样的问题,到目前为止这个问题是否已解决,我不太确定我错过了哪一步。

既然问了这个问题,我就决定不用ImGui,而是把自己的东西演绎出来。对不起,可悲的是,我认为这从未得到解决。

It seems pretty random that I can just use it fine in some unity versions, but in some it will just crash. I had a project before where I successfully enabled it, but today after I reimported uimgui it started crashing as well. Nothing has changed in the settings.

I've switched straight to godot for now and it's much easier on 2d development. And support for imgui is easy.

GenesisAN commented 2 months ago

You could try a commit based on #c849ffc, download it and put it directly into the project, don't use the url import, otherwise unity3d will download you the latest commit, in this version I managed to set the fonts without crashing. The crash seems to be related to the DemoWindows form size. If it is crashing before importing, then delete imgui.ini from the directory and don't display the demo windows window, just create your own.

As for configuring fonts, just use the Font Custom Initializer (ImGuilOptr) callback and add your method script with the ImGuilOptr parameter in it