Using unity version 2022.3.22f1
Question:
Hi all,
I’m encountering an issue where setting the renderingLayerMask on a DecalProjector works correctly in the Unity Editor but fails in the build. Here’s a detailed breakdown of what I’ve tried and the problem I’m facing:
Description:
I have a function to add blood spatter decals to my character. The logic involves performing a raycast to find the hit surface, adjusting the decal’s rotation to align with the surface normal, and setting its rendering layer mask. While this works perfectly in the Editor, it doesn’t display correctly in the build.
Code Snippet:
Code
public void AddBloodSpatter(int boneID, Vector3 position, Quaternion rotation)
{
Debug.Log(“Adding blood spatter”);
// Get the parent bone’s position
var parent = character.characterCustomizationManager.GetBoneFromArray(boneID);
Debug.Assert(parent != null, “Parent bone is null”);
Debug.Log($“Parent bone position: {parent.position}”);
// Perform a raycast from the position towards the parent bone to find the surface
Vector3 direction = (parent.position - position).normalized;
Debug.Log($“Raycast direction: {direction}”);
if (Physics.Raycast(position, direction, out RaycastHit hit))
{
// Get the position and normal of the hit surface
Vector3 hitPosition = hit.point;
Vector3 hitNormal = hit.normal;
// Log the hit position and normal
Debug.Log($“Hit position: {hitPosition}, Hit normal: {hitNormal}”);
// Adjust the rotation to align with the normal
rotation = Quaternion.LookRotation(hitNormal);
Debug.Log($“Adjusted rotation: {rotation}”);
var decal = particleCollisionHandler.GetBloodDecal(hitPosition, rotation);
Debug.Assert(decal != null, “Decal is null”);
decal.transform.SetParent(parent);
var projector = decal.GetComponent();
Debug.Assert(projector != null, “Projector is null”);
// Verify and set the rendering layer mask
int layerIndex = WorldUtilityManager.Instance.playerBloodSpatterIndex;
Debug.Log($“Layer Index: {layerIndex} (should be 3)”);
uint renderingLayerMask = (uint)(0x1 << layerIndex);
Debug.Log($“Setting renderingLayerMask to: {renderingLayerMask} (should be 8)”);
projector.renderingLayerMask = renderingLayerMask;
BloodSpatterElement bloodSpatterElement = new BloodSpatterElement
{
boneID = boneID,
position = hitPosition,
rotation = rotation,
projector = projector
};
bloodSpattersOnPlayer.Add(bloodSpatterElement);
Debug.Log(“Blood spatter added successfully.”);
// Verify rendering layer names
#if UNITY_EDITOR
var globalSettings = UniversalRenderPipelineGlobalSettings.instance;
if (globalSettings != null)
{
var renderingLayerNames = globalSettings.renderingLayerNames;
Debug.Log($“Rendering Layer Name for Index {layerIndex}: {renderingLayerNames[layerIndex]}”);
}
else
{
Debug.LogError(“UniversalRenderPipelineGlobalSettings is not set. Please ensure URP is configured correctly.”);
}
#endif
}
else
{
Debug.LogWarning(“Blood spatter could not find a surface to attach to.”);
}
}
Observations:
- Editor Behavior: The rendering layer mask is set correctly, this means the decals are changed from rendering on the terrain surface (“blood spatter”) to rendering on the players skin and armor (“player blood spatter”) and decals appear as expected.
- Build Behavior: The decals are displayed incorrectly and are still rendeing on the terrain layer and NOT on the players skin and armor as if the renderinglayermask has not been changed, despite the mask being set correctly according to Debug logs as they show the mask is set to the expected value (e.g., 8 for the ‘player blood spatter’ layer).
Additional Information:
- Layer Configuration: The UniversalRenderPipelineGlobalSettings is correctly configured.
- The players skinnedmesh renderer has the correct layers set as well
Questions:
- Why does the renderingLayerMask setting work in the Editor but not in the build?
- How can I ensure that all rendering settings, including layers, are correctly applied in the build?
- Are there any specific settings or configurations I might be missing that could affect rendering layers in the build?
Any help or insights would be greatly appreciated! Thank you!


