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 evilgras · Jun 17, 2015 at 07:19 PM · c#texturematerialtexture2dresources

Assigning script-generated textures

Hello everyone, new guy here. I've come across a particularly strange problem (I think).

So I'm trying to recreate an old game and one of the requirements I've set myself is that my program has to be compatible with the old game's data files (which are in an old format that's not supported by Unity). One of these data files contains textures, and as far as I can tell the code I've written is reading those out properly. The problems start when I try to assign a texture to a Unity 3D object: the texture simply does not show up, and the object remains its default white-greyish color. Here's the test code I'm working with at the moment (simplified):

 //this script is attached to an empty GameObject
 public class Initialize : MonoBehaviour {
     Style theStyle;
     List<GameObject> gameObjects;
     void Awake () {
         readStyle ();
         gameObjects=new List<GameObject>();
     }
     private void readStyle()
     {
         //stuff happens here that reads out a file, creates a Style object and assigns it to theStyle.
         //It contains public Texture2D[] tileTextures, which contains all textures.
     }
     void Start () {
         GameObject objectToAdd = GameObject.CreatePrimitive (PrimitiveType.Cube);
         objectToAdd.renderer.material.mainTexture=theStyle.tileTextures[10]; //just assigning one of the textures to the cube
         gameObjects.Add (objectToAdd);
     }
 }

The above code didn't work the way I expected it to: the cube that is created is untextured. I tried lots of things to figure out what the problem was. One thing I did was the following test: to check whether tileTextures actually contains textures and a texture gets properly assigned to the cube, I modified Start() as follows:

 void Start () {
     GameObject objectToAdd = GameObject.CreatePrimitive (PrimitiveType.Cube);
     objectToAdd.renderer.material.mainTexture=theStyle.tileTextures[10];
     File.WriteAllBytes(Application.dataPath + "/Resources/file12412.png", ((Texture2D)(objectToAdd.renderer.material.mainTexture)).EncodeToPNG ());
     Texture2D tex = Resources.Load("file12412") as Texture2D;
     objectToAdd.renderer.material.mainTexture = tex;
     gameObjects.Add (objectToAdd);
 }

So, basically, after assigning the texture to the cube, it then takes the cube's texture and exports it as a file. I inspected the resulting file (file12412.png) and it looks fine. After exporting the file, it then imports it again and assigns the imported texture to the cube. The reason I did that is because I wanted to see if a texture that is created using one of Unity's standard functions would actually work properly. It didn't produce any change at all though, but strangely enough, when I then ran the program again a little while later, with the file12412.png file still in the Resources folder, the cube showed up textured correctly.

I then cleared the resources folder to verify this behavior, and this happened:

  • 1st time running without anything in the resources folder, the cube remained untextured.

  • 2nd time running (immediately after the 1st time, but with the file that was generated during the 1st time running still in the resources folder), the cube remained untextured

  • 3rd time running directly after the 2nd time, the cube shows up textured.

It seems to me Unity has to get used to the idea of there being a file in its resources folder or something.

But the most important question is: Why isn't the cube showing up properly textured without all this file exporting/importing? What am I doing wrong?

Feel free to ask for clarifications of course, and thanks in advance for any help!

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

2 People are following this question.

avatar image avatar image

Related Questions

Material Not Reverting Back 2 Answers

Converting a RenderTexture to a Texture2D for use in a shader 2 Answers

Renderer Removed When Scene Started 0 Answers

Change texture of plane pixel by pixel (Unity 5) 0 Answers

Having a problem with the second material (on the same renderer) 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