Skip to content

Commit

Permalink
feat: Added new convenience button in all importer inspectors to open…
Browse files Browse the repository at this point in the history
… the file's profiler sample
  • Loading branch information
Cammin committed May 20, 2024
1 parent ae55889 commit 036b214
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using LDtkUnity.InternalBridge;
using UnityEditor;
using UnityEngine;

Expand All @@ -24,7 +26,8 @@ internal abstract class LDtkImporterEditor : ScriptedImporterEditor

protected LDtkSectionDependencies SectionDependencies;
private SerializedProperty _reimportOnDependencyChangedProp;
private readonly GUIContent _reimportOnDependencyChanged = new GUIContent

private static readonly GUIContent ReimportOnDependencyChanged = new GUIContent
{
text = "Depend On Dependencies",
tooltip = "Controls whether this project/level should be reimported when any of the dependencies are changed. (ex. saved changes to a prefab)\n" +
Expand Down Expand Up @@ -100,7 +103,60 @@ protected static void DrawTextBox(string msg = null, MessageType type = MessageT

public void DrawDependenciesProperty()
{
EditorGUILayout.PropertyField(_reimportOnDependencyChangedProp, _reimportOnDependencyChanged);
EditorGUILayout.PropertyField(_reimportOnDependencyChangedProp, ReimportOnDependencyChanged);
}

public void DrawProfilerButton()
{
if (serializedObject.isEditingMultipleObjects)
{
return;
}

//bool defined = LDtkScriptingDefines.IsProfilingEnabled();

string fileName = Path.GetFileName(Importer.assetPath);
string pathToSample = LDtkProfiler.GetOutputFilePath(fileName);
bool exists = File.Exists(pathToSample);

string FigureOutText()
{
string firstSentence = "Opens the profiler window with this file's sampled profiler recording. Make sure to reimport to get the latest sample.";
if (!exists)
{
return firstSentence + "\n\nNo profiler sample exists. Enable the profiler define in the LDtkUnity project settings and reimport to generate one.";
}

return firstSentence;
}

GUIContent profilerButtonContent = new GUIContent
{
text = "View Sample",
tooltip = FigureOutText(),
image = LDtkIconUtility.GetUnityIcon("UnityEditor.ProfilerWindow", ""),
};

using (new LDtkGUIEnabledScope(exists))
{
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
bool pressed = GUILayout.Button(profilerButtonContent);
GUILayout.EndHorizontal();

if (!pressed)
{
return;
}
}

if (!exists)
{
Debug.LogError($"No profiler sample exists for this import. Maybe the file wasn't generated yet.\n{pathToSample}");
return;
}

InternalEditorBridge.ShowAndLoadProfilerSample(pathToSample);
}

public void DrawLogEntries()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public override void OnInspectorGUI()

try
{
DrawProfilerButton();
LDtkEditorGUIUtility.DrawDivider();
DrawLogEntries();
TryDrawProjectReferenceButton();
DrawDependenciesProperty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ private void ShowGUI()

//todo disabled for now. Currently doesn't work perfectly as expected
//DrawExportButton();


DrawProfilerButton();
LDtkEditorGUIUtility.DrawDivider();

_sectionMain.SetJson(data);
_commandUpdater.TryDrawFixButton(data);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace LDtkUnity.Editor
{
/// <summary>
/// A sub asset would be anything that isn't the project, like levels and tileset files.
/// </summary>
internal abstract class LDtkSubImporterEditor : LDtkImporterEditor
{
private static readonly GUIContent ReimportProjectButton = new GUIContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ public override void OnInspectorGUI()
return;
}

DrawProfilerButton();
LDtkEditorGUIUtility.DrawDivider();
DrawLogEntries();

try
Expand All @@ -87,10 +89,9 @@ public override void OnInspectorGUI()
}

DrawDependenciesProperty();

DoOpenSpriteEditorButton();

SectionDependencies.Draw();

}
catch (Exception e)
{
Expand Down
18 changes: 16 additions & 2 deletions Assets/LDtkUnity/Editor/Utility/LDtkScriptingDefines.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,28 @@ public static void PreprocessorAddRemoveGui()
}
}

private static void DrawButton()
public static bool IsProfilingEnabled()
{
BuildTargetGroup current = EditorUserBuildSettings.selectedBuildTargetGroup;
string currentDefines = GetDefines(current);
return currentDefines.Contains(DEFINE);
}

private static string GetDefines(BuildTargetGroup current)
{
#if UNITY_2021_2_OR_NEWER
NamedBuildTarget group = NamedBuildTarget.FromBuildTargetGroup(current);
string currentDefines = PlayerSettings.GetScriptingDefineSymbols(group);
#else
string currentDefines = PlayerSettings.GetScriptingDefineSymbolsForGroup(current);
#endif
return currentDefines;
}

private static void DrawButton()
{
BuildTargetGroup current = EditorUserBuildSettings.selectedBuildTargetGroup;
string currentDefines = GetDefines(current);

GUILayoutOption width = GUILayout.Width(180);
if (currentDefines.Contains(DEFINE))
Expand All @@ -67,11 +79,13 @@ private static void DrawButton()
void SetNewDefines(string newDefines)
{
#if UNITY_2021_2_OR_NEWER
PlayerSettings.SetScriptingDefineSymbols(group, newDefines);
PlayerSettings.SetScriptingDefineSymbols(NamedBuildTarget.FromBuildTargetGroup(current), newDefines);
#else
PlayerSettings.SetScriptingDefineSymbolsForGroup(current, newDefines);
#endif
}
}


}
}
11 changes: 11 additions & 0 deletions Assets/LDtkUnity/InternalBridge/InternalEditorBridge.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Profiling;
using UnityEditor.ShortcutManagement;
using UnityEditor.U2D.Sprites;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Events;

Expand Down Expand Up @@ -156,6 +158,15 @@ public static GameObject InstantiateForAnimatorPreview(UnityEngine.Object origin
{
return EditorUtility.InstantiateForAnimatorPreview(original);
}

public static void ShowAndLoadProfilerSample(string filename)
{
ProfilerWindow window = EditorWindow.GetWindow<ProfilerWindow>(false);
if (ProfilerDriver.LoadProfile(filename, false))
{
window.SetRecordingEnabled(false);
}
}

public static void AddManagedGameObject(this PreviewRenderUtility scene, GameObject go) => scene.AddManagedGO(go);
}
Expand Down
7 changes: 7 additions & 0 deletions Assets/LDtkUnity/Runtime/Tools/LDtkProfiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ public static void BeginWriting(string path)
Profiler.BeginSample(path);
}

public static string GetOutputFilePath(string assetName)
{
string directory = $"{Path.GetDirectoryName(Application.dataPath)}/Profiler";
string fullPath = $"{directory}/{assetName}.raw";
return fullPath;
}

[Conditional("LDTK_ENABLE_PROFILER")]
public static void EndWriting()
{
Expand Down

0 comments on commit 036b214

Please sign in to comment.