Exemple #1
0
    public BoardSlot[,] BuildBoard(int p_size, Action <int, int> p_callback)
    {
        BoardSlot[,] result = new BoardSlot[p_size, p_size];

        Rect gamePanelRect = RectTransformUtility.PixelAdjustRect(_gamePanelRT, _gameCanvas);

        float panelWidth  = gamePanelRect.width;
        float panelHeight = gamePanelRect.height;

        _gamePanelGridLayout.cellSize = new Vector2(panelWidth / p_size, panelHeight / p_size);

        for (int i = 0; i < p_size; i++)
        {
            for (int j = 0; j < p_size; j++)
            {
                BoardSlot newSlot = Instantiate(SlotPrefab);
                newSlot.Init(new Vector2Int(i, j), p_callback);
                newSlot.transform.SetParent(GamePanel.transform, false);
                newSlot.gameObject.SetActive(true);

                result[i, j] = newSlot;
            }
        }

        return(result);
    }
        public static Rect RectTransformToScreenRect(this GameObject uiGameObject)
        {
            RectTransform yourRectTransform = uiGameObject.GetComponent <RectTransform> ();
            Canvas        root = yourRectTransform.transform.transform.root.GetComponent <Canvas> ();

            return(RectTransformUtility.PixelAdjustRect(yourRectTransform, root));
        }
Exemple #3
0
 private void OnRectTransformDimensionsChange()
 {
     if (isInitiated)
     {
         rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);
     }
 }
Exemple #4
0
    void Start()
    {
        CenterImage();
        //Get Rect position from bottom left of scren, and pixel size, for converting mouse coords from screen to texture coords
        rect_image_BL   = RectTransformUtility.PixelAdjustRect(image.rectTransform, image.transform.root.GetComponent <Canvas>());
        rect_image_BL.x = image.rectTransform.offsetMin.x;
        rect_image_BL.y = image.rectTransform.offsetMin.y;

        rendertex = (RenderTexture)image.texture;

        aspect_ratio         = rendertex.width / rendertex.height;
        brush_size_UV_half_x = brush_size / ((float)rendertex.width);
        brush_size_UV_half_y = brush_size / ((float)rendertex.height);

        material = new Material(Shader.Find("Custom/BrushShader"));
        material.SetTexture("_MainTex", this.rendertex);
        material.SetColor("_BrushColor", color);

        //brush size
        material.SetFloat("_Brush_size_UV_half_x", brush_size_UV_half_x);
        material.SetFloat("_Brush_size_UV_half_y", brush_size_UV_half_y);
        InitializeShaderArray();

        image.material = material;
    }
Exemple #5
0
    public void UpdateInventoryUI()
    {
        containerRect = RectTransformUtility.PixelAdjustRect(gridContainer, HUDCanvas);

        GameObject ItemUI;

        foreach (ItemBase invItem in inventory.inventoryList)
        {
            ItemUI = Instantiate(inventoryItemUIPrefab, gridContainer, false);

            float xpos = invItem.inventoryPosition.x * GridCellSize + Mathf.Max(invItem.inventoryPosition.x - 1, 0) * gridLayout.spacing.x; //Have to include the spacing between different cells for positioning.
            float ypos = invItem.inventoryPosition.y * GridCellSize + Mathf.Max(invItem.inventoryPosition.y - 1, 0) * gridLayout.spacing.y;

            ItemUI.transform.localPosition = new Vector2(xpos, -ypos); //The ypos is negative here due to unity being silly.

            float xSize = invItem.inventoryDimensions.x * GridCellSize + Mathf.Max(invItem.inventoryDimensions.x - 1, 0) * gridLayout.spacing.x;
            float ySize = invItem.inventoryDimensions.y * GridCellSize + Mathf.Max(invItem.inventoryDimensions.y - 1, 0) * gridLayout.spacing.y;

            ItemUI.GetComponent <RectTransform>().sizeDelta       = new Vector2(xSize, ySize);
            ItemUI.GetComponent <InventoryItemUI>().parentItem    = invItem;
            ItemUI.GetComponent <InventoryItemUI>().invDimensions = invItem.inventoryDimensions;
            ItemUI.GetComponent <InventoryItemUI>().invPosition   = invItem.inventoryPosition;
            ItemUI.GetComponent <InventoryItemUI>().invPanel      = this;
        }
    }
    public void UpdateTransformElements()
    {
        rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);

        var vector2Zero = new Vector2(0, 1);

        for (int i = 0; i < rectTransforms.Count; i++)
        {
            rectTransforms[i].anchorMax = vector2Zero;
            rectTransforms[i].anchorMin = vector2Zero;
        }

        Debug.Log(rect.width, this);

        float centerOffset = rect.width / 2.0f;
        float heightOffset = 0;

        for (int i = 0; i < rectTransforms.Count; i++)
        {
            rectTransforms[i].localPosition = new Vector3(0, heightOffset /*+ rectTransforms[i].rect.width / 2.0f*/);
            heightOffset -= rectTransforms[i].sizeDelta.y;
        }


        for (int i = 0; i < rectTransforms.Count; i++)
        {
            rectTransforms[i].sizeDelta = new Vector2(rect.width, rectTransforms[i].sizeDelta.y);
        }

        for (int i = 0; i < rectTransforms.Count; i++)
        {
            LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
        }
    }
Exemple #7
0
        private float[] GameObjectSizeInScreen(Rect rect, RectTransform rectTransform)
        {
            float[] size = { 0f, 0f };
            if (rectTransform)
            {
                Canvas     rootCanvas = GetRootCanvas(gameObject);
                RenderMode renderMode = rootCanvas != null ? rootCanvas.renderMode : new RenderMode();
                switch (renderMode)
                {
                case RenderMode.ScreenSpaceCamera:
                    Rect _rect = RectTransformUtility.PixelAdjustRect(rectTransform, rootCanvas);
                    size = new float[] {
                        _rect.width *rootCanvas.scaleFactor / (float)Screen.width,
                        _rect.height *rootCanvas.scaleFactor / (float)Screen.height
                    };
                    break;

                case RenderMode.WorldSpace:
                    Rect          rect_           = rectTransform.rect;
                    RectTransform canvasTransform = rootCanvas.GetComponent <RectTransform>();
                    size = new float[] { rect_.width / canvasTransform.rect.width, rect_.height / canvasTransform.rect.height };
                    break;

                default:
                    size = new float[] { rect.width / (float)Screen.width, rect.height / (float)Screen.height };
                    break;
                }
            }
            else
            {
                size = new float[] { rect.width / (float)Screen.width, rect.height / (float)Screen.height };
            }
            return(size);
        }
Exemple #8
0
    ///<summary>
    ///Animates RectTransform from its current position to out of screen in whichever direction specified.
    ///</summary>
    IEnumerator FlyOut(RectTransform rt, string dir)
    {
        //RectTransform objects to store the transforms of the main panel
        var rt_main = pnl_Main.GetComponent <RectTransform>();

        var rect_this = RectTransformUtility.PixelAdjustRect(rt, cv);                           //Rect object to store rt's height and width
        var rect_main = RectTransformUtility.PixelAdjustRect(rt_main, cv);                      //Rect object to store main panel's height and width

        Vector2 startPos = rt.localPosition;                                                    //Start Position is defined as where the panel starts
        Vector2 endPos;                                                                         //declares variable to set later

        dir = dir.ToLower();

        //Some math to figure out the rightPos (offscreen)
        switch (dir)
        {
        case "up":
        case "top":
        case "north":
            endPos.x = 0f;
            endPos.y = rect_main.height + (rect_this.height - rect_main.height) / 2;
            break;

        case "right":
        case "east":
            endPos.x = rect_main.width + (rect_this.width - rect_main.width) / 2;
            endPos.y = 0f;
            break;

        case "down":
        case "bottom":
        case "south":
            endPos.x = 0f;
            endPos.y = -(rect_main.height + (rect_this.height - rect_main.height) / 2);
            break;

        case "left":
        case "west":
            endPos.x = -(rect_main.height + (rect_this.height - rect_main.height) / 2);
            endPos.y = 0f;
            break;

        default:
            endPos.x = 5000f;
            endPos.y = 5000f;
            Debug.Log("FlyOut(): No Valid Direction Declared");
            break;
        }

        for (float i = 0f; i < animTime; i = i + Time.deltaTime)
        {
            var     timePCT    = i / animTime;
            Vector2 currentPos = new Vector2(Mathf.Lerp(startPos.x, endPos.x, easeAccel.Evaluate(timePCT)), Mathf.Lerp(startPos.y, endPos.y, easeAccel.Evaluate(timePCT)));
            rt.localPosition = new Vector2(currentPos.x, currentPos.y);
            yield return(null);
        }

        rt.localPosition = endPos;
    }
 private void OnRectTransformDimensionsChange()
 {
     if (isInitialised)
     {
         rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);
         UpdateScaleBarElement();
     }
 }
Exemple #10
0
 /// <summary>
 ///
 /// <para>
 /// Returns a pixel perfect Rect closest to the Graphic RectTransform.
 /// </para>
 ///
 /// </summary>
 ///
 /// <returns>
 ///
 /// <para>
 /// Pixel perfect Rect.
 /// </para>
 ///
 /// </returns>
 public Rect GetPixelAdjustedRect()
 {
     if (!(bool)((UnityEngine.Object) this.canvas) || !this.canvas.pixelPerfect)
     {
         return(this.rectTransform.rect);
     }
     return(RectTransformUtility.PixelAdjustRect(this.rectTransform, this.canvas));
 }
Exemple #11
0
 public Rect GetPixelAdjustedRect()
 {
     if ((this.canvas != null) && this.canvas.pixelPerfect)
     {
         return(RectTransformUtility.PixelAdjustRect(this.rectTransform, this.canvas));
     }
     return(this.rectTransform.rect);
 }
Exemple #12
0
 public Rect GetPixelAdjustedRect()
 {
     if (!canvas || !canvas.pixelPerfect)
     {
         return(rectTransform.rect);
     }
     return(RectTransformUtility.PixelAdjustRect(rectTransform, canvas));
 }
        public static Rect ProjectToRectTransform(this RectTransform source, RectTransform target, Canvas canvas)
        {
            var screenP = RectTransformUtility.WorldToScreenPoint(Camera.current, source.position);

            RectTransformUtility.ScreenPointToLocalPointInRectangle(target, screenP, Camera.current, out var localPoint);
            target.anchoredPosition += localPoint;
            return(RectTransformUtility.PixelAdjustRect(target, canvas));
        }
Exemple #14
0
    // Start is called before the first frame update
    void Start()
    {
        var vXBounds       = xBounding.GetComponent <RectTransform>();
        var vYBottomBounds = yBounding.GetComponent <RectTransform>();

        xBoundingTransform = RectTransformUtility.PixelAdjustRect(vXBounds, xBounding);
        yBoundingTransform = RectTransformUtility.PixelAdjustRect(vYBottomBounds, yBounding);
    }
Exemple #15
0
 public Rect GetPixelAdjustedRect()
 {
     if (!canvas || canvas.renderMode == RenderMode.WorldSpace || canvas.scaleFactor == 0f || !canvas.pixelPerfect)
     {
         return(rectTransform.rect);
     }
     return(RectTransformUtility.PixelAdjustRect(rectTransform, canvas));
 }
    private void OnRectTransformDimensionsChange()
    {
        //rsize button to fit
        float   width   = RectTransformUtility.PixelAdjustRect(GetComponent <RectTransform>(), FindObjectOfType <Canvas>()).width - 32;
        Vector2 newSize = new Vector2(width / 4, width / 4);

        gridGroup.cellSize = newSize;
    }
        private IEnumerator GetStartupSize()
        {
            yield return(new WaitForEndOfFrame());

            var pixelRect = RectTransformUtility.PixelAdjustRect(consoleOutputRect, testCanvas);

            textOutputWidth  = pixelRect.width;
            textOutputHeight = pixelRect.height;
        }
Exemple #18
0
    public static Vector2 ActualSize(this RectTransform trans)
    {
        //var v = new Vector3[4];
        //trans.GetWorldCorners(v);
        //method one
        //return new Vector2(v[3].x - v[0].x, v[1].y - v[0].y);

        //method two
        return(RectTransformUtility.PixelAdjustRect(trans, trans.GetComponentInParent <Canvas>()).size);
    }
    private void UpdateLayoutGroup()
    {
        var scrollBarRect = RectTransformUtility.PixelAdjustRect(scrollBarRectTransform, parentCanvas);

        itemListLayoutGroup.padding.right = (int)scrollBarRect.width;

        itemListLayoutGroup.padding.top = (int)(rect.height * 0.35f);

        parentCanvas.sortingOrder = (int)rectTransform.position.y;
    }
        protected override Vector2 queryWindowSize()
        {
            var rect = RectTransformUtility.PixelAdjustRect(this._widgetCanvas.rectTransform,
                                                            this._widgetCanvas.canvas);
            var size = new Vector2(rect.width, rect.height) / this._widgetCanvas.devicePixelRatio;

            size.x = Mathf.Round(size.x);
            size.y = Mathf.Round(size.y);
            return(size);
        }
 private void OnRectTransformDimensionsChange()
 {
     if (isInitialised)
     {
         _rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);
         UpdateHierarchyObjectElementWidths(hierarchyBaseElement.parentElements);
         RecaculateHierarchyObjectElements(hierarchyBaseElement.parentElements);
         //UpdateHierarchyObjectElements();
     }
 }
Exemple #22
0
        public Vector2 GetRtSize(RectTransform trans, Canvas canvas)
        {
            // var v = new Vector3[4];
            // trans.GetWorldCorners(v);
            //method one
            //return new Vector2(v[3].x - v[0].x, v[1].y - v[0].y);

            //method two
            return(RectTransformUtility.PixelAdjustRect(trans, canvas).size);
        }
    public void onResizeWindow()
    {
        //updates the contents when the window has changes size
        //fix zooming on y

        Rect r = RectTransformUtility.PixelAdjustRect(this.rectTransform, this.canvas);

        //Debug.Log (r);
        this.data.Contents.changeWindowSize((int)r.width, (int)r.height);
    }
    static int PixelAdjustRect(IntPtr L)
    {
        LuaScriptMgr.CheckArgsCount(L, 2);
        RectTransform arg0 = (RectTransform)LuaScriptMgr.GetUnityObject(L, 1, typeof(RectTransform));
        Canvas        arg1 = (Canvas)LuaScriptMgr.GetUnityObject(L, 2, typeof(Canvas));
        Rect          o    = RectTransformUtility.PixelAdjustRect(arg0, arg1);

        LuaScriptMgr.PushValue(L, o);
        return(1);
    }
Exemple #25
0
 private Rect GetPixelAdjustedRect(RectTransform rect)
 {
     if (!canvas || canvas.renderMode == RenderMode.WorldSpace || canvas.scaleFactor == 0.0f || !canvas.pixelPerfect)
     {
         return(rect.rect);
     }
     else
     {
         return(RectTransformUtility.PixelAdjustRect(rect, canvas));
     }
 }
Exemple #26
0
    // Load the library.
    void Start()
    {
        gl = new GestureLibrary(libraryToLoad, forceCopy);

        if (gestureLimitType == GestureLimitType.RectBoundsClamp)
        {
            parentCanvas               = gestureLimitRectBounds.GetComponentInParent <Canvas>();
            gestureLimitRect           = RectTransformUtility.PixelAdjustRect(gestureLimitRectBounds, parentCanvas);
            gestureLimitRect.position += new Vector2(gestureLimitRectBounds.position.x, gestureLimitRectBounds.position.y);
        }
    }
    private void OnRectTransformDimensionsChange()
    {
        if (isInitialized)
        {
            rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);

            UpdateDropdownElements();

            UpdateLayoutGroup();
        }
    }
Exemple #28
0
 // Use this for initialization
 void Start()
 {
     Vector3[] corners = new Vector3[4];
     gameObject.GetComponent <RectTransform>().GetWorldCorners(corners);
     foreach (Vector3 corner in corners)
     {
         Debug.Log(corner);
     }
     Debug.Log(RectTransformUtility.PixelAdjustRect(gameObject.GetComponent <RectTransform>(), transform.parent.GetComponent <Canvas>()));
     Debug.Log(gameObject.GetComponent <RectTransform>().sizeDelta);
     Debug.Log(gameObject.GetComponent <RectTransform>().rect);
 }
        private void Awake()
        {
            hierarchyBaseElement = GetComponentInParent <HierarchyBaseElementV2>();

            parentCanvas = GetComponentInParent <Canvas>();

            rectTransform = GetComponent <RectTransform>();

            _rect = RectTransformUtility.PixelAdjustRect(rectTransform, parentCanvas);

            isInitialised = true;
        }
Exemple #30
0
    public void Start()
    {
        var Target        = TargetCanvas.transform.GetChild(0).GetComponent <RectTransform>();
        var Size          = RectTransformUtility.PixelAdjustRect(Target, GetComponent <Canvas>());
        var EffectiveSize = new Vector2(Size.width, Size.height) * (new Vector2(Screen.width, Screen.height) / GetCanvasTargetRes());
        var Tex           = new RenderTexture(Mathf.FloorToInt(EffectiveSize.x), Mathf.FloorToInt(EffectiveSize.y), 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.sRGB);

        Tex.Create();
        Tex.filterMode = FilterMode.Point;
        GetComponent <Camera>().targetTexture = Tex;
        this.rt = Tex;
        StartCoroutine(TurnOff());
    }