How do I change my material's shader property at runtime - c#

I have a material whose shader has a opacity property which I want to change at runtime. How do I get the reference to that property.

This is done via Material.SetFloat.
You'll need to use the actual property reference name as key, not the Inspector display name, e.g.

Related

ShaderGraph Material Color change

I made a ShaderGraph Shader, that I applied to a material. In the SHader, I put in "public" a variable Color in order to change the material color. From the inspector, it do work if it is not runtime ;
I would like to change this material color depending on different elements during the game. The Material is applied to a lineRenderer. I tried many ways, but it never change color ;
GetComponent<EnemyType>().color; return a RGBA color. I tried :
LaserLine.material.color = GetComponent<EnemyType>().color;
LaserLine.startColor = GetComponent<EnemyType>().color;
LaserLine.endColor = GetComponent<EnemyType>().color;
LaserLine.material.SetColor("_Color", GetComponent<EnemyType>().color);
What Am I doing wrong ?
Thanks
As you are using a custom shader, properties might have different names
Based on the screenshot in your other question your color property is named "Color" not "_Color", though it's hard to see (because for some reason you scaled the images for that question) and I don't know what kind of code ShaderGraph actually outputs (e.g. it could just be a color node and its actual internal name is something like "_Node-2389").
If you hit this button and then "edit shader" it'll show you some information about the shader's properties, including the true string representation of each one.
Like this:
Ok I found out,
It is not the name of the property that count but the reference. To see it, go inside ShaderGraph, and click on the property, you will find the reference name on the property parameters (top left) :
Change the parameter value using the reference trough code like this
material.SetColor("_Color", new Color());

How to make `Label`'s font size inside `ViewCell` dependent on the device in Xamarin Forms?

So I have a few custom cells using ViewCell in my app. Inside my ViewCell are Labels. I want the font size of those labels to be dependent on the text size of the device just like how the TextCell's text changes when you adjust the text size of your device.
Below image is using a built-in TextCell.
Below image is using a custom cell ViewCell
Im testing this in an iOS device and the text size is set to the smallest available. Whenever I change the device text size the cells using TextCells will just automatically changed. The labels inside the ViewCell doesn't change at all. Any suggestion pointing me to the right direction is very much appreciated.
Short answer
The TextCell uses the native UITableViewCell on iOS. This native view takes the device accessibility settings into account. When you use the Xamarin Forms Label, it does not. Xamarin Forms does not support iOS Dynamic Type (yet).
Explanation
The Forms Label uses the FontSizeConverter to set the actual font size. This font size defaults to the value -1 if you don't set any value yourself. The value -1 results in the use of NamedSize.Default (see the Font class).
When you take a look at the FontSizeConverter, if it can't parse the value as an absolute value, it will try to convert to a NamedSize value. In that case, it will use Device.GetNamedSize to get the actual size. This is done through the IPlatformServices interface, which converts these to absolute values (see the IOSPlatformServices).
There is also a FontExtensions class, that implements the same conversion from Named Size to an absolute value. Not very DRY at first sight, but there could be a good reason for this, that I'm not aware of.
Unfortunately, both the FontExtensions and the IOSPlatformServices don't take the accessibility options of iOS into account, but just return absolute values.
Possible solution
On iOS, you could use UIKit.UIApplication.SharedApplication.PreferredContentSizeCategory to get the current accessibility category. Based on this value, you could determine a scale for your fonts (use DynamicResource for the font sizes if you use them in XAML, so they can change at runtime).
Or take a look at SushiHangover's answer on a similar question.
See the preferredContentSizeCategory on the Apple Developer API reference.

C# "new" command

I got a question, when I try to relocate a panel e.g. named pPan, I got 2 different ways to do that:
Inside a click event of a button I write
pPan.Location = new Point (
pPan.Location.X +10 , pPan.Location.Y );
Or I can write:
pPan.Location.X += 10;
So what is the difference here? Why do I create this new Point?
The Location property has type Point which is a struct. you may receive error like
Cannot modify the return value of
'System.Windows.Forms.Control.Location' because it is not a variable
when you set X, Y values of it.
but you can asign new Point object:
pPan.Location = new Point (
pPan.Location.X +10 , pPan.Location.Y );
Control.Location Property
Because the Point class is a value type (Structure in Visual Basic,
struct in Visual C#), it is returned by value, meaning accessing the
property returns a copy of the upper-left point of the control. So,
adjusting the X or Y properties of the Point returned from this
property will not affect the Left, Right, Top, or Bottom property
values of the control. To adjust these properties set each property
value individually, or set the Location property with a new Point. If
the Control is a Form, the Location property value represents the
upper-left corner of the Form in screen coordinates.
Related SO Question : C# Change the location of an object programmatically
If Point is a struct, you must use first one, because when you call the property, it returns a copy of the Location, and you try to modify the copy.
The difference is that in first way you allocate new memory for Point object. second way you are only change the property of existing object.
It is a good practice to operate existing objects as much as possible. So i suggest you use the second way to change location.

Is it possible to extend an existing TypeConverter?

I have made a control in silverlight that has a SolidColorBrush property. In XAML, you can set this to be preexisting Colors such as Black, Yellow, White, etc. I assume there is a TypeConverter that takes the string "Black" and converts it to a Color object and creates a new SolidColorBrush based on this object. I want this functionality to remain, but with the added capability to also accept hex strings to set the color. I would need a TypeConverter for this, correct? Is there a way I can extend from the current one so that I can still pass in color names?
XAML already accepts hex. Eg:
Background="#FF9D661C"
Having the VS Properties pane open while on a XAML element helps indicate possibilities thats aren't necessarily obvious via intelisence (Eg setting Background as hex or a gradiant brush).
As #Ricibob said, the existing converter alreadys works how you want it to. In addition, it looks like all of the built in TypeConverters are sealed, so you can't inherit from them (although there's nothing keeping you from using composition to solve the problem).

Best way of dynamically setting color of control at runtime

I currently load controls at run time, and want to set the color, but not a named color.
I was thinking I need to create a color object , set the color of this then assign it, any direction?
Thanks
You can use Color.FromName() method to set colors :
txt.ForeColor = System.Drawing.Color.FromName("#00B300");
txt.ForeColor = System.Drawing.Color.FromName("red");

Categories