Unity 在Editor下保存对Text组件的文本的修改
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
using System;
using System.Text;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
[CustomEditor(typeof(TimeStampForTextMono))]
public class TimeStampForTextEditor : Editor
{
static string _tmp;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
TimeStampForTextMono mono= (TimeStampForTextMono)target;
bool useTimeStamp = false;
if (GUILayout.Button("更新时间戳"))
{
DateTime nowDt = DateTime.Now;
if (!useTimeStamp)
{
string after = (mono.gameObject.name + nowDt);
mono.Text.text = after.ToString();
}
else
{
string afterTimeStamp = nowDt.DateTime2TimeStamp().ToString();
string after = (mono.gameObject.name + afterTimeStamp);
mono.Text.text = after.ToString();
}
EditorUtility.SetDirty(mono.Text);
}
}
}
#endif
public class TimeStampForTextMono : MonoBehaviour
{
public Text Text { get { return GetComponent<Text>();} }
}
引申的拓展
public static partial class ExtendDateTime
{
public static int DateTime2TimeStamp(this DateTime now)
{
int ts = Convert.ToInt32((now - TheFirstYearOfAnEra()).TotalSeconds);
return ts;
}
public static DateTime TimeStamp2DateTime(this long timeStamp)
{
DateTime preDateTime = TimeZone.CurrentTimeZone.ToLocalTime(TheFirstYearOfAnEra());
long nowTimeStamp = timeStamp * 10000000;
TimeSpan nowTimeSpan=new TimeSpan(nowTimeStamp);
DateTime newDateTime = preDateTime.Add(nowTimeSpan);
return newDateTime;
}
public static DateTime TimeStamp2DateTime(this int timeStamp)
{
long after = (long)timeStamp;
return timeStamp.TimeStamp2DateTime();
}
public static DateTime TimeStamp2DateTime(this string timeStamp)
{
long after = long.Parse(timeStamp);
return timeStamp.TimeStamp2DateTime();
}
static DateTime TheFirstYearOfAnEra()
{
return new DateTime(1970, 1, 1, 8, 0, 0);
}
}