Changing Material Color from string value (hex)

I have found that there are no proper tutorial for material color changing in unity by scripting.

Here I have tried a small code to change the color of material by string value. Other old posts are using Color.White method, this code will support string value for color changing ( #FFFFFFFF ).

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MatTest : MonoBehaviour {

public Transform go1;
MaterialPropertyBlock _propblock;
Renderer _renderer;
String colorCode = β€œ#3E0808FF”;

void Start () {
_renderer = go1.GetComponent ();
_propblock = new MaterialPropertyBlock ();
_renderer.GetPropertyBlock (_propblock);

Color myColor = new Color();
ColorUtility.TryParseHtmlString(colorCode, out myColor);
_propblock.SetColor(β€œ_Color”, myColor);
_renderer.SetPropertyBlock (_propblock);
}
}

1 Like

Maybe this will help : Unity - Scripting API: ColorUtility.TryParseHtmlString