using UnityEngine;
using UnityEditor;

[ExecuteInEditMode] 
public class ObliqueProjection : MonoBehaviour {

    [Range(-1f, 1f)]
    public float horizontal;
    [Range(-1f, 1f)]
    public float vertical;
    public float distance;
    private Vector3 orgPosition;

    void OnEnable(){
        EditorApplication.update += EditorUpdate;
        orgPosition = transform.localPosition;
    }

   void OnDisable(){
        EditorApplication.update -= EditorUpdate;
    }

    void EditorUpdate(){
        var cam = GetComponent<Camera>();
        var proj = cam.projectionMatrix;
        proj.m02 = horizontal;
        proj.m12 = vertical;
        cam.projectionMatrix = proj;
        var tan = Mathf.Tan(cam.fieldOfView * 0.5f * Mathf.Deg2Rad);
        var rect = cam.pixelRect;
        var ratio = (float)rect.width / (float)rect.height;
        transform.localPosition = orgPosition + new Vector3(-horizontal * distance * tan * ratio, -vertical * distance * tan, 0f);
    }
}
