I have a (hopefully) simple question. In my script I have the hours, minutes, and secods displayed like this: #145723 Currently this is just a multiple varialbes stringed together. I want to be able to convert the string, this is: hours + zerogap1 + minutes + zerogap2 + seconds the zerogap making it not ‘2’ but ‘02’ if the seconds/minutes are under 10, to a real hex value and then that hex value to an RGBA value. This RGBA value is going to set a materials color.
To make that easier to understand, I am recreating the Hex Clock in Unity: Hex clock
function refreshData()
{
x = 1; // x = seconds
var d = new Date()
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
if (h<=9) {h = '0'+h};
if (m<=9) {m = '0'+m};
if (s<=9) {s = '0'+s};
var color = '#'+h+m+s;
$("div.background").css("background-color", color );
$("p#hex").text(color);
setTimeout(refreshData, x*1000);
}
This can be simplified using the DateTime class, and a string format parameter:
string color = "#" + DateTime.Now.ToString("HHmmss");
Debug.Log(color);
Color c;
ColorUtility.TryParseHtmlString(color, out c);
Camera.main.backgroundColor = c; // use the color