0
\$\begingroup\$

My application has a UI for settings which is a mixture of textboxes and scrollbars with which users can interact. I have created a class for these settings and create an object to store them. The user can also give command line arguments to apply settings from a file.

I have a function that loads this data into the UI from the object (i.e read from a file) and another function that changes object data when user make changes to the UI.

My problem is: if I load the data from file into UI, the function responsible to reflect these changes in the UI fires up but the other function also fires up because changes were made to the UI.

So what should I do to overcome this? Should I implement some kind of state mechanism that would tell code that user has made the changes, or setup some flag which will mark that the data is loaded from a file. If you can suggest some design patterns to follow?

\$\endgroup\$
2
  • \$\begingroup\$ Many Unity UI components have a method like SetValueWithoutNotify() that will allow you to populate their displayed state without invoking the event handler that responds to the player interacting with the UI control. Are you using something like this? Can you show us a sample of your code so we can understand the strategy you're using currently? \$\endgroup\$ – DMGregory Apr 25 at 19:39
  • \$\begingroup\$ I haven't implemented the code yet. I am still in the planning phase. So I am getting confused about how to solve such an issue. Your suggestion was the perfect thing I was looking for. Thanks :) \$\endgroup\$ – Vidit Apr 25 at 20:39
0
\$\begingroup\$

Elaborating on my comment as an answer:

Many Unity UI components (and their Text Mesh Pro UGUI counterparts) include a SetValueWithoutNotify method. This lets you update the widget with a new displayed value - as though the player had entered or selected it themselves - but without invoking any onValueChanged events that your code might be listening for to update the data model behind the scenes.

See eg.

This way you can update the displayed state of the UI from your data model, without creating a feedback loop where the UI invokes a callback that immediately tries to write the new UI value back into the model it came from.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.