Unity 图片动态修改Pivot并进行缩放

 /// 
    /// 更新位置
    /// 
    public void Update_scale()
    {
        if (main_lines != null)
        {
            main_lines.rectTransform.localScale += Vector3.one * Input.GetAxis("Mouse ScrollWheel");

            if(Input.GetAxis("Mouse ScrollWheel") != 0)
            {
               
                float OffsetX = Input.mousePosition.x - main_lines.transform.position.x;
                float OffsetY = Input.mousePosition.y - main_lines.transform.position.y;

                float PivotX = OffsetX / main_lines.rectTransform.rect.width / main_lines.transform.localScale.x;
                float PivotY = OffsetY / main_lines.rectTransform.rect.height / main_lines.transform.localScale.y;


               

                main_lines.rectTransform.pivot += new Vector2(PivotX, PivotY);
                main_lines.rectTransform.position += new Vector3(OffsetX, OffsetY, 0);

            }
            if (main_lines.rectTransform.localScale.x > 4)
            {
                main_lines.rectTransform.localScale = Vector3.one * 4;
            }
            if (main_lines.rectTransform.localScale.x <= 1)
            {
                main_lines.rectTransform.localScale = Vector3.one;
            }
        }
    }

你可能感兴趣的:(ui,unity)