Ejemplo n.º 1
0
    private void DrawSizeStats()
    {
        PixelPerfectCameraCustome myCamera = (PixelPerfectCameraCustome)target;

        EditorGUI.BeginDisabledGroup(true);

        // Size
        {
            GUIStyle style = new GUIStyle(GUI.skin.label);
            style.richText = true;
            string width  = string.Format("{0:0.00}", myCamera.cameraSize.x);
            string height = string.Format("{0:0.00}", myCamera.cameraSize.y);
            if (myCamera.contraintUsed == PixelPerfectCameraCustome.ConstraintType.Horizontal)
            {
                width = makeBold(width);
            }
            else if (myCamera.contraintUsed == PixelPerfectCameraCustome.ConstraintType.Vertical)
            {
                height = makeBold(height);
            }
            EditorGUILayout.LabelField("Size", string.Format("{0} x {1}", width, height), style);
        }

        // Pixels Per Unit
        {
            string ppuString     = string.Format("{0:0.00}", myCamera.cameraPixelsPerUnit);
            string tooltipString = "The number of screen pixels a unit is rendered to.";
            EditorGUILayout.LabelField(new GUIContent("Pixels Per Unit", tooltipString), new GUIContent(ppuString));
        }

        // Ratio
        {
            string ratioFormat   = (myCamera.pixelPerfect) ? "{0:0}" : "{0:0.0000}";
            string pixelsString  = string.Format(ratioFormat + "x [{1:0.00} x {2:0.00}]", myCamera.ratio, myCamera.nativeAssetResolution.x, myCamera.nativeAssetResolution.y);
            string tooltipString = "The screen resolution as a multiple of 2 numbers. The first is the number of screen pixels an asset pixel will render to. The second is the camera resolution in asset pixels.";
            EditorGUILayout.LabelField(new GUIContent("Pixels", tooltipString), new GUIContent(pixelsString));
        }

        // Target Coverage
        {
            string percentageUsed = string.Format("{0:P2}", myCamera.fovCoverage);
            string tooltipString  = "How much of the targeted camera size was covered.";
            EditorGUILayout.LabelField(new GUIContent("Coverage", tooltipString), new GUIContent(percentageUsed));
        }

        EditorGUI.EndDisabledGroup();
    }
    void Initialize(bool warn)
    {
        #if UNITY_EDITOR
        if (!gameObject.activeInHierarchy)
        {
            return;
        }
        #endif

        _canvas = GetComponent <Canvas> ();

        if (_canvas.renderMode != RenderMode.WorldSpace)
        {
            Debug.Log("Render mode: " + _canvas.renderMode + " is not supported by CanvasWorldScaler");
            return;
        }

        if (uiCamera == null)
        {
            if (warn)
            {
                Debug.Log("You have to assign a UI camera!");
            }
            return;
        }

        _pixelPerfectCamera = uiCamera.GetComponent <PixelPerfectCameraCustome> ();

        if (_pixelPerfectCamera == null)
        {
            if (warn)
            {
                Debug.Log("You have to use the PixelPerfectCamera script on the assigned UI camera!");
            }
            return;
        }

        _isInitialized = true;

        AdjustCanvas();
    }
Ejemplo n.º 3
0
    void OnWillRenderObject()
    {
        //Debug.Log("on will" + Camera.current);
        Camera cam = Camera.current;

        if (!cam)
        {
            return;
        }

        PixelPerfectCameraCustome pixelPerfectCamera = cam.GetComponent <PixelPerfectCameraCustome>();
        bool retroSnap = (pixelPerfectCamera == null) ? false : pixelPerfectCamera.retroSnap;

#if !REDUCE_JITTER
        if (!retroSnap)
        {
            return;
        }
#endif

        shouldRestorePosition = true;
        actualPosition        = transform.position;

        float cameraPPU = (float)cam.pixelHeight / (2f * cam.orthographicSize);
        float cameraUPP = 1.0f / cameraPPU;

        Vector2 camPos = cam.transform.position.xy();
        Vector2 pos    = actualPosition.xy();
        Vector2 relPos = pos - camPos;

        Vector2 offset = new Vector2(0, 0);
        // offset for screen pixel edge if screen size is odd
        offset.x = (cam.pixelWidth % 2 == 0) ? 0 : 0.5f;
        offset.y = (cam.pixelHeight % 2 == 0) ? 0 : 0.5f;
        // offset for pivot in Sprites
        Vector2 pivotOffsetInt  = new Vector2(0, 0);
        Vector2 pivotOffsetFrac = new Vector2(0, 0);
        if (sprite != null)
        {
            Vector2 pivotOffset = sprite.pivot - new Vector2(Mathf.Floor(sprite.pivot.x), Mathf.Floor(sprite.pivot.y)); // the fractional part in texture pixels
            if (retroSnap)
            {
                pivotOffsetFrac = pivotOffset;
            }
            else
            {
                float camPixelsPerAssetPixel = cameraPPU / sprite.pixelsPerUnit;
                pivotOffset    *= camPixelsPerAssetPixel;                                                              // in screen pixels
                pivotOffsetFrac = pivotOffset - new Vector2(Mathf.Floor(pivotOffset.x), (Mathf.Floor(pivotOffset.y))); // fract part in screen pixels
                pivotOffsetInt  = pivotOffset - pivotOffsetFrac;                                                       // integer part in screen pixels
                                                                                                                       // We subtract the integer part so that the anchor point snaps to the texel's edge
            }
        }
        if (retroSnap)
        {
            float assetPPU = pixelPerfectCamera.assetsPixelsPerUnit;
            float assetUPP = 1.0f / assetPPU;
            float camPixelsPerAssetPixel = cameraPPU / assetPPU;

            offset.x /= camPixelsPerAssetPixel; // zero or half a screen pixel in texture pixels
            offset.y /= camPixelsPerAssetPixel;
            relPos.x  = (Mathf.Round(relPos.x / assetUPP - offset.x - pivotOffsetFrac.x) + offset.x + pivotOffsetFrac.x) * assetUPP;
            relPos.y  = (Mathf.Round(relPos.y / assetUPP - offset.y - pivotOffsetFrac.y) + offset.y + pivotOffsetFrac.y) * assetUPP;
        }
        else
        {
            // Convert the units to pixels, round them, convert back to units. The offsets make sure that the distance we round is from screen pixel (fragment) edges to texel edges.
            relPos.x = (Mathf.Round(relPos.x / cameraUPP - offset.x - pivotOffsetFrac.x) + offset.x + pivotOffsetFrac.x + pivotOffsetInt.x) * cameraUPP;
            relPos.y = (Mathf.Round(relPos.y / cameraUPP - offset.y - pivotOffsetFrac.y) + offset.y + pivotOffsetFrac.y + pivotOffsetInt.y) * cameraUPP;
        }

        pos = relPos + camPos;

        transform.position = new Vector3(pos.x, pos.y, actualPosition.z);
    }