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 UnbreakableOne · Jan 15, 2014 at 07:28 PM · renderskybox

Changing Skybox's texture via code.

Hi,

I've read about how to change Skybox's texture via code. I'm interested to change RenderSetting's one.

I googled a lot and tried whatever I could find, from this:

RenderSettings.skybox.mainTexture = myTexture;

to:

 //                RenderSettings.skybox.SetTexture("_FrontTex", textures[kos]);
 //                RenderSettings.skybox.SetTexture("_BackTex", textures[kos]);
 //                RenderSettings.skybox.SetTexture("_LeftTex", textures[kos]);
 //                RenderSettings.skybox.SetTexture("_RightTex", textures[kos]);
 //                RenderSettings.skybox.SetTexture("_UpTex", textures[kos]);
 //                RenderSettings.skybox.SetTexture("_DownTex", textures[kos]);

to:

 Camera.main.GetComponent<Skybox>().material.SetTexture("_FrontTex", textures[kos]);
                 Camera.main.GetComponent<Skybox>().material.SetTexture("_BackTex", textures[kos]);
                 Camera.main.GetComponent<Skybox>().material.SetTexture("_LeftTex", textures[kos]);
                 Camera.main.GetComponent<Skybox>().material.SetTexture("_RightTex", textures[kos]);
                 Camera.main.GetComponent<Skybox>().material.SetTexture("_UpTex", textures[kos]);
                 Camera.main.GetComponent<Skybox>().material.SetTexture("_DownTex", textures[kos]);


But none of them changed it. What am I missing?

Thanks!

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 iwaldrop · Jan 15, 2014 at 07:44 PM 0
Share

Why don't you just create another material with all of the textures that you want and switch the material on the renderer ins$$anonymous$$d?

avatar image UnbreakableOne · Jan 15, 2014 at 08:16 PM 0
Share

Because the levels are procedurally made and game decides at runtime on which textures to use for skybox.

avatar image iwaldrop · Jan 15, 2014 at 08:27 PM 0
Share

Since all the textures have to mate with each other for good effect it can't decide which material to use ins$$anonymous$$d? Just trying to understand.

avatar image UnbreakableOne · Jan 16, 2014 at 04:11 AM 0
Share

It's a procedurally generated game, so in runtime I have to decide to which texture to use for the sky so I can't make it offline.

Does that answer you?

avatar image iwaldrop · Jan 16, 2014 at 04:31 AM 1
Share

No, not at all. Not only did you previously mention that it's procedurally generated, it doesn't factor into my thinking at all.

I am asking why don't you have a set of materials that you decide between ins$$anonymous$$d of sets of textures. It factors into an answer, because if you are downloading textures at runtime from a server or other online source then, obviously, you need use those textures. However, if all of the textures are already a part of your project, then creating n number of skybox materials and then deciding between them is the way to go.

It is a simple, straight forward, question that I thought would help you to realize what to do by merely asking.

2 Replies

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

Answer by iwaldrop · Jan 16, 2014 at 04:31 AM

Reworked my solution. This version will allow you to use whatever textures you want. Honestly its a little rough and has some superfluous struct action, but it works, and I'm not going to spend any more time on it. :)

 using UnityEngine;
 using System.Collections;
 
 public class SkyboxSetter : MonoBehaviour
 {    
     public static Material CreateSkyboxMaterial(SkyboxManifest manifest)
     {
         Material result = new Material(Shader.Find("RenderFX/Skybox"));
         result.SetTexture("_FrontTex", manifest.textures[0]);
         result.SetTexture("_BackTex", manifest.textures[1]);
         result.SetTexture("_LeftTex", manifest.textures[2]);
         result.SetTexture("_RightTex", manifest.textures[3]);
         result.SetTexture("_UpTex", manifest.textures[4]);
         result.SetTexture("_DownTex", manifest.textures[5]);
         return result;
     }
 
     public Texture2D[] textures;
 
     void OnEnable()
     {
         SkyboxManifest manifest = new SkyboxManifest(textures[0], textures[1], textures[2], textures[3], textures[4], textures[5]);
         Material material = CreateSkyboxMaterial(manifest);
         SetSkybox(material);
         enabled = false;
     }
 
     void SetSkybox(Material material)
     {
         GameObject camera = Camera.main.gameObject;
         Skybox skybox = camera.GetComponent<Skybox>();
         if (skybox == null)
             skybox = camera.AddComponent<Skybox>();
         skybox.material = material;
     }
 }
 
 public struct SkyboxManifest
 {
     public Texture2D[] textures;
 
     public SkyboxManifest(Texture2D front, Texture2D back, Texture2D left, Texture2D right, Texture2D up, Texture2D down)
     {
         textures = new Texture2D[6]
         {
             front,
             back,
             left,
             right,
             up,
             down
         };
     }
 }
Comment
Add comment · Show 7 · 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 UnbreakableOne · Jan 16, 2014 at 04:49 AM 0
Share

Thanks Tyler,

I understand how to change material of a skybox and as I've read it works but I don't have materials for my skybox made and ready.

All I have are textures for skybox. So I need to just change textures of the material of the skybox.

Wish I made myself clear.

avatar image iwaldrop · Jan 16, 2014 at 04:53 AM 0
Share

I've asked this twice already, but here goes again. Why can't you just make the materials at design time, and then use them at runtime?? You're being perfectly clear. I understand what you want to do. I just think there's a better way to do it, and I'm trying to help you see it.

avatar image UnbreakableOne · Jan 16, 2014 at 05:13 AM 0
Share

Because it's not definable before entering the game which textures will end up in the skybox.

When game runs, based on some parameters and calculations it decides which textures it wants to use for skybox, then I apply those to the current skybox and voila.

Problem, as hopefully visible here, is that I cannot do that offline and if I want to do it, I have to make like thousands of skybox materials and decide which one to use in runtime.

That's why I need to be able to alter skybox's textures manually at runtime.

avatar image UnbreakableOne · Jan 16, 2014 at 05:14 AM 0
Share

Imagine making a procedurally level in roguelike games, you don't know which level layout you will have and only in runtime it's deter$$anonymous$$ed via some calculations.

avatar image iwaldrop · Jan 16, 2014 at 05:17 AM 0
Share

Ok. I'm going to stop trying to understand your use case because you obviously have it thought through, and know what you need. Let me see what I can come up with.

Show more comments
avatar image
0

Answer by sheffieldlad · Jan 15, 2014 at 08:27 PM

The link below from the unity wiki may help you.

It blends 2 sky boxes. You just need it to blend them real fast from your 1st skybox to the one you want to replace it with.

Hope it helps.

Link to wiki document here.

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 sheffieldlad · Jan 15, 2014 at 08:29 PM 0
Share

$$anonymous$$ake sure you 'clamp' the textures of both skyboxes in the inspector otherwise you will get lines appear between the individual textures of the skyboxes.

avatar image sheffieldlad · Jan 15, 2014 at 08:30 PM 0
Share

this link should help you with using the code at the wiki site.

here

avatar image UnbreakableOne · Jan 16, 2014 at 04:14 AM 0
Share

Thanks but I don't want to blend, I want to change RenderSetting's texture to another texture.

avatar image iwaldrop · Jan 16, 2014 at 04:58 AM 0
Share

RenderSettings uses a material, not a texture, for the Skybox.

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

21 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change Render Settings (SkyBox) through code. 1 Answer

Why do I see this fog in the mittle of my view on my skybox cubemap? 1 Answer

Change skybox at runtime? 2 Answers

Dynamic, warped text 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