Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate public readonly properties instead of private fields #2

Closed
Eastrall opened this issue Jan 14, 2022 · 0 comments
Closed

Generate public readonly properties instead of private fields #2

Eastrall opened this issue Jan 14, 2022 · 0 comments
Labels
area: code-generator The issue is related to the code generator engine enhancement New feature or request

Comments

@Eastrall
Copy link
Owner

Eastrall commented Jan 14, 2022

At this moment, Rosalina generates private fields for that represents the named UI components defined in the UXML template.
But if we want to interect with properties within another document, a public field or property is required. Thus, I believe the use of public readonly property could be interesting.

public to grant access to the property.
"readonly" to prevent the user to reassign the inner UI field.

This would give us something like:

// MyDocument.g.cs
public partial class MyDocument
{
    // Root property has been omitted
    public Button MyButton { get; private set; }

    public void InitializeDocument()
    {
        MyButton = (UnityEngine.UIElements.Button)Root?.Q("MyButton");
    }
}

In the extension we will be able to use the property.

public partial class MyDocument
{
     private void OnEnable()
     {
         MyButton.clicked += OnClicked;
     }

    private void OnClicked()
    {
        // Do something
    }
}

When using a MyDocument instance, you'll be able to calls the MyButton property.

// other component
public class OtherComponent : MonoBehavior
{
    private void OnEnable()
    {
        var myDocument = new MyDocument();
       myDocument.MyButton.text = "Hello world!"; // OK !
       myDocument.MyButton = new UnityEngine.UIElements.Button(); // KO !
    } 
}
@Eastrall Eastrall added enhancement New feature or request area: code-generator The issue is related to the code generator engine labels Jan 14, 2022
@Eastrall Eastrall added this to To Do in Rosalina roadmap Jan 14, 2022
@Eastrall Eastrall added area: editor-extension The issue is related to Rosalina's editor extension area: code-generator The issue is related to the code generator engine and removed area: code-generator The issue is related to the code generator engine area: editor-extension The issue is related to Rosalina's editor extension labels Jan 14, 2022
@Eastrall Eastrall moved this from To Do to In Progress in Rosalina roadmap Jan 16, 2022
@Eastrall Eastrall moved this from In Progress to Done in Rosalina roadmap Jan 16, 2022
crowdoka pushed a commit to crowdoka/Rosalina that referenced this issue Dec 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: code-generator The issue is related to the code generator engine enhancement New feature or request
Projects
Development

No branches or pull requests

1 participant