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
0
Question by Jeremy 6 · Oct 13, 2010 at 06:32 PM · animationtexture2deditorwindow

How do you create animated textures with Texture2D.Apply with any regularity?

I've noticed that one cannot really dynamically modify textures and expect them to animate with any sort of regularity. This may be an Editor specific issue. I am hoping that I can create a "paint" tool in unity but I don't seem to be able to draw reliably. I'm hoping it's just something I'm missing.

Here's my code:

using UnityEditor; using UnityEngine; using System.Collections;

public class TestEditor : EditorWindow {

 [MenuItem("Editor/TestEditor")]
 static void ShowWindow ()
 {
     EditorWindow.GetWindow<TestEditor> ();
 }

 Texture2D canvas;
 Color[] colors;
 bool isInitialized = false;
 bool penDown =false;

 int offset =0;
 public TestEditor() 
 {

 }

 int count =0;
 public void OnGUI()
 {
     this.wantsMouseMove = true;

     canvas = getWhiteCanvas(canvas, (int)(position.width/2), (int)( position.height/2));
     colors = canvas.GetPixels();
     GUI.Box (new Rect(0,0,canvas.width, canvas.height), canvas);

     Event current = Event.current;

     switch(current.type)
     {
     case EventType.Repaint:
         Debug.Log("Repaint:" +count++);
         //if(penDown == true)
         {


             /*
             int x = (int) current.mousePosition.x;
             int y = (int) ((float)(canvas.height) - current.mousePosition.y);
             //Debug.Log("MouseMove:" + x + ":" + y);

             if (x < canvas.width && y < canvas.height) 
             {
                 colors[y * canvas.width + x] = Color.blue;
                 canvas.SetPixels(colors);
                 canvas.Apply();
             }
             */
         }
         break;
     case EventType.MouseDown:
         //Debug.Log("MouseDown:" + current.mousePosition.x + ":" + current.mousePosition.y);
         penDown = true;         
         break;
     case EventType.MouseUp:
         //Debug.Log("MouseUp:" + current.mousePosition.x + ":" + current.mousePosition.y);
         penDown = false;
         break;
     }
 }


 public void Update()
 {
             // Fill the texture with Sierpinski's fractal pattern!
             int y = 0;
             while (y < canvas.height) {
                 int x = 0;
                 while (x < canvas.width) {
                     Color color = ((x+offset & y+offset) == 0)?Color.white:Color.gray;
                     canvas.SetPixel(x, y, color);
                     ++x;
                 }
                 ++y;
             }
             canvas.Apply();
             offset = offset + 1 % canvas.width;

 }

 //-----------------------------------------------------------------------------
 public Texture2D getWhiteCanvas(Texture2D current, int width, int height)
 {
     Texture2D canvas = current;
     if (canvas == null)
     {   
         canvas = new Texture2D(width, height, TextureFormat.ARGB32, false);
         for(int x = 0; x < width; x++)
             for(int y = 0; y < height; y++)
                 canvas.SetPixel(x,y,Color.white);
     }
     else if (canvas.width != width || canvas.height != height)
     {
         canvas = new Texture2D(width, height, TextureFormat.ARGB32, false);
         int oldWidth = (current.width < width) ? current.width : width;
         int oldHeight = (current.height < height) ? current.height : height;
         for(int x = 0; x < oldWidth; x++)
             for(int y = 0; y < oldHeight; y++)
                 canvas.SetPixel(x,y,current.GetPixel(x,y));
     }
     canvas.Apply();
     return canvas;


 }

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Adam Rademacher · Oct 13, 2010 at 07:07 PM

If you use FixedUpdate() instead of Update() does it help the regularity? Update() is frame-dependent, but FixedUpdate runs on a fixed timestep.

Comment
Add comment · Show 3 · 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 Jeremy 6 · Oct 13, 2010 at 07:27 PM 0
Share

No, it doesn't appear to change anything. I wouldn't expect that it would. As erratic as "Update" might be between update calls, it shouldn't be 10s of seconds.

This is specific to creating GUIs for the Editor which I expect that if I ran this code inside of the unity runtime, it would probably change. Sigh. I wonder if I should do that... $$anonymous$$aking an Editor interface for the task that I'm undertaking makes more sense though.

avatar image Adam Rademacher · Oct 14, 2010 at 02:24 PM 0
Share

The docs say it's called 100 times per second, the only thing I can think of is that it's just a very taxing call (double loop setpixel and apply in an update).

avatar image Jeremy 6 · Oct 15, 2010 at 11:48 PM 0
Share

Some forum conversations suggest otherwise. That the Editor calls "OnGui" intermittently when "something happens".

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

No one has followed this question yet.

Related Questions

Change the color of a black sprite?,How can i change the color of a black sprite? 1 Answer

EditorWindow loses Texture2D reference on scene change: how to handle? 4 Answers

Creating Editor 2D Animation Preview 2 Answers

Animation with Texture 2D? 0 Answers

How can I hide function in Animation Event Window? 0 Answers


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