docs.unity3d.com
    Show / Hide Table of Contents

    Creating and editing lights at runtime

    The High Definition Render Pipeline (HDRP) extends Unity's Light component with additional data and functionality. To do this, it adds the HDAdditionalLightData component to the GameObject that the Light component is attached to. Because of this, you cannot create and edit Lights at runtime in the usual way. This document explains how to create an HDRP Light at runtime and how to edit its properties.

    Creating a new light

    HDRP provides a utility function that adds a Light component to a GameObject, and sets up its dependencies. The function is AddHDLight and it takes an HDLightTypeAndShape as a parameter which sets the Light's type and shape.

    using UnityEngine;
    using UnityEngine.Rendering.HighDefinition;
    
    public class HDLightCreationExample : MonoBehaviour
    {
        public GameObject m_Object;
        void Start()
        {
            // Calls a Utility function to create an HDRP Light on a GameObject.
            HDAdditionalLightData lightData = m_Object.AddHDLight(HDLightTypeAndShape.ConeSpot);
    
            // Sets property values for the Light.
            lightData.intensity = 5;
            lightData.range = 1.5f;
            lightData.SetSpotAngle(60);
        }
    }
    

    Editing an existing Light

    HDRP does not use the data stored in the Light component. Instead it stores Light data in another component called HDAdditionalLightData. To access a property, use the HDAdditionalLightData component, even if the property is visible in the Light component Inspector. The following code sample changes the Light's intensity:

    using UnityEngine;
    using UnityEngine.Rendering.HighDefinition;
    
    public class HDLightEditingExample : MonoBehaviour
    {
        public GameObject m_LightObject;
    
        void Start()
        {
            HDAdditionalLightData lightData;
            lightData = m_LightObject.GetComponent<HDAdditionalLightData>();
            // The Light intensity is stored in HDAdditionalLightData.
            lightData.intensity = 40f;
        }
    }
    
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023