- Home /
Clock Script From Java to C# Help
Can someone with a lot more conversion experience please change this Java script to C# Also if you can make it so that it uses the new UI text VS the On GUI text that would be great. I have points to award to whoever helps :)
#pragma strict
var ClockText : GUIText; // Hierarchy DRAG E DROP over var GUI Text in Inspector
var ClockDateText : GUIText; // Hierarchy DRAG E DROP over var GUI Text in Inspector
var ClockSecondsText : GUIText; // Hierarchy DRAG E DROP over var GUI Text in Inspector
private var dt = Date();
function Update () {
var day = dt.Now.Day;
var month = dt.Now.Month;
var year = dt.Now.Year;
var hours = dt.Now.Hour;
var minutes = dt.Now.Minute;
var seconds = dt.Now.Second;
ClockText.text = hours + ":" + minutes;
ClockDateText.text = day + " " + month + " " + year + " ";
ClockSecondsText.text = seconds + " ";
}
Answer by whaleinthesea · Dec 07, 2015 at 07:38 PM
Here is the code, just assign your UI Text elements in the inspector:
using UnityEngine;
using System.Collections;
using System;//you need to import this if you want to use DateTime
using UnityEngine.UI;
public class UITimeShow : MonoBehaviour{ //feel free to change the name of the class to whatever you want
public Text ClockText;
public Text ClockDateText;
public Text ClockSecondsText;
void Update()
{
//Get the time
int day = DateTime.Now.Day;
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;
int hours = DateTime.Now.Hour;
int minutes = DateTime.Now.Minute;
int seconds = DateTime.Now.Second;
ClockText.text = hours + ":" + minutes;
ClockDateText.text = day + " " + month + " " + year + " ";
ClockSecondsText.text = seconds + " ";
}
}
EDIT: if you are using stripping in Unity, make sure you add this to link.xml:
<linker>
<assembly fullname="System" preserve="all"/>
</linker>
Sorry, I changed the code. Now it should work.
@matyboy02 Thanks man that was awesome, you have no idea how long I have been chasing after someone, anyone to do that little job for me, god its like pulling teeth out of a dead horses head on here sometimes trying to solicit help from the proverbial "Boys Club" :) Can't tell you how much I appreciate that man. 100 points going your way :D btw what is that:
<linker>
<assembly fullname="System" preserve="all"/>
</linker>
stuff about?