RenderTransform read and write
In my xaml code I have a RenderTransform which looks like this:
How do I read and write the translation and rotation values in Unity C#?
Edit:
presumably it is something like this:
But I get this error:
Edit 2:
It seems that whether or not you can cast to RotateTransform depends on what is in the xaml file. So I guess I am stuck with using the MatrixTransform. So how do I convert the matrix to a rotation value? Maybe providing a function for that would be helpful.
Code: Select all
<Canvas Name="g4709" RenderTransform="0,1,-1,0,868.716430664063,217.774169921875">
Edit:
presumably it is something like this:
Code: Select all
UIElement pitch = (UIElement)root.FindName("g4709");
RotateTransform pitchRotateTransform = new RotateTransform();
pitchRotateTransform = (RotateTransform)pitch.RenderTransform;
float debug = pitchRotateTransform.Angle;
It does cast to a MatrixTransform, but that doesn't contain easy functions for getting the rotation.InvalidCastException: Cannot cast from source type to destination type.
Edit 2:
It seems that whether or not you can cast to RotateTransform depends on what is in the xaml file. So I guess I am stuck with using the MatrixTransform. So how do I convert the matrix to a rotation value? Maybe providing a function for that would be helpful.
-
sfernandez
Site Admin
- Posts: 2997
- Joined:
Re: RenderTransform read and write
As you found out, if you write in the xaml RenderTransform="0,1,-1,0,868.716430664063,217.774169921875", the XamlReader automatically converts that string to a MatrixTransform object.
A MatrixTransform object provides a Matrix property that gives you access to a matrix (Transform2) object with all the functionality you need:
If you don't like to work with the MatrixTransform object, you can use other alternatives:
CompositeTransform:
TransformGroup with RotateTransform + ScaleTransform + ...:
A MatrixTransform object provides a Matrix property that gives you access to a matrix (Transform2) object with all the functionality you need:
Code: Select all
var matrixTransform = (MatrixTransform)pitch.RenderTransform;
var m = matrixTransform.Matrix; // gets current matrix
m.RotateAt(30.0f, 5.0f, 5.0f); // adds a rotation to current matrix
m = Transform2.Rot(30.0f); // creates a new rotation matrix
matrixTransform.Matrix = m; // update matrix property
CompositeTransform:
Code: Select all
<Canvas Name="g4709">
<Canvas.RenderTransform>
<CompositeTransform
Rotation="0"
ScaleX="1" ScaleY="1"
SkewX="0" SkewY="0"
TranslateX="0" TranslateY="0"/>
</Canvas.RenderTransform>
</Canvas>
Code: Select all
pitch.RenderTransform = new CompositeTransform()
{
Rotation = 0.0f,
ScaleX = 1.0f, ScaleY = 1.0f,
SkewX = 0.0f, SkewY = 0.0f,
TranslateX = 0.0f, TranslateY = 0.0f
};
Code: Select all
<Canvas Name="g4709">
<Canvas.RenderTransform>
<TransformGroup>
<RotateTransform Angle="0"/>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<SkewTransform AngleX="0" AngleY="0"/>
<TranslateTransform X="0" Y="0"/>
</TransformGroup>
</Canvas.RenderTransform>
</Canvas>
Code: Select all
TransformGroup group = new TransformGroup();
group.Children.Add(new RotateTransform { Angle = 0.0f });
group.Children.Add(new ScaleTransform { ScaleX = 0.0f, ScaleY = 0.0f });
group.Children.Add(new SkewTransform { SkewX = 0.0f, SkewY = 0.0f });
group.Children.Add(new TranslateTransform { X = 0.0f, Y = 0.0f });
pitch.RenderTransform = group;
Re: RenderTransform read and write
Ah, the Transform2.Rot() function surely helps
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 3 guests