Cita de: pioj date=1554147631Lo más parecido que he encontrado es ésto:
https://answers.unity.com/questions/26606/align-with-view-programmatically.html (https://answers.unity.com/questions/26606/align-with-view-programmatically.html)
Ya anduve por ahí, en lo practico no pude lograr nada con lo que se comenta.
Encontré una manera de ejecutar acciones del menú de Unity (https://docs.unity3d.com/ScriptReference/EditorApplication.ExecuteMenuItem.html (https://docs.unity3d.com/ScriptReference/EditorApplication.ExecuteMenuItem.html)), pero lamentablemente la acción que necesito depende de una selección manual previa de un objeto de la escena.
Entonces estuve haciendo unas pruebas locas
[MenuItem( "VisualDebugging/Align Camera With Viewport %Q" )]
public static void AlignCameraWithView()
{
Object[] aux = Selection.objects;
GameObject[] newSelection = new GameObject[1] { FindObjectOfType<Camera>().gameObject };
Selection.objects = newSelection;
EditorApplication.ExecuteMenuItem( "GameObject/Align With View" );
Selection.objects = aux;
}
Esto funciona, pero no es nada lindo, técnicamente lo que hace ese método es buscar la cámara de la escena y decirle a Unity que se trata de un GameObject seleccionado por el usuario.
Luego ejecuto el mismo "Align With View" de la interfaz del engine, y para no perder la selección que pudo haber hecho el usuario antes de ejecutar este método, restablezco Selection.objects usando la variable auxiliar (aux).
Lo se, es horrible, sigo haciendo pruebas e investigando...
EDIT:
Y por cierto, el objetivo final es que esto se haga en bucle durante un tiempo en editor mode, de manera que la cámara del juego copie lo que el usuario hace con el viewport.
Bueno, lo logré pero por alguna razón no funciona de forma fluida, dejo el código fuente por si a alguien le interesa encontrarle la vuelta que le falta.
using Unit***itor;
using UnityEngine;
public class ViewportSceneCameraManager: EditorWindow
{
private static Camera auxiliaryCamera;
private RenderTexture renderTexture;
[MenuItem( "Test/Create viewport scene camera" )]
private static void Init()
{
EditorWindow editorWindow = GetWindow( typeof( ViewportSceneCameraManager ) );
editorWindow.autoRepaintOnSceneChange = true;
editorWindow.Show();
}
public void Awake()
{
renderTexture = new RenderTexture( (int)position.width,
(int)position.height,
(int)RenderTextureFormat.ARGB32 );
}
public void OnEnable()
{
CreateAuxiliaryCamera();
}
public void Update()
{
AlignAuxiliaryCameraWithViewport();
if ( auxiliaryCamera != null )
{
auxiliaryCamera.targetTexture = renderTexture;
auxiliaryCamera.Render();
auxiliaryCamera.targetTexture = null;
}
if ( renderTexture.width != position.width ||
renderTexture.height != position.height )
renderTexture = new RenderTexture( (int)position.width,
(int)position.height,
(int)RenderTextureFormat.ARGB32 );
}
private void OnGUI()
{
if ( renderTexture )
GUI.DrawTexture( new