Controlling image height programatically
Hello!
I think this is a good learning example.
I'm trying to fill up this cup with water as the user moves a slider up/down.
One way I thought about achieving this was by having 2 pictures. One of a full cup, one of an empty cup and having the full cup's being positioned on top of the empty cup and having the height 0 when empty and at 100% height when full.
How would you go about controlling the image object height programatically?
thanks

Hi, Marco!
Sorry for the late answer!
That is a good learning example indeed. I will implement this for the Screen Image ( The Image with a Screen Transform Component on it)
I do this because ScreenTransform has a convenient API that transforms screen point to objects local point.
In case if you need to use Image in World Space we can adjust controller later.
I decided to modify UV coordinates to achieve desired event.
To do this I created a custom Graph Material [add link]
I prepared 3 textures:
1. Contents opacity mask ( black is transparent , white is solid)
2. Contents itself (with a transparent pixel row at the top)
3. Glass Image (with transparency)
Then I pressed Add New -> Graph Unlit , double clicked on created material and set up
I am going to change Contents texture UV coordinates to shift it up and down
This image has graph scheme. It may seem a bit complicated but it's a nice exercise :
Double click to see full sized image
So basically my material has a parameter that can have values from 0 to 1 and affects UМ offset of Contents texture.
Let's see how it works
Select AddNew -> Screen Image on the Objects panel and assign our material to the image . Then Select GraphMaterial on the Resources and drag the slider to see how it affects Image:
If you move your cursor over the property of material you can see it's Script Name ( you can also select corresponding node on the material graph editor window)
Knowing Scripting Name i can modify this property via script. For example :
Then i made a script that takes a screen position and affects material parameter.
[Hint] Set Stretch mode of an image to fit height :
It's a bit complex but It might give a direction to work on
Let me know if you have any questions!
Best
Olha
Project file