elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

dynamic lines

16 Jul 2015, 16:32

In Unity, what would be the best way to procedurally add dynamic lines to an xaml file? Some lines in the xaml file will be static (or just rotated), but others must be fully generated at runtime.

This is for aircraft display simulation:
https://www.youtube.com/watch?v=hlNAIt1NeO4

I suppose it is the same as this, but in C#:
http://www.noesisengine.com/docs/Gui.Core._Path.html

I have this code:
using UnityEngine;
using System.Collections;
using Noesis;

	public class ND : MonoBehaviour {

	void Start (){
		
		NoesisGUIPanel panel = GetComponent<NoesisGUIPanel>();
		FrameworkElement root = panel.GetContent();
		root.Parent.UseLayoutRounding = false; 

		UIElement lineRoot = (UIElement)root.FindName("path4137");
		lineRoot.RenderTransformOrigin = new Point(0.5f, 0.5f);
		TransformGroup group = new TransformGroup();

		Line line = new Line();
		Thickness thickness = new Thickness(101,-11,362,250);
		line.Margin = thickness;
		line.StrokeThickness = 4;
		line.X1 = 10;
		line.X2 = 40;
		line.Y1 = 70;
		line.Y2 = 70;

		group.Children.Add(line);
		lineRoot.RenderTransform = group;	
	}
}
But it throws this error:
ApplicationException: Non Freezable item added to FreezableCollection
Noesis.FreezableCollection.Add (System.Object item) (at Assets/Plugins/NoesisGUI/Scripts/Proxies/FreezableCollection.cs:94)
ND.Start () (at Assets/Scripts/ND.cs:29)
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: dynamic lines

17 Jul 2015, 08:11

Ok, I got it to work:
using UnityEngine;
using System.Collections;
using Noesis;

	public class ND : MonoBehaviour {

	Line line;

	void Start (){
		
		NoesisGUIPanel panel = GetComponent<NoesisGUIPanel>();
		FrameworkElement root = panel.GetContent();
		root.Parent.UseLayoutRounding = false; 
		Noesis.Canvas canvas = (Noesis.Canvas)root.FindName("layer1");

		line = new Line();
		line.StrokeThickness = 2f;
		line.X1 = 10;
		line.X2 = 40;
		line.Y1 = 70;
		line.Y2 = 5;
		line.Stroke = new SolidColorBrush(Colors.Blue);

		canvas.Children.Add(line);
	}

	void Update(){

		line.Y2 ++;
	}
}
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: dynamic lines

17 Jul 2015, 11:58

Hi,

If lines have different stroke styles, then adding different Line shape objects to a Canvas like you did is fine.

But when lines share the same stroke style, you can optimize the render by using a single Path object with a StreamGeometry you can update procedurally:
StreamGeometry _lines = new StreamGeometry();
Path path = new Path()
{
    Data = _lines,
    Stroke = Brushes.Blue,
    StrokeThickness = 2.0f
};
//... then update lines when needed
using (var context = _lines.Open())
{
    context.BeginFigure(new Point(0, 0), false);
    context.LineTo(new Point(10, 10));
    context.BeginFigure(new Point(0, 0), false);
    context.LineTo(new Point(0, 20));
} 
 
elecman
Topic Author
Posts: 90
Joined: 20 Jul 2014, 04:28

Re: dynamic lines

17 Jul 2015, 16:02

The strokes for all segments are the same, so I will go with the StreamGeometry.
Thanks for the suggestion :-)

Who is online

Users browsing this forum: No registered users and 1 guest