• 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 cftcimert · Mar 10, 2021 at 01:02 PM · texturebuild

Texture Apply does not work when build.

Processing textures feature does not work when building while working in the editor. My code is here.

 public class MyShaderBehavior : MonoBehaviour
 {
     [Tooltip("Number of pixels per 1 unit of size in world coordinates.")]
     [Range(16, 8182)]
     public int textureSize = 64;
 
     private readonly Color c_color = new Color(0, 0, 0, 0);
 
     private Material m_material;
     [HideInInspector] public  Texture2D m_texture;
     private bool m_isEnabled = false;
 
     private readonly object m_lockFlag = new object();
 
     void Start()
     {
         Renderer renderer = GetComponent<Renderer>();
         if (renderer == null)
             return;
 
         foreach (Material material in renderer.materials)
         {
             if (material.shader.name.Contains("Custom/PaintShaders/"))
             {
                 m_material = material;
                 break;
             }
         }
 
         if (m_material != null)
         {
             m_texture = new Texture2D(textureSize, textureSize);
             for (int x = 0; x < textureSize; ++x)
                 for (int y = 0; y < textureSize; ++y)
                     m_texture.SetPixel(x, y, c_color);
             m_texture.Apply();
 
             m_material.SetTexture("_DrawingTex", m_texture);
             m_isEnabled = true;
         }
     }
 
     public void PaintOnColored(Vector2 textureCoord, float[,] splashTexture, Color color)
     {
         MyPaintOn(textureCoord, splashTexture, color);
     }
 
     private void MyPaintOn(Vector2 textureCoord, float[,] splashTexture, Color targetColor)
     {
         if (m_isEnabled)
         {
             lock (m_lockFlag)
             {
                 Stopwatch sw = new Stopwatch();
                 sw.Start();
                 int reqnx = splashTexture.GetLength(0);
                 int reqny = splashTexture.GetLength(1);
                 int reqX = (int)(textureCoord.x * textureSize) - (reqnx / 2);
                 int reqY = (int)(textureCoord.y * textureSize) - (reqny / 2);
                 int right = m_texture.width - 1;
                 int bottom = m_texture.height - 1;
 
                 int x = IntMax(reqX, 0);
                 int y = IntMax(reqY, 0);
                 int nx = IntMin(x + reqnx, right) - x;
                 int ny = IntMin(y + reqny, bottom) - y;
 
                 Color[] pixels = m_texture.GetPixels(x, y, nx, ny);
 
                 int counter = 0;
                 for (int i = 0; i < nx; ++i)
                 {
                     for (int j = 0; j < ny; ++j)
                     {
                         float currAlpha = splashTexture[i, j];
                         if (currAlpha == 1)
                             pixels[counter] = targetColor;
                         else
                         {
                             Color currColor = pixels[counter];
                             // resulting color is an addition of splash texture to the texture based on alpha
                             Color newColor = Color.Lerp(currColor, targetColor, currAlpha);
                             // but resulting alpha is a sum of alphas (adding transparent color should not make base color more transparent)
                             newColor.a = pixels[counter].a + currAlpha;
                             pixels[counter] = newColor;
                         }
                         counter++;
                     }
                 }
 
                 m_texture.SetPixels(x, y, nx, ny, pixels);
                 m_texture.Apply();
                 sw.Stop();
             }   
         }
     }
 
     private int IntMax(int a, int b)
     {
         return a > b ? a : b;
     }
 
     private int IntMin(int a, int b)
     {
         return a < b ? a : b;
     }
 }
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

152 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 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 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 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 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 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 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 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 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

Distribute terrain in zones 3 Answers

Why are my texture sizes so large in my build? 3 Answers

Small game, huge size? 1 Answer

Shuriken Billboarded Texture Gone in Built Executable 1 Answer

Loading textures into an array after build 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