简体   繁体   中英

Unity3D: Change skybox color via script?

I am working on a game in the engine Unity, and am trying to make the skybox change color based on the time of day, but I can't seem to find out how to get it working.. What I want to do, I think, is to change the color of the material I use for the skybox in render settings, and be able to set it using one variable for red, one for green and one for blue.

I am using C#.

Thanks in advance for all answers :)

From the code you displayed in the comment:

RenderSettings.skybox.SetColor("_Tint", 0, 0, blue)

I think you mean

RenderSettings.skybox.SetColor("_Tint", Color.blue)

no need for extra zeros and remember that the color " blue " is a member variable of the Color class .

Next you would have to develop a time system and based on the time var you pass to the script controlling the skybox renderer you would then use a Lerp function to smoothly transition from one color to the next... like this

using UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
    public Color colorStart = Color.blue;
    public Color colorEnd = Color.green;
    public float duration = 1.0F;
    void Update() {
        float lerp = Mathf.PingPong(Time.time, duration) / duration;
        RenderSettings.skybox.SetColor("_Tint", Color.Lerp(colorStart, colorEnd, lerp));
    }
}

Then you could write a function to change the colorStart and colorEnd...

Hope this helps...

We can change Skybox color using the _Tint property. RenderSettings is the base class used to change the render properties at run time. For ensuring the attribute is existing in the skybox is done by the HasProperty() . SetColor() is used to set the color of the skybox.

     if (RenderSettings.skybox.HasProperty("_Tint"))
         RenderSettings.skybox.SetColor("_Tint", Color.red);
     else if (RenderSettings.skybox.HasProperty("_SkyTint"))
         RenderSettings.skybox.SetColor("_SkyTint", Color.red);

you can make your own skybox too in unity by changing texture shape into a cube one then apply for those changes it will create a cube mesh which you can simply drop it into your unity editor scree.

and if you want to load multiple skybox materials on run time by click on button I have that code for that i hope it will help you for building a project in which you want to change skybox by some time or by using other input method.

enter code here

public class skybox : MonoBehaviour {

enter code here
public Material[] secondSkybox;
public static int i = 0;
public void skyboxOn()
{

    if (i == 0) {
        RenderSettings.skybox = secondSkybox[0];
        i++;
    }
    else if(i==1)
    {
        RenderSettings.skybox = secondSkybox[1];
        i++;
    }else if(i==2)
    {
        RenderSettings.skybox = secondSkybox[2];
        i=0;
    }
    }
} 

如果你也想改变天空盒的颜色,这可以通过使用这行代码来完成

RenderSettings.skybox.SetFloat ("_Exposure", Mathf.Sin (Time.time * Mathf.Deg2Rad * 100) + 2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM