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 BearShark1993 · May 07, 2015 at 02:13 PM · texturecolortexture2dtexturesuv mapping

Editing Texture during Run time causing Unity Editor to Crash

So I am trying to make a uv map editor script for creating randomly generated characters in my game. I decided to try a prototype brute force implementation when doing this as shown in the code below. It basically grabs the main texture of the material of the object then it attempts to iterate pixel by pixel through the texture and checks for the pixel if its not black. If its not black it will change the pixel color to the given one. I am not sure what is about this causing the editor to stall and not respond when I push play. It could be that I am comparing floats a large its definitely caused by the changeColor method amount of times. I noticed no changes in CPU or RAM usage by unity.

If anyone would have any suggestions or a possible better implementation I would appreciate it.

 using UnityEngine;
 using System.Collections;
 
 public class UVTextureEditor : MonoBehaviour {
     public Texture2D uvMap;
     public Color changecolor;
 
     // Use this for initialization
     void Start () {
         uvMap = GetComponent<MeshRenderer>().material.GetTexture("_MainTex") as Texture2D;
         Debug.Log(uvMap.width);
         Debug.Log (uvMap.height);
         this.changeColor();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     public void changeColor(){
         for(int i = 0; i<this.uvMap.width/2;i++){
             for(int j =0; j<this.uvMap.height/2;j++){
                 Color comp = uvMap.GetPixel(i,j);
                 if(comp.r != Color.black.r && comp.g != Color.black.g && comp.b !=Color.black.b){
                     uvMap.SetPixel(i,j,this.changecolor);
                 }
             }
         }
     }
 
 }

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
2
Best Answer

Answer by $$anonymous$$ · May 07, 2015 at 05:42 PM

Got it, so the first thing I did was I Changed Texture properties to ReadEnabled and Format to Automatic Truecolor. Then I've modified your code a bit:

     public Texture2D uvMap;
     public Color changecolor;
 
     private void Start()
     {
         uvMap = (Texture2D)GetComponent<Renderer>().material.mainTexture;
 
         Debug.Log(uvMap.width);
         Debug.Log(uvMap.height);
 
         this.changeColor();
     }
 
     public void changeColor()
     {
         Color[] Pixels = uvMap.GetPixels(); // Get the whole Color Array (More Efficient)
 
         for (int x = 0; x < this.uvMap.width / 2; x++)
         {
             for (int y = 0; y < this.uvMap.height / 2; y++)
             {
                 Color CurrPix = Pixels[x + y * uvMap.width];
 
                 if (CurrPix.r != 0 && CurrPix.g != 0 && CurrPix.b != 0)
                     Pixels[x + y * uvMap.width] = this.changecolor;
             }
         }
         uvMap.SetPixels(Pixels);
         uvMap.Apply();
     }


Not sure why it crashes your Unity as well as mine but this will do it :) Also note that your code only colors just 1 / 4 (25%) of 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 BearShark1993 · May 07, 2015 at 06:33 PM 0
Share

Thanks yeah the half thing was because I was checking if comparing 1024x1024 floats was causing the problem.

Its not crashing But when I test it on the unity construction worker it doesn't appear to be changing the textures colour. I think there is something I am missing with this idea.

EDIT: NEVER$$anonymous$$IND ITS WOR$$anonymous$$ING NOW

avatar image BearShark1993 · May 07, 2015 at 06:42 PM 0
Share

Whats interesting is that when I tested with the construction worker it colors all his parts except his orange overall and its always pink no matter what I set changecolor to in the editor.

EDIT: there must be something special about that UV map for the construction worker cause I tried it with my other meshes and UV maps I made in blender and its working like a charm

Thanks again

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distorted/Warped Textures On Character Model After Bundling For Tabletop Simulator/In A Build 1 Answer

uv does't apply correctly 0 Answers

Best way to show an image 2 Answers

Combining 2 black-white textures 2 Answers

How can I efficiently determine that certain parts of a texture have a specific color? 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