Пример #1
0
        protected override void OnUpdate()
        {
            AddMissingComponentsToCanvas();

            float2 screenSize = UILayoutService.GetScreenSize(this);

            Entities.ForEach((ref UICanvas canvas, ref RectTransform rectTransform, ref NonUniformScale scale,
                              ref Translation localPosition, ref Rotation localRotation) =>
            {
                if (!EntityManager.HasComponent <Camera2D>(canvas.camera))
                {
                    return;
                }

                var camera = EntityManager.GetComponentData <Camera2D>(canvas.camera);

                float cameraScale = (camera.halfVerticalSize * 2.0f) / screenSize.y;
                float canvasScale = GetUICanvasScale(screenSize, ref canvas);
                scale.Value       = canvasScale * cameraScale;

                rectTransform.sizeDelta = screenSize / canvasScale;

                // If the camera is translated or rotated, we need to apply the same transformation on the canvas,
                // so the UI is always in the center.
                AlignCanvasWithCamera(canvas, ref rectTransform, ref localRotation);

                rectTransform.anchoredPosition.x += screenSize.x / 2.0f;
                rectTransform.anchoredPosition.y += screenSize.y / 2.0f;

                // Pivot and anchors never change for the Canvas.
                rectTransform.pivot     = new float2(0.5f, 0.5f);
                rectTransform.anchorMin = float2.zero;
                rectTransform.anchorMax = float2.zero;
            });
        }
 protected override void OnUpdate()
 {
     Entities.ForEach((
                          Entity e, ref Parent node, ref RectTransform rt, ref Sprite2DRenderer sprite,
                          ref Sprite2DRendererOptions sprite2DRendererOptions) =>
     {
         var size = UILayoutService.GetRectTransformSizeOfEntity(this, e);
         sprite2DRendererOptions.size = size;
     });
 }
Пример #3
0
 protected override void OnUpdate()
 {
     Entities.ForEach((Entity e, ref RectTransform rect, ref RectTransformFinalSize size) =>
     {
         var s = UILayoutService.GetRectTransformSizeOfEntity(this, e);
         EntityManager.SetComponentData(e, new RectTransformFinalSize()
         {
             size = s
         });
     });
 }
Пример #4
0
        /**
         * Calculate correct Transform.localPosition based on RectTransform component.
         */
        private float2 GetLocalPosition(
            Entity e, ref RectTransform rectTransform, ref Parent parent)
        {
            var parentSize = UILayoutService.GetRectTransformSizeOfParent(this, parent.Value);
            var size       = UILayoutService.ComputeRectTransformSize(rectTransform.anchorMin, rectTransform.anchorMax,
                                                                      rectTransform.sizeDelta, parentSize);
            var anchorReferencePoint =
                GetAnchorReferencePoint(rectTransform.anchorMin, rectTransform.anchorMax, rectTransform.pivot, parentSize);
            var pivotRealUnits = GetPivotRealUnits(rectTransform.pivot, size);
            var position       = anchorReferencePoint + rectTransform.anchoredPosition - pivotRealUnits;

            float3 scale;

            if (EntityManager.HasComponent <NonUniformScale>(e))
            {
                scale = EntityManager.GetComponentData <NonUniformScale>(e).Value;
            }
            else
            {
                scale = new float3(1, 1, 1);
            }

            quaternion rotation;

            if (EntityManager.HasComponent <Rotation>(e))
            {
                rotation = EntityManager.GetComponentData <Rotation>(e).Value;
            }
            else
            {
                rotation = quaternion.identity;
            }

            position = TransformPosition(position, pivotRealUnits, rotation, scale);

            return(position);
        }