Hey! I’m quite new to Unity (also new to the forum, so I hope I’m not doing anything wrong here xP), and have been dabbling in shaders quite a bit recently. In order to have better control over some parameters per material instances, I am using a MaterialPropertyBlock. However, this seems to nullify any other property in the shader, and I’m not quite sure how to get around it in my case.
Below is a slightly simplified (and WAY less disorganized xP) recreation of my issue.
The shader being used here simply offsets the vertices through an “x_Offset” value that can be changed per object.
However, as I adjust the exposed value in the script (which uses the MaterialPropertyBlock), it seems the _MainTex property gets completely discarded (the white sprite should normally also have a blue emoji texture here, but it’s gone now). This is until I change something in the sprite, like temporarily flipping it in the inspector.
Here’s the script I’m using to change the offset:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Material_xOffset_Controller : MonoBehaviour
{
private int offsetPropertyId = Shader.PropertyToID("_x_Offset");
[SerializeField] private float offset;
private SpriteRenderer rend;
MaterialPropertyBlock propertyBlock;
void OnValidate()
{
if (propertyBlock == null)
propertyBlock = new MaterialPropertyBlock();
SpriteRenderer rend = GetComponent<SpriteRenderer>();
propertyBlock.SetFloat(offsetPropertyId, offset);
rend.SetPropertyBlock(propertyBlock);
}
}
Any help would be appreciated. Thank you in advance!
