Creating a custom Editor tool in Unity that modifies the behavior of the Animation Preview panel requires using Unity's Editor scripting API. The following script will create a simple Editor window that allows you to set a default view for the Animation Preview, including setting a specific zoom level, camera angle, and enabling autoplay.
This script assumes you are familiar with Unity's Editor scripting and have a basic understanding of C#.
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
public class AnimationPreviewSettings : EditorWindow
{
// Settings
public float zoom = 1.0f;
public float cameraAngle = 30.0f;
public bool autoPlay = true;
[MenuItem("Tools/Animation Preview Settings")]
public static void ShowWindow()
{
GetWindow<AnimationPreviewSettings>("Animation Preview Settings");
}
void OnEnable()
{
// Load existing settings or set defaults
zoom = EditorPrefs.GetFloat("AnimationPreviewZoom", 1.0f);
cameraAngle = EditorPrefs.GetFloat("AnimationPreviewCameraAngle", 30.0f);
autoPlay = EditorPrefs.GetInt("AnimationPreviewAutoPlay", 1) == 1;
}
void OnDisable()
{
// Save settings
EditorPrefs.SetFloat("AnimationPreviewZoom", zoom);
EditorPrefs.SetFloat("AnimationPreviewCameraAngle", cameraAngle);
EditorPrefs.SetInt("AnimationPreviewAutoPlay", autoPlay ? 1 : 0);
}
void OnGUI()
{
EditorGUI.BeginChangeCheck();
zoom = EditorGUILayout.Slider("Zoom", zoom, 0.1f, 5.0f);
cameraAngle = EditorGUILayout.Slider("Camera Angle", cameraAngle, 0.0f, 180.0f);
autoPlay = EditorGUILayout.Toggle("Auto Play", autoPlay);
if (EditorGUI.EndChangeCheck())
{
ApplySettings();
}
}
void ApplySettings()
{
// Get the current Animation window
EditorWindow animationWindow = GetAnimationWindow();
if (animationWindow != null)
{
// Unfortunately, directly accessing and modifying the Animation Preview's camera and play state isn't straightforward
// through the public API. The following is a workaround and may not be fully reliable or future-proof.
// Zoom
// Note: As of my last update, directly setting zoom through Editor API isn't provided.
// This part is tricky and might require reflection or internal Unity APIs which can break.
// Camera Angle
// Similar to zoom, manipulating the camera angle directly isn't supported through public APIs.
// AutoPlay
if (autoPlay)
{
// Play the animation
// This part also isn't directly supported through public APIs for the Animation Preview.
}
}
}
EditorWindow GetAnimationWindow()
{
// Find the Animation window
foreach (EditorWindow window in EditorWindow.GetWindows())
{
if (window.title.StartsWith("Animation"))
{
return window;
}
}
return null;
}
}
However, there are significant limitations with this script:
-
Zoom and Camera Angle: Unity's Editor API does not provide direct access to modify the Animation Preview's zoom and camera angle. Manipulating these settings would likely require using internal Unity APIs or reflection, which can be risky and is against Unity's terms of service.
-
AutoPlay: Starting the playback of the animation automatically isn't directly supported through the public API for the Animation Preview.
Given these limitations, a practical approach would involve:
- For zoom and camera angle, you might need to use third-party plugins or internal Unity APIs (not recommended).
- For autoplay, consider developing a custom Animation Preview window from scratch or modifying the existing one through a plugin (which requires C++ knowledge and access to Unity's source).
For a more feasible solution within Unity's public API, consider creating a custom Editor script that could control an AnimationClip playback in a custom EditorWindow, allowing full control over playback, camera, and zoom. However, replicating the full functionality of the built-in Animation Preview would be substantial work.