Scrolling a spritesheet inside an editorwindow

Hey, so I’m trying to set up a 2D animation editor in Unity but I’m not able to get my texture to scroll using the code below. The scrollbars show up and can be scrolled, but not the entire span of the texture. I’m probably doing something dumb, but I’m not very familiar with all of these different gui types and using them in conjunction.

I am trying to just pull the texture from the current Sprite Object’s material, which may change, but I’m just dipping my toes into this tool as an educational experience for myself. I know some people will point to the documentation, which I have read, but I’m either missing something or its just not clicking.

Any help would be greatly appreciated! I apologize in advance if my code formatting is poor and/or unreadable.

using UnityEditor;
using UnityEngine;
using System.Collections;

public class AnimationWindow : EditorWindow {

CAnimation animation;
Texture2D texture;
Vector2 scrollPos;

int currentFrame;

public void Init(CAnimation cAnimation)
{
    SetAnimation(cAnimation);
}

public void SetAnimation(CAnimation cAnimation)
{
    animation = cAnimation;
    texture = (Texture2D)cAnimation.renderer.material.mainTexture;

    if (animation == null)
    {
        currentFrame = 0;
        texture = null;
    }
}

void OnGUI()
{

    ////////////////////////////////current frame
    GUILayout.BeginHorizontal();
        GUILayout.Label(" Current Frame ");
        currentFrame = (int)EditorGUILayout.Slider((float)currentFrame, (float)0, (float)animation.m_nNumberOfFrames);
    GUILayout.EndHorizontal();
    ///// end sprite sheet

    ////////////////////////////////sprite sheet
    EditorGUILayout.BeginHorizontal();
        GUILayout.Label(" Sprite Sheet ");
    EditorGUILayout.EndHorizontal();

    EditorGUILayout.BeginHorizontal();
        scrollPos =  EditorGUILayout.BeginScrollView(scrollPos, true, true, GUILayout.Width(400), GUILayout.Height(400));
            EditorGUI.DrawPreviewTexture(new Rect(0, 0, 1000, 1000), texture);
        EditorGUILayout.EndScrollView();
    EditorGUILayout.EndHorizontal();
    ///// end sprite sheet

}

}

EditorGUI.DrawPreviewTexture is not a layout function so it won’t push any layouting information onto the layout stack. You could use GUILayoutUtility.GetRect to create a layouted rectangle which you can use to draw your image.

This is the code you are looking for with a preview for a sprite (uses the sprite native size):

[CustomEditor(typeof(Asset))]
public class TestOnInspector : Editor
{
    public override bool HasPreviewGUI()
    {
        return true;
    }

    public override void OnPreviewGUI(Rect r, GUIStyle background)
    {
        base.OnPreviewGUI(r, background);
        
        Asset asset = (Asset)target;
        if(asset.CharacterPortrait != null)
        {
            GUI.DrawTexture(asset.sprite.rect, asset.sprite.texture);
        }
    }
}