Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by chase cobb · Jun 07, 2013 at 03:33 PM · 2dguieditoreditorwindow

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

 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 07, 2013 at 03:39 PM

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chase cobb · Jun 07, 2013 at 03:52 PM 0
Share

That did exactly what I needed it to do. Thanks!

avatar image chingwa · Aug 16, 2013 at 01:39 PM 0
Share

I've been looking all over for this. Thank you SOO much for your answer!

avatar image
0

Answer by Demerzel_ · Aug 25, 2016 at 02:41 PM

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);
         }
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Handles in EditorWindow 0 Answers

Can't find proper use of objectfield in a custom editor window (C#) 0 Answers

GUI Editior Script not working in ( Unity 2019.x ) 0 Answers

EditorWindow: Window works, why does ModalWindow not? 1 Answer

Create an Editor Window like Mecanim 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges