0
\$\begingroup\$

I am trying to develop a tool for baking light in Unity but with some modifications and additions.

Currently, what I want to achieve is for the commands Generate Lighting, Bake Reflection Probes, and Clear Baked Data to function similarly to how they do in the Lighting window. However, Unity does not provide a direct API for these functions.

To work around this, I created a debugging tool to list all available method names in the Lighting window, then I called them (the mentioned functions) by their names, which I found through the debugging results.

So, I have two questions:

  1. Does this method violate any of Unity's copyrights or policies (Attempting to access an API in Unity that was not provided by the engine's owning company itself)?

  2. Is there a better way to achieve this? (Currently, this approach works well since it is executed only once within the editor and does not affect the baking results or the game's performance).

Note: I know that I can access some of the mentioned functions through Lightmapping API, but sometimes the results vary or become inaccessible.

The debugging tool code:

using System.Reflection;
using UnityEditor;
using UnityEngine;

public class DebugLightingWindowMethods : EditorWindow
{
    [MenuItem("Special Tools/Debug Lighting Window Methods")]
    public static void DebugMethods()
    {
        System.Type lightingWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.LightingWindow");

        if (lightingWindowType == null)
        {
            Debug.LogError("LightingWindow type not found!");
            return;
        }

        MethodInfo[] methods = lightingWindowType.GetMethods(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);

        Debug.Log($"Found {methods.Length} methods in LightingWindow:");

        foreach (MethodInfo method in methods)
        {
            Debug.Log($"Method: {method.Name}");
        }
    }
}

The function by which I call the methods: (methodName will be something like "DoBake" which is called when the 'Generate Lighting' button is pressed.)

void InvokeLightingWindowMethod(string methodName)
{
    System.Type lightingWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.LightingWindow");

    EditorWindow lightingWindow = EditorWindow.GetWindow(lightingWindowType);

    MethodInfo method = lightingWindowType.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic);
    if (method != null)
    {
        method.Invoke(lightingWindow, null);
    }
    else
    {
        Debug.LogError($"Could not find method '{methodName}' in {lightingWindowType}.");
    }
}
\$\endgroup\$

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.