Tuesday, April 16, 2024
HomeSample Page

Sample Page Title


				
					utilizing UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(Camera))]
public class CameraViewportHandler : MonoBehaviour
{
    public enum Constraint { Panorama, Portrait }

    #area FIELDS
    public Colour wireColor = Colour.white;
    public float UnitsSize = 1; // measurement of your scene in unity items
    public Constraint constraint = Constraint.Portrait;
    public static CameraViewportHandler Occasion;
    public new Digicam digicam;

    public bool executeInUpdate;

    non-public float _width;
    non-public float _height;
    //*** backside display screen
    non-public Vector3 _bl;
    non-public Vector3 _bc;
    non-public Vector3 _br;
    //*** center display screen
    non-public Vector3 _ml;
    non-public Vector3 _mc;
    non-public Vector3 _mr;
    //*** prime display screen
    non-public Vector3 _tl;
    non-public Vector3 _tc;
    non-public Vector3 _tr;
    #endregion

    #area PROPERTIES
    public float Width
    {
        get
        {
            return _width;
        }
    }
    public float Top
    {
        get
        {
            return _height;
        }
    }

    // helper factors:
    public Vector3 BottomLeft
    {
        get
        {
            return _bl;
        }
    }
    public Vector3 BottomCenter
    {
        get
        {
            return _bc;
        }
    }
    public Vector3 BottomRight
    {
        get
        {
            return _br;
        }
    }
    public Vector3 MiddleLeft
    {
        get
        {
            return _ml;
        }
    }
    public Vector3 MiddleCenter
    {
        get
        {
            return _mc;
        }
    }
    public Vector3 MiddleRight
    {
        get
        {
            return _mr;
        }
    }
    public Vector3 TopLeft
    {
        get
        {
            return _tl;
        }
    }
    public Vector3 TopCenter
    {
        get
        {
            return _tc;
        }
    }
    public Vector3 TopRight
    {
        get
        {
            return _tr;
        }
    }
    #endregion

    #area METHODS
    non-public void Awake()
    {
        digicam = GetComponent<camera>();
        Occasion = this;
        ComputeResolution();
    }

    non-public void ComputeResolution()
    {
        float leftX, rightX, topY, bottomY;

        if (constraint == Constraint.Panorama)
        {
            digicam.orthographicSize = 1f / digicam.facet * UnitsSize / 2f;
        }
        else
        {
            digicam.orthographicSize = UnitsSize / 2f;
        }

        _height = 2f * digicam.orthographicSize;
        _width = _height * digicam.facet;

        float cameraX, cameraY;
        cameraX = digicam.rework.place.x;
        cameraY = digicam.rework.place.y;

        leftX = cameraX - _width / 2;
        rightX = cameraX + _width / 2;
        topY = cameraY + _height / 2;
        bottomY = cameraY - _height / 2;

        //*** backside
        _bl = new Vector3(leftX, bottomY, 0);
        _bc = new Vector3(cameraX, bottomY, 0);
        _br = new Vector3(rightX, bottomY, 0);
        //*** center
        _ml = new Vector3(leftX, cameraY, 0);
        _mc = new Vector3(cameraX, cameraY, 0);
        _mr = new Vector3(rightX, cameraY, 0);
        //*** prime
        _tl = new Vector3(leftX, topY, 0);
        _tc = new Vector3(cameraX, topY, 0);
        _tr = new Vector3(rightX, topY, 0);
    }

    non-public void Replace()
    {
#if UNITY_EDITOR

        if (executeInUpdate)
            ComputeResolution();

#endif
    }

    void OnDrawGizmos()
    {
        Gizmos.coloration = wireColor;

        Matrix4x4 temp = Gizmos.matrix;
        Gizmos.matrix = Matrix4x4.TRS(rework.place, rework.rotation, Vector3.one);
        if (digicam.orthographic)
        {
            float unfold = digicam.farClipPlane - digicam.nearClipPlane;
            float middle = (digicam.farClipPlane + digicam.nearClipPlane) * 0.5f;
            Gizmos.DrawWireCube(new Vector3(0, 0, middle), new Vector3(digicam.orthographicSize * 2 * digicam.facet, digicam.orthographicSize * 2, unfold));
        }
        else
        {
            Gizmos.DrawFrustum(Vector3.zero, digicam.fieldOfView, digicam.farClipPlane, digicam.nearClipPlane, digicam.facet);
        }
        Gizmos.matrix = temp;
    }
    #endregion

} // class</camera>
			



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments