in the unity editor, when i open a aniamtion the aniamtion preveiw panel opens, i want…
- User
in the unity editor, when i open a aniamtion the aniamtion preveiw panel opens, i want to amke an editor tool tht allows me to choose the default view of tha niamtion editor preview, and allows me to set a zoom or camera andgle, and adds autopaly on the aniamtion on start, giv me code, full working script
- Assistant
Below is a SINGLE ‑ FILE drop-in script that lives inside an Editor/ folder and gives you:
• your own “override” for the default Animation-Clip preview that appears at the bottom of the Inspector
• a little Settings bar where you can
– switch Auto-Play on/off (defaults to ON)
– pick a default camera orbit / zoom (distance & yaw/pitch)
• remembers the last settings you used (stored in EditorPrefs )
• always starts the clip at time 0 when you first select the asset.How it works
Unity allows you to replace the tiny preview area of any asset type by implementing ObjectPreview and tagging it with [CustomPreview(typeof(AnimationClip))] .
The script below does exactly that.
Because Unity does not expose the internal “real” preview camera, we spin up our own PreviewRenderUtility , drop a simple Capsule in there (you can plug in whatever prefab you want), and drive the animation manually.
This way we never touch the original clip on disk – everything happens in the preview scene.Save the file as
Assets/Editor/AnimationClipCustomPreview.cs/* AnimationClipCustomPreview.cs * --------------------------------------------------------- * Put this file inside an Assets/Editor/ folder. * * Replaces Unity’s default AnimationClip preview with a * fully controllable version that supports: * • auto-play on asset selection * • custom camera orbit / zoom * • persistent editor-wide settings * * Tested with Unity 2020 LTS – 2023.2 * --------------------------------------------------------- */ using UnityEditor; using UnityEngine; using System; [CustomPreview(typeof(AnimationClip))] public class AnimationClipCustomPreview : ObjectPreview { // --------------------------- Settings --------------------------- private static readonly string PREF_AUTOPLAY = "AnimPrev_Autoplay"; private static readonly string PREF_DISTANCE = "AnimPrev_Distance"; private static readonly string PREF_YAW = "AnimPrev_Yaw"; private static readonly string PREF_PITCH = "AnimPrev_Pitch"; private bool autoPlay; private float camDistance; private float camYaw; private float camPitch; // --------------------------- Runtime ---------------------------- private PreviewRenderUtility previewUtil; private GameObject previewGO; // Capsule (or your own model) private Animation animationComp; // Legacy animation component private float currentTime; private bool firstRepaint = true; // ---------------------------------------------------------------- // PREVIEW LIFECYCLE // ---------------------------------------------------------------- public override bool HasPreviewGUI() => true; public override void Initialize(UnityEngine.Object[] targets) { base.Initialize(targets); // Load persisted user prefs or fall back to defaults autoPlay = EditorPrefs.GetBool (PREF_AUTOPLAY, true); camDistance = EditorPrefs.GetFloat(PREF_DISTANCE, 3.0f); camYaw = EditorPrefs.GetFloat(PREF_YAW, 180f); camPitch = EditorPrefs.GetFloat(PREF_PITCH, 10f); // Build preview scene previewUtil = new PreviewRenderUtility(true); previewUtil.cameraFieldOfView = 30f; // Simple lit scene previewUtil.lights[0].intensity = 1.2f; previewUtil.lights[0].transform.rotation = Quaternion.Euler(40f, 40f, 0); previewUtil.lights[1].intensity = 1.2f; // Object that will receive the clip previewGO = GameObject.CreatePrimitive(PrimitiveType.Capsule); previewGO.hideFlags = HideFlags.HideAndDontSave; // Use *legacy* Animation component (no controller needed) animationComp = previewGO.AddComponent<Animation>(); animationComp.playAutomatically = false; // Add clip from target[0] (we only preview the first selection) var clip = target as AnimationClip; animationComp.AddClip(clip, clip.name); animationComp.clip = clip; previewUtil.AddSingleGO(previewGO); } public override void OnPreviewSettings() { // Camera orbit camYaw = EditorGUILayout.Slider(new GUIContent("Yaw"), camYaw, 0, 360); camPitch = EditorGUILayout.Slider(new GUIContent("Pitch"), camPitch, -90, 90); camDistance = EditorGUILayout.Slider(new GUIContent("Zoom"), camDistance, 0.5f, 10f); // Autoplay autoPlay = GUILayout.Toggle(autoPlay, "Auto Play", "preButton"); // Persist immediately so the setting survives domain reload EditorPrefs.SetBool (PREF_AUTOPLAY, autoPlay); EditorPrefs.SetFloat(PREF_DISTANCE, camDistance); EditorPrefs.SetFloat(PREF_YAW, camYaw); EditorPrefs.SetFloat(PREF_PITCH, camPitch); } public override void OnPreviewGUI(Rect r, GUIStyle background) { if (previewUtil == null) return; // --------------------------------- // Update animation time // --------------------------------- var clip = target as AnimationClip; if (firstRepaint) { // Reset at frame 0 when you first click the asset currentTime = 0f; firstRepaint = false; } else if (Event.current.type == EventType.Repaint && autoPlay) { currentTime += Time.deltaTime; if (currentTime > clip.length) currentTime = 0f; } animationComp[clip.name].time = currentTime; animationComp.Sample(); // --------------------------------- // Camera transform // --------------------------------- var cam = previewUtil.camera; cam.transform.position = Quaternion.Euler(camPitch, camYaw, 0f) * Vector3.back * camDistance; cam.transform.LookAt(Vector3.zero); cam.nearClipPlane = 0.01f; cam.farClipPlane = 100f; // --------------------------------- // Render // --------------------------------- previewUtil.BeginPreview(r, background); previewUtil.Render(); Texture result = previewUtil.EndPreview(); GUI.DrawTexture(r, result, ScaleMode.StretchToFill, false); } public override void OnDisable() { // Clean up when preview object is destroyed if (previewGO) UnityEngine.Object.DestroyImmediate(previewGO); if (previewUtil != null) { previewUtil.Cleanup(); previewUtil = null; } base.OnDisable(); } // Make sure our preview is preferred over Unity’s default public override GUIContent GetPreviewTitle() => new GUIContent("Clip Preview ✦"); // When the user selects another asset next time public override void ReloadPreviewInstances() => firstRepaint = true; }HOW TO USE
- Drop the file anywhere under Assets/Editor/ .
- Select any .anim (AnimationClip) asset in your Project view.
- The Inspector’s bottom pane is now replaced by our custom preview:
• It auto-plays right away (toggle in the title-bar).
• Drag sliders in the settings bar to pick a default yaw, pitch, and zoom.
• Those settings are remembered globally for the whole editor session (and beyond).
Customising further
• Swap the primitive capsule with any prefab you like. Change this line inside Initialize():previewGO = (GameObject)PrefabUtility.InstantiatePrefab(myPrefab);• Want a Play/Pause button or a scrubber? Just extend OnPreviewSettings() and/or OnPreviewGUI() – you already have access to currentTime .
• Prefer a dedicated dockable window instead of replacing the inspector? See Unity’s EditorWindow and PreviewRenderUtility docs: you can copy almost the same code into a window class.Enjoy friction-free animation previewing! 🕺
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.