Skip to content

Override the color of an asset

You can change the color of an asset by overriding its materials by script using UMI3D transactions. Here is a way to achieve this.

Define a new Material

Create a Material Scriptable Object.

image.png

Such as a Umi3D_External_Material or a Umi3D_PBR_Material.

image.png

Change color by script

Create a script referencing the object and the material.

image.png

using umi3d.edk;
using UnityEngine;

public class ChangeBalloonColor : MonoBehaviour
{
    UMI3DModel balloonModel;

    public MaterialSO newMaterial;

    public void Start()
    {
        balloonModel = GetComponent<UMI3DModel>();
    }

}

Override the material

Override the material by setting a new Material overrider to the object.

1
2
3
4
5
Operation op;
if (model.objectMaterialOverriders.GetValue().Count > 0)
    op = balloonModel.objectMaterialOverriders.SetValue(0, new MaterialOverrider() { overrideAllMaterial = true, newMaterial = newMaterial});
else
    op = balloonModel.objectMaterialOverriders.Add(new MaterialOverrider() { overrideAllMaterial = true, newMaterial = newMaterial});

Perform transaction

1
2
3
4
5
6
Transaction t = new transaction()
{
    reliable = true
};
t.AddIfNotNull(op);
t.Dispatch();