Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
12
Question by Sammual12 · Jan 09, 2010 at 02:42 AM · textureprocedural

How do I create a texture dynamically in Unity?

For example I want to create a 2 x 2 texture.

The upper left pixel would be White.
The upper right pixel would be Black.
The lower left pixel would be 50% transparent Black.
The lower right pixel would be 100% transparent.

Thanks,
Sammual

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

Answer by Jaap Kreijkamp · Jan 09, 2010 at 02:55 AM

 function Start () {
     // Create a new 2x2 texture ARGB32 (32 bit with alpha) and no mipmaps
     var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
 
     // set the pixel values
     texture.SetPixel(0, 0, Color(1.0, 1.0, 1.0, 0.5));
     texture.SetPixel(1, 0, Color.clear);
     texture.SetPixel(0, 1, Color.white);
     texture.SetPixel(1, 1, Color.black);
 
     // Apply all SetPixel calls
     texture.Apply();
 
     // connect texture to material of GameObject this script is attached to
     renderer.material.mainTexture = texture;
 }
                 

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 Sammual12 · Jan 09, 2010 at 04:42 AM 1
Share

Other then changing texture.SetPixel(0, 0, Color(1.0, 1.0, 1.0, 0.5)); to texture.SetPixel(0, 0, Color(0, 0, 0, 0.5)); that was exactly what I needed.
Thank you!

avatar image Eowyn27 · Mar 12, 2015 at 03:37 PM 0
Share

@Jaap $$anonymous$$reijkamp, Hi I have a question related to this topic: http://answers.unity3d.com/questions/922309/applying-contrast-makes-the-image-black.html

avatar image
12

Answer by Meltdown · Jul 02, 2015 at 04:55 AM

C# version for all the cool coders out there :)

 void Start()
     {
         // Create a new 2x2 texture ARGB32 (32 bit with alpha) and no mipmaps
         var texture = new Texture2D(2, 2, TextureFormat.ARGB32, false);
         
         // set the pixel values
         texture.SetPixel(0, 0, new Color(1.0f, 1.0f, 1.0f, 0.5f));
         texture.SetPixel(1, 0, Color.clear);
         texture.SetPixel(0, 1, Color.white);
         texture.SetPixel(1, 1, Color.black);
         
         // Apply all SetPixel calls
         texture.Apply();
         
         // connect texture to material of GameObject this script is attached to
         GetComponent<Renderer>().material.mainTexture = texture;
     }
Comment
Add comment · Show 4 · 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 mfahadminhas · Aug 05, 2016 at 07:17 PM 0
Share

How can we do it if we have a camera frame and we want to use it as a texture? How can we convert the Image to texture?

avatar image Kyuugatsu mfahadminhas · Aug 05, 2016 at 07:50 PM 0
Share

Use: https://docs.unity3d.com/ScriptReference/Camera-targetTexture.html

avatar image MandL27 · Aug 25, 2016 at 08:08 PM 1
Share

How could I make texture.SetPixel() use ARGB data from an existing texture (e.g. making (24,0) on the new texture being the same color and alpha as (0,0) on an existing texture)?

avatar image monteredento · Dec 29, 2021 at 09:41 PM 0
Share

It has been some time since you posted this, thanks. But I can't get it to work. It shows an error about namespace. Any ideas?

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

6 People are following this question.

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

Related Questions

Setting mipmaps 0 Answers

Modify Six Times Nothing Road Tool Script to have a lower wear range 0 Answers

Can't add texture to procedurally generated plane 1 Answer

Set the procedural color texture script to a set pattern? 1 Answer

Normal maps using TerrainToolkit and Code? 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