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 ChaseRLewis73003 · Sep 22, 2012 at 10:21 PM · guierrortexturecopy

Issue with Drawing a GUI texture

So I'm creating a color edit mode for my game. I've determine how to do it and am working on implementing it in Unity. Thing is the images need some preprocessing to turn them into maps since photoshop aliasing of lines doesn't mean every pixel that needs a specific color is that specific color in the image.

Thing is I want to be able to show the changes as they are made so I am cacheing a copy of the texture and manipulating it. When I use that texture in GUI.DrawTexture(rect,texture) though it draws an opaque grey rectangle whereas if I use the original texture it draws the texture as it should be seen. How can I get the copy to render properly in the GUI.

      public float tolerance = 0.1f;
         public void GetPallette()
         {
             if (texture != null)
             {
                 if(ColorPallette.Count != 0) 
                     ColorPallette.Clear();
                 
                 Color temp = new Color();
                 if (tolerance < 1.0f / 255.0f)
                     tolerance = 1.0f / 255.0f;
                 int indexer = -1;
     
                 InternalTextureCopy = new Texture2D(texture.width, texture.height,texture.format,false);
                 InternalTextureCopy.SetPixels(texture.GetPixels(0),0);
                 
     
                 for (int i = 0; i < InternalTextureCopy.height; i++)
                 {
                     for (int j = 0; j < InternalTextureCopy.width; j++)
                     {
                         temp = InternalTextureCopy.GetPixel(j, i);
                         
                         if (temp.a != 1.0f)//Ignore pure alpha colors
                             continue;
     
                         //See if color is unique
                         indexer = isUniqueColor(ref temp);
     
                         if (indexer < 0)
                         {
                             ColorPallette.Add(new ColorMap(temp));
                             ColorPallette[ColorPallette.Count - 1].indexes.Add((uint)(i * InternalTextureCopy.width + j));
                         }
                         else
                         {
                             ColorPallette[indexer].indexes.Add((uint)(i
 *InternalTextureCopy.width + j));
                         }
                      }
                 }
             }
     
      public void OnGUI()
         {
             Rect rect = new Rect();
     
             if (texture)
             {
                 rect.x = rect.y = 0;
                 if (texture.height > texture.width)
                 {
                     if (texture.height > 250)
                         rect.height = 250;
                     else
                         rect.height = texture.height;
     
                     rect.width = rect.height * (float)texture.width / (float)texture.height;
                 }
                 else
                 {
                     if (texture.width > 250)
                         rect.width = 250;
                     else
                     {
                         rect.width = texture.width;
                     }
     
                     rect.height = rect.width * (float)texture.height / (float)texture.width;
                 }
     
                 rect.height = rect.width * (float)texture.height / (float)texture.width;
             }
     
             if (texture)
             {
                 texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), GUILayout.Width(rect.width), GUILayout.Height(rect.height)) as Texture2D;
             }
             else
                 texture = EditorGUILayout.ObjectField(texture, typeof(Texture2D), GUILayout.MaxWidth(250.0f)) as Texture2D;
     
     
             if (InternalTextureCopy != null)
             {
                 rect.x += rect.width + 10;
                 GUI.DrawTexture(rect,texture,ScaleMode.ScaleToFit,true);
             }
             
             if (GUILayout.Button("Get Pallette"))
             {
                 GetPallette();
             }
             
             tolerance = EditorGUILayout.FloatField("Tolerance: ", tolerance);
             EditorGUILayout.LabelField("Colors: "
 + ColorPallette.Count);
             
             for (int i = 0; i < ColorPallette.Count; i++)
             {
                 GUILayout.BeginHorizontal();
                  int g = 0;
                 //Highligh color channel in picture for easy analysis
                 if(GUILayout.Button("Highlight",GUILayout.MaxWidth(75.0f)))
                 {
                     uint Length = (uint)InternalTextureCopy.width*(uint)InternalTextureCopy.height;
                     int k = 0;
                     Color dummy = new Color();
                     for (int j = 0; j < texture.height; j++)
                     {
                         for(int l = 0;l < texture.width;l++)
                             if (j == ColorPallette[i].indexes[k])
                             {
                                 InternalTextureCopy.SetPixel(l, j, Color.white);
                                 k++;
                             }
                             else
                             {
                                 dummy = InternalTextureCopy.GetPixel(l,j);
                                 dummy.a *= 0.4f;
                                 InternalTextureCopy.SetPixel(l, j, dummy);
                             }
                     }
                 }
     
                 //Delete Color Channel Completely
                 if (GUILayout.Button("Delete", GUILayout.MaxWidth(75.0f)))
                 {
                     if (!ReplacementMode) 
                     ColorPallette.Remove(ColorPallette[i]);
                 }
                
                 //Replace Selection with another existing color channel - bring up window
                 if (i < ColorPallette.Count && GUILayout.Button("Replace", GUILayout.MaxWidth(75.0f)))
                 {
                     if (!ReplacementMode)
                     {
                         ReplacementMode = true;
                         ReplacementIndex = i;
                     }
                     else
                     {
                         if (ReplacementIndex == i)
                         {
                             ReplacementMode = false;
                             ReplacementIndex = -1;
                         }
                         else
                         {
                             ColorPallette[i].indexes.AddRange(ColorPallette[ReplacementIndex].indexes);
                             ColorPallette.Remove(ColorPallette[ReplacementIndex]);
                             ColorPallette[i].indexes.Sort();
                             ReplacementMode = false;
                             ReplacementIndex = -1;
                         }
                     }
                 }
     
                 if (i < ColorPallette.Count) //Needed because if a delete happens this will throw an exception since the count will have changed
                 ColorPallette[i].color = EditorGUILayout.ColorField(ColorPallette[i].color,GUILayout.MaxWidth(125.0f));
     
                 GUILayout.EndHorizontal();
             }
         }
         }
     
     //How I draw it

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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

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

Related Questions

Compnent.guiTexture is obsolete 1 Answer

Wearied Error 1 Answer

GUI Texture error. 1 Answer

Apply Render Texture To GUI + Transparency? 2 Answers

GuiTexture (Touch button) unfollow camera! 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