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
}
}
}
代码测试结果:
按照RenderSettings.skybox = mySkyBoxMat方式设置并没有成功,用下面例子中的SetSkybox运行成功。
需要注意的是,Texture目录需要放在Resources目录下,最终的目录结构是这样的:
项目主目录/Assets/Resources/Textures/Skybox/skybox_Front.jpg
1.打开软件扒册,新建工程如下图所示。点击创建工程按钮。
2.在层级面板中穿件Cube,在项目面板鼠标右键中创建材质,如下图所示
3.在项目面板下创建脚本文件,如图所示
4.双击脚本文件打开,编写代乱此岁码如哗睁下
5.创建标准shader,如下图所示
6.并将创建的shader替换给Cube对象
7.双击打开5中创建的Shader文件,查看参数信息(参数信息的结果是为了书写4中的代码,参见经验视频)
8.将3中的脚本附加给Cube。
9.点击运行,结果如图。
新建一个c#文件,名称为Test,复制以下代码到文件里,然后拖到要凯罩变色的梁激物体上,运行,OKusing System.Collections
using System.Collections.Generic
using UnityEngine
public class Test : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
GetComponent<橡孙袜Renderer>().material.color = Color.red
Debug.Log("我变红啦!")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)