2015-07-18 34 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); 
     } 
    } 
} 
相關問題