i’m trying to make a fraction calculator simulator kinda like the pic below
so when input is changed it automatically change according to the input are there any way to do this?
Your question is a bit vague. This is totally doable but could you please clarify what you’d like to hear? Is this a question on:
- How to do the UI for this?
- How to react to the UI values changing?
- What UI solution are you using? UGUI or UI ToolKit?
- How to do the visualization?
- How to do the code architecture for this, e.g. subscribing to a value-changed-event with a callback to update the visualization?
In the interest of time, I won’t lay out the full solution here, but hopefully enough to get you started.
- First of all, I’d recommend that make sure that you are using Unity 2022.3.x or newer for this.
- Create a new UI Document in your scene and Project browser, see the introduction manual pages for UI Toolkit in runtime usage for detailed instructions.
- Make sure to use UI Builder to create the UI you want in an intuitive manner. Integer Input fields are likely what you’ll want to be using for the value inputs
- Create your own custom control (find more details on how to do that here) for the diagram by creating a new script with a class that inherits from
VisualElement and in its constructor, subscribe to
generateVisualContent += OnGenerateVisualContent; (find details on this API here)
- Check out this forum post for how to draw the circular shape for the diagram. Particularly the parts about using the MeshGenerationContext passed in to the method you subscribed to the
generateVisualContent event and how to use the painter2D property on it, and its Arc function, to draw your diagram.
- In a script that you attach to the same GameObject that hosts the UI Document in your scene, add a script that sets up the input fields by Registering like such
RegisterValueChangedCallback<ValueChange<int>>(FunctionToUpdateMyUI) and in your
FunctionToUpdateMyUI you then instruct your custom control to regenerate its mesh and use the new value as input for your arc visualizations.