• 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 Marxon13 · May 19, 2016 at 05:31 PM · materialtexture2dskyboxloading file

After changing a material's main texture, the main texture is null

I'm trying to load a jpeg image from disk and set it as the skybox's material. I retrieve the image from disk, then use texture.LoadImage() to add the image to the texture. I create a new material, and set the texture (which exists and is not null) to the new material mate.mainTexture. After setting the texture to the material the skybox turns grey. After some debugging, I determined that is caused by mat.mainTexture remaining null and not being set to the texture.

Here is the code that creates and sets the material:

 Debug.Log("Checking if image exists");
 string fileName = name + "-" + id.ToString ();
 string filePath = "";
 if (Application.platform == RuntimePlatform.Android) {
         filePath = Application.persistentDataPath + "/Resources/skyboxImages/" + fileName + ".jpg";
 } else {
         filePath = Application.persistentDataPath + "/Resources/skyboxImages/" + fileName + ".jpg";
 }
 
 Debug.Log ("Checking: " + filePath);
 
 if (!File.Exists (filePath)) {
         Debug.Log("File does not exist");
         return null;
 }
 
 byte[] fileData = File.ReadAllBytes (filePath);
 
 Debug.Log("Creating material");
 Material mat = new Material(Shader.Find("Skybox/Cubemap"));
 Debug.Log ("Created material: " + mat + " END");
 
 Debug.Log ("Creating texture");
 Texture2D texture = new Texture2D (2048, 2048, TextureFormat.PVRTC_RGB4, false);
 Debug.Log ("Created texture: " + texture);
 
 Debug.Log ("Setting name: " + name);
 texture.name = name;
 
 Debug.Log ("Loading image into texture.");
 
 bool loadImageSuccess = texture.LoadImage (fileData);
 Debug.Log ("Success: " + loadImageSuccess.ToString() + " Texture size: {" + texture.width.ToString () + ", " + texture.height.ToString () + "}");
 
 mat.mainTexture = texture;
 
 Debug.Log ("This (" + texture + ") should equal this (" + mat.mainTexture + ")");
 print ("Setting skybox.");
 RenderSettings.skybox = mat;
 DynamicGI.UpdateEnvironment ();
 
 print ("Skybox: " + RenderSettings.skybox + " Textute: " + RenderSettings.skybox.mainTexture);

And this is the debug log:

 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/1B02811B-B2C3-44DD-BE35-C0509E50CC7F/Documents/Resources/skyboxImages/Office-2.jpg
 Creating material
 Created material: Skybox/Cubemap (UnityEngine.Material) END
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Setting name: Office
 Loading image into texture.
 This (Office (UnityEngine.Texture2D)) should equal this ()
 Setting skybox.
 Skybox: Skybox/Cubemap (UnityEngine.Material) Textute: 
 SkyboxLogic:RetreivedSkyboxList()

Why is the texture not being set to the material?

Attempts:

  • I have tried using the png data in the Unity example here. And I get the same result. (Dark grey skybox, mat.mainTexture is null.

  • I have tried using: mat.setTexture("_MainTex", texture); to set the texture instead of mat.mainTexture = texture and I get the same result of mat.mainTexture is null and a grey skybox.

  • I have also tried not creating the new material and setting the texture of the directly to Renderer.skybox.mainTexture. This produces no change in the skybox. It remains the previous material.


    UPDATE

I have updated the code to create cube maps as @FortisVenaliter suggested and am receiving an error when trying to set the texture to on of the cube map's faces: Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats The part I don't understand is that the imported texture's format is RGB24.

It also looks like the material is still not accepting the cube map.

The updated code:

 string[] imageList = new string[6] {"posx.jpg", "negx.jpg", "posy.jpg", "negy.jpg", "posz.jpg", "negz.jpg"};
 CubemapFace[] faceList = new CubemapFace[6] {CubemapFace.PositiveX, CubemapFace.NegativeX, CubemapFace.PositiveY, CubemapFace.NegativeY, CubemapFace.PositiveZ, CubemapFace.NegativeZ};
 
 Cubemap cubemap = new Cubemap (2048, TextureFormat.PVRTC_RGB4, false);
 
 for (int i = 0; i < 6; i++) {
         string imageName = imageList [i];
         CubemapFace face = faceList [i];
 
         Debug.Log("Checking if image exists");
         string filePath = "";
         if (Application.platform == RuntimePlatform.Android) {
                 filePath = Application.persistentDataPath + "/Resources/skyboxImages/" + id.ToString() + "/" + imageName;
         } else {
                 filePath = Application.persistentDataPath + "/Resources/skyboxImages/" + id.ToString() + "/" + imageName;
         }
 
         Debug.Log ("Checking: " + filePath);
 
         if (!File.Exists (filePath)) {
                 Debug.Log("File does not exist");
                 return null;
         }
 
         byte[] fileData = File.ReadAllBytes (filePath);
 
         Debug.Log ("Creating texture");
         Texture2D texture = new Texture2D (2048, 2048, TextureFormat.RGB24, false);
         Debug.Log ("Created texture: " + texture);
 
         Debug.Log ("Loading image into texture.");
         bool loadImageSuccess = texture.LoadImage (fileData);
         fileData = null;
         Debug.Log ("Success: " + loadImageSuccess.ToString() + " Texture size: {" + texture.width.ToString () + ", " + texture.height.ToString () + "} format: " + texture.format);
 
         Debug.Log ("Setting image to cubemap.");
         cubemap.SetPixels(texture.GetPixels(), face);
         cubemap.Apply ();
         texture = null;
 }
             
 Resources.UnloadUnusedAssets ();
 
 Debug.Log("Creating material");
 Material mat = new Material(Shader.Find("Skybox/Cubemap"));
 Debug.Log ("Created material: " + mat + " END");
 
 mat.mainTexture = cubemap;
 
 Debug.Log ("This (" + cubemap + ") should equal this (" + mat.mainTexture + ")");

 print ("Setting skybox.");
 RenderSettings.skybox = mat;
 DynamicGI.UpdateEnvironment ();
 
 print ("Skybox: " + RenderSettings.skybox + " Textute: " + RenderSettings.skybox.mainTexture)
 

And here is the updated debug log:

 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/posx.jpg
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Loading image into texture.
 Success: True Texture size: {2048, 2048} format: RGB24
 Setting image to cubemap.
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/negx.jpg
 Unloading 5 Unused Serialized files (Serialized files now loaded: 0)
 Loading image into texture.
 Success: True Texture size: {2048, 2048} format: RGB24
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/posy.jpg
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Loading image into texture.
 Success: True Texture size: {2048, 2048} format: RGB24
 Setting image to cubemap.
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/negy.jpg
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Loading image into texture.
 Success: True Texture size: {2048, 2048} format: RGB24
 Setting image to cubemap.
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/posz.jpg
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Loading image into texture.
 Success: True Texture size: {2048, 2048} format: RGB24
 Setting image to cubemap.
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Checking if image exists
 Checking: /var/mobile/Containers/Data/Application/8458AAEA-C977-4512-A220-B98913F65C19/Documents/Resources/skyboxImages/2/negz.jpg
 Creating texture
 Created texture:  (UnityEngine.Texture2D)
 Success: True Texture size: {2048, 2048} format: RGB24
 Setting image to cubemap.
 Unsupported texture format - needs to be ARGB32, RGBA32, RGB24, Alpha8 or one of float formats
 
 Creating material
 Created material: Skybox/Cubemap (UnityEngine.Material) END
 This ( (UnityEngine.Cubemap)) should equal this ()
 
 Setting skybox.
 Skybox: Skybox/Cubemap (UnityEngine.Material) Textute: 
 
 Unloading 13 unused Assets to reduce memory usage. Loaded Objects now: 260.
 Total: 64.804794 ms (FindLiveObjects: 0.077833 ms CreateObjectMapping: 0.016750 ms MarkObjects: 7.544916 ms  DeleteObjects: 56.372875 ms)

UPDATE 2

I figured out from trial and error, that the skybox texture is set to the material's _Tex texture. Replacing mat.mainTexture = cubemap; above to mat.SetTexture("_Tex", cubemap); The skybox turns pink when the material is changed.

Comment
Add comment · Show 2
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 FortisVenaliter · May 20, 2016 at 02:30 PM 0
Share

$$anonymous$$aybe because your cubemap is set to PVRTC_RGB4 format? Try regular RBG24, and see if that fixes the import.

avatar image Marxon13 FortisVenaliter · May 20, 2016 at 04:39 PM 0
Share

That would do it... I don't know how I missed that.

1 Reply

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

Answer by FortisVenaliter · May 19, 2016 at 06:16 PM

Doesn't the Skybox\Cubemap shader require a TextureCube instead of a Texture2D? If you set the wrong type, it will reject the value.

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 Marxon13 · May 19, 2016 at 06:31 PM 0
Share

Do you mean a Cubemap? I don't see anything called a TextureCube in Unity's documentation.

avatar image FortisVenaliter Marxon13 · May 19, 2016 at 06:42 PM 0
Share

Correct. Cubemap in unity parlance. I believe it's called TextureCube in Cg. But if you create a new Skybox/Cubemap material in your project manually, you'll see it needs a cubemap.

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

58 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

Related Questions

Texture drawing on the wrong side of a mesh 0 Answers

Load Material From PersistentDataPath at runtime for Android 0 Answers

Unity seems to be in 2D, but I want it in 3D 1 Answer

How do I use a sliced 2D sprite as the repeating texture on a 3D object? 0 Answers

The unity of linux project cannot show material Texture, while build a project with openCvforUnity. 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