I know how to set MaterialPropertyAuthoring for single type property ,just like below:
using Unity.Entities;
using Unity.Mathematics;
using Unity.Rendering;
[MaterialProperty("_Length")]
public struct CustomMaterialPropertyLength : IComponentData
{
public float LengthValue;
}
[UnityEngine.DisallowMultipleComponent]
public class CustomMaterialPropertyLengthAuthoring : UnityEngine.MonoBehaviour
{
[Unity.Entities.RegisterBinding(typeof(CustomMaterialPropertyLength), nameof(CustomMaterialPropertyLength.LengthValue))]
public float Length=5;
class CustomMaterialPropertyLengthBaker : Unity.Entities.Baker<CustomMaterialPropertyLengthAuthoring>
{
public override void Bake(CustomMaterialPropertyLengthAuthoring authoring)
{
CustomMaterialPropertyLength component = default(CustomMaterialPropertyLength);
component.LengthValue = authoring.Length;
var entity = GetEntity(TransformUsageFlags.Renderable);
AddComponent(entity, component);
}
}
}
But, when it turns to be float[ ] , it can’t work anymore,because it is managed type;
[MaterialProperty("_LengthArray")]
public struct MaterialPropertyLengthArray : IComponentData
{
public float[] LengthArray;
}
Then, I tried to change it into NativeArray
[MaterialProperty("_LengthArray")]
public struct MaterialPropertyLengthArray : IComponentData
{
public NativeArray<float> LengthArray;
}
[UnityEngine.DisallowMultipleComponent]
public class MaterialPropertyLengthArrayAuthoring : UnityEngine.MonoBehaviour
{
[Unity.Entities.RegisterBinding(typeof(MaterialPropertyLengthArray), nameof(MaterialPropertyLengthArray.LengthArray))]
public float[] LengthArray = new float[]{1,1,1,1,1,1,1,1};
class MaterialPropertyLengthArrayBaker : Unity.Entities.Baker<MaterialPropertyLengthArrayAuthoring>
{
public override void Bake(MaterialPropertyLengthArrayAuthoring authoring)
{
MaterialPropertyLengthArray component;
component.LengthArray = new NativeArray<float>(authoring.LengthArray,Allocator.Persistent);
var entity = GetEntity(TransformUsageFlags.Renderable);
AddComponent(entity, component);
}
}
}
But there is error like this .
I want to change this componentData in runtime (PerInstance) , so BlobAsset doesn’t work for this.
ISharedComponent is not for AddComponent(xxxx).
Is there anyone who knows about this?