docs.unity3d.com
    Show / Hide Table of Contents

    Creating a Decal Projector at runtime

    The Decal Projector component enables you to project decals into an environment. When you create a Decal Projector at runtime, the workflow is slightly different to the workflow you usually use when instantiating Prefabs.

    Instantiating a new DecalProjector from a Prefab does not automatically create a new instance of the DecalProjector's material. If you spawn multiple instances of the DecalProjector and make changes in one of its materials, the changes affect every DecalProjector instance. In the usual Prefab workflow, Unity creates a new instance of the material with the new Prefab instance. To match this usual workflow you must manually create a new material from the Prefab when you instantiate a new DecalProjector instance. For an example of how to do this, see the following code sample.

    using UnityEngine;
    using UnityEngine.Rendering.HighDefinition;
    
    public class CreateDecalProjectorExample : MonoBehaviour
    {
        public GameObject m_DecalProjectorPrefab;
    
        void Start()
        {
            GameObject m_DecalProjectorObject = Instantiate(m_DecalProjectorPrefab);
            DecalProjector m_DecalProjectorComponent = m_DecalProjectorObject.GetComponent<DecalProjector>();
    
            // Creates a new material instance for the DecalProjector.
            m_DecalProjectorComponent.material = new Material(m_DecalProjectorComponent.material);
    
        }
    }
    
    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