修改天空盒顏色的例子(C#):
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));
}
修改天空盒材質的例子(C#):
// 載入所有 skybox 紋理
Texture2D sbFront = Resources.Load("Texture/Skybox/skybox_Front") as Texture2D;
Texture2D sbBack = Resources.Load("Texture/Skybox/skybox_Back") as Texture2D;
...
// 建立新的 SkyBox 材質
Material mySkyBoxMat = new Material(Resources.Load("Shaders/myOwnSkyboxShader") as Shader);
// 將所有紋理新增到材質中
mySkyBoxMat.SetTexture("_FrontTex", sbFront);
mySkyBoxMat.SetTexture("_BackTex", sbBack);
// 設定RenderSettings skybox 變數
RenderSettings.skybox = mySkyBoxMat;
稍微完整些的例子(C#):
public class SkyboxSetter : MonoBehaviour
{
// 建立Skybox材質
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 SkyboxManifest(Texture2D front, Texture2D back, Texture2D left, Texture2D right, Texture2D up, Texture2D down)
textures = new Texture2D[6]
front,
back,
left,
right,
up,
down
};
修改天空盒顏色的例子(C#):
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));
}
}
修改天空盒材質的例子(C#):
// 載入所有 skybox 紋理
Texture2D sbFront = Resources.Load("Texture/Skybox/skybox_Front") as Texture2D;
Texture2D sbBack = Resources.Load("Texture/Skybox/skybox_Back") as Texture2D;
...
// 建立新的 SkyBox 材質
Material mySkyBoxMat = new Material(Resources.Load("Shaders/myOwnSkyboxShader") as Shader);
// 將所有紋理新增到材質中
mySkyBoxMat.SetTexture("_FrontTex", sbFront);
mySkyBoxMat.SetTexture("_BackTex", sbBack);
// 設定RenderSettings skybox 變數
RenderSettings.skybox = mySkyBoxMat;
稍微完整些的例子(C#):
using UnityEngine;
using System.Collections;
public class SkyboxSetter : MonoBehaviour
{
// 建立Skybox材質
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
};
}
}