kamyker / UnityUxmlToCsharp

MIT License
10 stars 1 forks source link

Doesn't work at runtime (in html5, possibly all runtimes) #2

Open Gryhyphen opened 1 year ago

Gryhyphen commented 1 year ago

Hi kamyker!

I'm really impressed by how this works, I haven't seen anybody else attempt something like this!

However, I don't believe it works for runtime. Attempting to compile it has not been successful. I tried some attempts to fix it on my own by throwing in a bunch of #if UNITY_EDITOR directives, got it to compile but it wasn't working. I don't think I'll have time to continue working on this and submit a PR, since the project I'm working on can probably scrape by using the unity query / jquery approach.

Still, wanted to raise this as an issue with the current project so that if anybody else wants to use it, they are aware of this problem.

So the big things I've discovered (that are blocking runtime use) are:

  1. UnityUxmlToCsharp (as of 2022-09-25) currently only supports editor ui, not runtime ui.
  2. Code is tightly coupled to the using UnityEditor (which is not available at runtime) - either need to figure out how to use a lot of directives or create a better separation between editor-time and run-time code (through things like assembly defs) a. to be fair, this package only has an editor assembly def, I just hadn't realized that meant it only worked in the editor
  3. Need to update the assembly definition to allow this package to be included at runtime.

Here's the example code for how I was using UnityUxmlToCsharp at runtime (which isn't that different from how it works in editor time), unfortunately while this works in play mode, it does not build.

using KS.UxmlToCsharp;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    using UnityEngine.UIElements;

    /// <summary>
    /// The Main Menu is a component that
    /// populates a UIDocument with the MainMenu uxml and allows
    /// for code access to the UI.
    /// </summary>
    [RequireComponent(typeof(UIDocument))]
    public class MainMenu : MonoBehaviour
    {
        private UIDocument Document { get; set; }

        // Start is called before the first frame update
        private void Start()
        {
            this.Document = this.gameObject.GetComponent<UIDocument>();

            // Hard-coded to main menu - TODO: is there a better way to do this?
            var root = this.Document.rootVisualElement;
            root.Clear();
            var menuUI = new MainMenuConverted();
            root.AddChildrenOf(menuUI);

            // Set focus
            menuUI.NewGameButton.Focus();

            // Register callbacks
            menuUI.NewGameButton.RegisterCallback<NavigationSubmitEvent>(this.OnNewGameButtonSubmit);
        }

        private void OnNewGameButtonSubmit(NavigationSubmitEvent e)
        {
            SceneManager.LoadScene("Level 1");
        }
    }
kamyker commented 1 year ago

Yes, it was created when UI Toolkit was supporting only editor. I have rough idea how to convert it to runtime. Will update in few days :).

kamyker commented 1 year ago

Or not for now as there are still bugs...

https://forum.unity.com/threads/bug-in-ui-builder-nullref-when-building-custom-element-tree-from-uxml-unity-2021-2-6f1.1217169/