public void OnEnable()
        {
            var root = this.GetRootVisualContainer();

            root.AddStyleSheetPath("styles");

            // Here we just take all layout properties and other to extract them in USS!
            var boxes = new VisualContainer()
            {
                name = "boxesContainer"
            };

            boxes.AddToClassList("horizontalContainer");
            root.AddChild(boxes);

            for (int i = 0; i < m_Colors.Length; i++)
            {
                Color c = m_Colors[i];

                // inform layout system of desired width for each box
                boxes.AddChild(new VisualElement()
                {
                    backgroundColor = c
                });
            }

            // Some more advanced layout now!
            var twoPlusOneContainer = new VisualContainer()
            {
                name = "2Plus1Container"
            };

            twoPlusOneContainer.AddToClassList("horizontalContainer");
            root.AddChild(twoPlusOneContainer);
            twoPlusOneContainer.AddChild(new VisualElement()
            {
                name = "large"
            });
            twoPlusOneContainer.AddChild(new VisualElement()
            {
                name = "small"
            });

            var wrapContainer = new VisualContainer()
            {
                name = "wrapContainer"
            };

            wrapContainer.AddToClassList("horizontalContainer");
            root.AddChild(wrapContainer);

            for (int i = 0; i < 20; i++)
            {
                wrapContainer.AddChild(new VisualElement());
            }
        }
        public VisualContainer CreateTask(string name)
        {
            var task = new VisualContainer();

            task.name = name;
            task.AddToClassList("task");

            var taskName = new Toggle(() => {})
            {
                text = name, name = "checkbox"
            };

            task.AddChild(taskName);

            var taskDelete = new Button(() => task.parent.RemoveChild(task))
            {
                name = "delete", text = "Delete"
            };

            task.AddChild(taskDelete);

            return(task);
        }
        public void RecursiveSetUp(IEnumerable <GameObject> gameObjects, VisualContainer container)
        {
            var currentGOs = new List <GameObject>();

            container.ToList().ForEach((e) => {
                GameObject go = e.data as GameObject;

                // remove gone objects
                if (go == null || !gameObjects.Contains(go))
                {
                    container.RemoveChild(e);
                }
                // or-remember those that already have an item
                else
                {
                    currentGOs.Add(go);
                }
            });


            foreach (GameObject go in gameObjects.Except(currentGOs))
            {
                VisualContainer added = asset.CloneTree();
                added.data = go;

                UpdateName(added, go);
                added.RegisterWatch(go, UpdateName);
                UpdateChildren(added, go.transform);
                added.RegisterWatch(go.transform, UpdateChildren);

                added.Q <Toggle>().OnToggle(() =>
                {
                    OnToggle(added);
                });

                container.AddChild(added);
            }

            container.Sort((a, b) => {
                Transform aTrans = (a.data as GameObject).transform;
                Transform bTrans = (b.data as GameObject).transform;
                Debug.Assert(aTrans.parent == bTrans.parent);
                int order = aTrans.GetSiblingIndex() - bTrans.GetSiblingIndex();
                return(order);
            });
        }
        public void OnEnable()
        {
            // Each editor window contains a root VisualContainer object
            VisualContainer root = this.GetRootVisualContainer();

            // VisualContainer objects can contain VisualElement objects,
            // which is the base class for VisualContainer and other controls
            VisualContainer boxes = new VisualContainer();

            root.AddChild(boxes);

            // The most basic way to place an element is to assign its rect
            // although you should prefer layout in most cases
            boxes.layout = new Rect(
                kMargin,
                kMargin,
                kPadding * 2 + kBoxSize * m_Colors.Length,
                kPadding * 2 + kBoxSize
                );

            // The VisualTree is painted back-to-front following depth first traversal
            // thus a parent paints before its children
            boxes.backgroundColor = Color.grey;

            // A VisualContainer will clip its descendants outside of its own
            // rect based on this property
            boxes.clipChildren = true;

            for (int i = 0; i < m_Colors.Length; i++)
            {
                Color c = m_Colors[i];
                // position rects are relative to the parent rect
                boxes.AddChild(new VisualElement()
                {
                    layout          = new Rect(kPadding + i * kBoxSize, kPadding, kBoxSize, kBoxSize),
                    backgroundColor = c
                });
            }
        }
示例#5
0
        public void OnEnable()
        {
            var root = this.GetRootVisualContainer();

            // Let's now try to do an example similar to the 1st example
            var boxes = new VisualContainer();

            boxes.marginLeft   = kMargin;
            boxes.marginTop    = kMargin;
            boxes.marginRight  = kMargin;
            boxes.marginBottom = kMargin;
            root.AddChild(boxes);

            // By control layout parameters we can simply stack boxes horizontally
            boxes.backgroundColor = Color.grey;
            boxes.paddingLeft     = kPadding;
            boxes.paddingTop      = kPadding;
            boxes.paddingRight    = kPadding;
            boxes.paddingBottom   = kPadding;
            boxes.alignSelf       = Align.FlexStart;
            boxes.flexDirection   = FlexDirection.Row; // makes the container horizontal


            for (int i = 0; i < m_Colors.Length; i++)
            {
                Color c = m_Colors[i];

                // inform layout system of desired width for each box
                boxes.AddChild(new VisualElement()
                {
                    width           = kBoxSize,
                    height          = kBoxSize,
                    backgroundColor = c
                });
            }

            // Some more advanced layout now!
            var twoPlusOneContainer = new VisualContainer();

            twoPlusOneContainer.marginLeft   = kMargin;
            twoPlusOneContainer.marginTop    = kMargin;
            twoPlusOneContainer.marginRight  = kMargin;
            twoPlusOneContainer.marginBottom = kMargin;
            root.AddChild(twoPlusOneContainer);

            // Example of flexibles elements with 70%-30% distribution
            // this is possible thanks to the "flex" property
            twoPlusOneContainer.height        = 100;
            twoPlusOneContainer.alignSelf     = Align.FlexStart;
            twoPlusOneContainer.flexDirection = FlexDirection.Row;
            twoPlusOneContainer.AddChild(new VisualElement()
            {
                flex            = 0.7f,
                backgroundColor = Color.red
            });
            twoPlusOneContainer.AddChild(new VisualElement()
            {
                flex            = 0.3f,
                backgroundColor = Color.blue
            });

            var wrapContainer = new VisualContainer();

            wrapContainer.marginLeft   = kMargin;
            wrapContainer.marginTop    = kMargin;
            wrapContainer.marginRight  = kMargin;
            wrapContainer.marginBottom = kMargin;
            root.AddChild(wrapContainer);

            // Example of an horizontal container that wraps its contents
            // over several lines depending on available space
            wrapContainer.flexWrap      = Wrap.Wrap;
            wrapContainer.flexDirection = FlexDirection.Row;


            for (int i = 0; i < 20; i++)
            {
                wrapContainer.AddChild(new VisualElement()
                {
                    width           = 20,
                    height          = 20,
                    marginLeft      = 5,
                    marginTop       = 5,
                    marginRight     = 5,
                    marginBottom    = 5,
                    backgroundColor = Color.blue
                });
            }
        }