Eastrall / Rosalina

Rosalina is a code generation tool for Unity's UI documents. It generates C# code-behind script based on a UXML template.
MIT License
135 stars 18 forks source link

Feature Request #12

Closed Randolphjand closed 2 years ago

Randolphjand commented 2 years ago

Is there a way to support using dashes in naming the fields? When I try to modify existing uxml files that use dashes in the names, it does not work. I am talking about when someone names a field something like a label field and they name it main-label which seems to be a kind of standard from many toolkit existing projects that I have found.

Eastrall commented 2 years ago

Hello,

Right now Rosalina doesn't support property names with snake-case. What the generator does, it that it takes the element name attribute and just uses it for the C# code generation. For example:

<ui:Label name="title-label" text="Information" />

this will generate the following property in C#

public Label title-label { get; private set; }

Of course, has a feature, we could imagine implementing something that converts the names that has - (dashes) into PascalCase; if we take the UXML above, that would result in a C# property:

public Label TitleLabel { get; private set; }

Thus, by doing this, we will have to check if there is not another TitleLabel in the document:

<ui:Label name="title-label" text="Information" />
<ui:Label name="TitleLabel" text="Information" />

This will result in a generation error.

Thank you for suggesting this feature. I am going to plan this and do the appropriate changes. 😉

Blackclaws commented 2 years ago

Here you go:

public static class StringExtensions
{
    public static string ToUpperStart(this string s)
    {
        if (s.Length == 0) return s;
        if (s.Length == 1) return s.ToUpper();
        return s.Substring(0, 1).ToUpper() + s.Substring(1);
    }

    public static string KebabToCamelCase(this string s)
    {
        var parts = s.Split('-');
        var builder = new StringBuilder();
        foreach (var part in parts)
        {
            builder.Append(part.ToUpperStart());
        }

        return builder.ToString();
    }
}

Licensing this snippet CC0 or MIT whichever you prefer.

Eastrall commented 2 years ago

Released in version 1.1.0: https://github.com/Eastrall/Rosalina/releases/tag/v1.1.0