2015-07-18 31 views
0

在Unity3D中,我试图设置我的刹车灯材料上的发光属性上的浮动 - 但没有任何运气。使用C#,我如何在我的发射属性上设置浮点数?

这里是我的C#代码:

using UnityEngine; 
using System.Collections; 

public class BrakeLights : MonoBehaviour { 

    private Renderer Brakes; 

    // Use this for initialization 
    void Start() { 
     Brakes = GetComponent<Renderer>(); 
     Brakes.material.shader = Shader.Find("Standard"); 
     Brakes.material.EnableKeyword("_EMISSION"); 
    } 

    // Update is called once per frame 
    void Update(){ 
     //Brake lights 
     if(Input.GetKey(KeyCode.Space)){ 
      Brakes.material.SetFloat("_Emission", 1.0f); 
      Debug.Log (Brakes.material.shader); 
     }else{ 
      Brakes.material.SetFloat("_Emission", 0f); 
      Debug.Log ("Brakes OFF"); 
     } 
    } 
} 

缺少什么我在这里?我也没有在控制台中发现错误,并且当我按下空格键时,我的调试日志在运行时显示。

感谢您的帮助!

+1

不知道有什么统一但为什么启用关键字时,您使用大写字母,然后使用不同的情况下引用它?使用相同的案例会更加连贯吗? – Steve

+1

史蒂夫,谢谢你的回复。非常感激。 因此,在启用关键字时,Unity3D将所有带下划线的大写字母作为第一个字符。作为参考,这里是关键字的链接(向下滚动一下): http://docs.unity3d.com/Manual/MaterialsAccessingViaScript.html 我使用了不同的情况下字符串在设置浮动功能模仿什么我在这里看到: http://docs.unity3d.com/ScriptReference/Material.SetFloat.html 我觉得我很亲密,但只是没有做正确的事情或缺少一个步骤。 –

+0

@Steve这些是两个不同的**基于字符串的属性ID。你必须接受Unity团队的任何一致性问题。 –

回答

0

我发现成功使用发光颜色,没有为其强度设置浮点值。因此,例如,我会将发射颜色设置为黑色以表示没有强度/发射,并指定红色(在我的情况下)以使材质发红光。

我也意识到我必须使用传统着色器才能工作。

这里是工作的代码:

using UnityEngine; 
using System.Collections; 

public class Intensity : MonoBehaviour { 

    private Renderer rend; 

    // Use this for initialization 
    void Start() { 

     rend = GetComponent<Renderer>(); 
     rend.material.shader = Shader.Find ("Legacy Shaders/VertexLit"); 
     rend.material.SetColor("_Emission", Color.black); 
    } 

    // Update is called once per frame 
    void Update() { 
     if (Input.GetKey (KeyCode.Space)) { 
      rend.material.SetColor ("_Emission", Color.red); 
     } else { 
      rend.material.SetColor ("_Emission", Color.black); 
     } 
    } 
} 
相关问题