- Home /
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!
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?
Because the levels are procedurally made and game decides at runtime on which textures to use for skybox.
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.
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?
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.
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
};
}
}
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.
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.
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.
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.
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.
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.
$$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.
this link should help you with using the code at the wiki site.
Thanks but I don't want to blend, I want to change RenderSetting's texture to another texture.
RenderSettings uses a material, not a texture, for the Skybox.