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 /
  • Help Room /
avatar image
0
Question by unity_4XWcnhMEUZ043g · May 02, 2021 at 06:31 AM · scripting problemtexturetexture2dattributetextureformat

How to create texture at runtime (Without using TextureImporter)?

So, what I basically have is: I have a texture Q with some attributes t1,t2,t3... Texture Q is imported into the unity editor. I want to make a script, that will make a new texture P that has the same parameters as texture Q, but: 1. Change Read/Write Enabled to True, 2. Change Max Size to 32, 3. Change Format to RGBA32

My idea is something like:

alt text

But this obviously does not work. What can I do about this?

problem.png (9.1 kB)
Comment
Add comment · Show 5
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 unity_4XWcnhMEUZ043g · May 03, 2021 at 12:16 PM 0
Share

BUMP

Still waiting.

avatar image unity_4XWcnhMEUZ043g · May 05, 2021 at 04:44 PM 0
Share

BUMP Still waiting.

avatar image unity_4XWcnhMEUZ043g · May 08, 2021 at 06:20 PM 0
Share

BUMP Still waiting.

avatar image unity_4XWcnhMEUZ043g · May 09, 2021 at 06:27 PM 0
Share

BUMP Still waiting

avatar image The-Peaceful unity_4XWcnhMEUZ043g · May 09, 2021 at 06:44 PM 0
Share
 Texture2D Q;
 
 Texture2D clone = new Texture2D(Q.width, Q.height);
 clone.SetPixels(Q.GetPixels());
 clone.format = TextureFormat.RGBA32;
 //clone.isReadable(); //Says in the scriptingAPI docs that this is always true if created from script
 clone.Apply();

Something like this? I don't quite understand the 'MaxSize' part though, where is that one com$$anonymous$$g from? :D (If you want to resize the texture though, use 'texture2D.Resize()')

https://docs.unity3d.com/ScriptReference/Texture2D.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by The-Peaceful · May 10, 2021 at 07:54 AM

Ok, looking at it again I think you've understood something wrong. Settings like read/write and maxsize are Importer settings which tell the importer to what maxSize the texture will be compressed and read/write is, when the texture is made from code, of course always true (because otherwise you wouldn't be able to make one :D )

 Texture2D Q;
  
  Texture2D clone = new Texture2D(Q.width, Q.height);
  clone.SetPixels(Q.GetPixels());
  clone.format = TextureFormat.RGBA32;
  clone.Apply();

This code should work for cloning a Texture and setting the format to RGBA32. If what you are meaning to do is then get that texture smaller use clone.Resize(int width, int height, TextureFormat format, bool hasMipMap);. If you are trying to store it as an asset at runtime, that will only work in the editor. If that is what you are trying to do you can do it by using the AssetDatabase, but as I said this will only work for working inside the editor, so make sure to not include this in your build!


https://docs.unity3d.com/ScriptReference/Texture2D.html

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 unity_4XWcnhMEUZ043g · May 10, 2021 at 06:37 PM 0
Share

I can say this solved 75% of my problem. But about resize function. How can I know how much to resize the texture to be 32? That 32 u can find in: 1.Click on the texture 2.In the inspector, go to advanced settings 3.Default 4.MaxSize. There is 32, 1024,2048, ... 5. I need maxSize to be 32, so how much should I resize my texture?

avatar image The-Peaceful unity_4XWcnhMEUZ043g · May 10, 2021 at 07:30 PM 0
Share

Yes that's what I thought. So you want the compression MaxSize to be 32? That will not be possible without using the TextureImporter, because the compression stuff is only for assets of type texture. But you can resize it via Script like so:

 //Either like this (though I don't know but there might be some details getting lost with this one and you call resize twice so a bit less efficient)
 if(clone.width > 32){
     clone.Resize(32, clone.height);
 }
 
 if(clone.height > 32){
     clone.Resize(clone.width, 32);
 }
 
 //Or a bit less code but not as neat looking :D
 if(clone.width > 32 || clone.height > 32){
     clone.Resize(clone.width > 32 ? 32 : clone.width, clone.height > 32 ? 32 : clone.height);
 }

https://docs.unity3d.com/ScriptReference/Texture2D.Resize.html https://docs.unity3d.com/2018.4/Documentation/Manual/class-TextureImporterOverride.html

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

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

Better way to edit RawImage texture data?,Better way to edit a RawImage texture? 0 Answers

How would I go about color correcting a Raw Image? 0 Answers

Unable to save generated/rendered Texture2D to use for billboard 3 Answers

Unity WebCamTexture looks squashed in RawImage 0 Answers

Cannot change material texture with script in WebGL build (but I can in the editor/game) 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