private string TimeFormat(int seconds) { TimeAttribute time = attribute as TimeAttribute; if (time.DisplayHours) { return(string.Format("{0}:{1}:{2} (h:m:s)", seconds / (60 * 60), ((seconds % (60 * 60)) / 60).ToString().PadLeft(2, '0'), (seconds % 60).ToString().PadLeft(2, '0'))); } else { return(string.Format("{0}:{1} (m:s)", (seconds / 60).ToString(), (seconds % 60).ToString().PadLeft(2, '0'))); } }
// unity public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label) { if (property.propertyType != SerializedPropertyType.Float) { Debug.LogWarning(notFloatWarning); EditorGUI.PropertyField(rect, property, label, true); } else { TimeAttribute attr = (attribute as TimeAttribute); // mode choice Rect popUpRect = new Rect(rect); float modeWidth = Mathf.Clamp((rect.width - EditorGUIUtility.labelWidth) * modeChoiceWidthRatio, modeChoiceWidthMin, modeChoiceWidthMax); popUpRect.width = modeWidth; popUpRect.x = rect.xMax - popUpRect.width; int cacheIndent = EditorGUI.indentLevel; EditorGUI.indentLevel = 0; int modeChoice = (!property.isExpanded ^ attr.defaultToFrame ? 0 : 1); EditorGUI.BeginChangeCheck(); modeChoice = EditorGUI.Popup(popUpRect, modeChoice, new string[2] { attr.unit.ToString(), frameStr }); if (EditorGUI.EndChangeCheck()) { property.isExpanded = (modeChoice == 1) ^ attr.defaultToFrame; } EditorGUI.indentLevel = cacheIndent; bool timeMode = (modeChoice == 0); // field tooltip Rect hiddenRect = new Rect(rect); hiddenRect.xMin += EditorGUIUtility.labelWidth; hiddenRect.xMax -= modeWidth + 2; EditorGUI.LabelField(hiddenRect, new GUIContent(" ", timeMode ? $"{TimeToFrames(property.floatValue)} {frameStr.ToLower()}" : $"{RealToDisplayTime(property.floatValue, attr.unit)} {attr.unit.ToString().ToLower()}")); // field rect.xMax -= modeWidth + 2; if (timeMode) { EditorGUI.BeginChangeCheck(); float time = EditorGUI.FloatField(rect, GetGUIContentFromProperty(property), RealToDisplayTime(property.floatValue, attr.unit)); if (EditorGUI.EndChangeCheck()) { property.floatValue = DisplayToRealTime(time, attr.unit); } } else // frameMode { EditorGUI.BeginChangeCheck(); int frame = EditorGUI.IntField(rect, GetGUIContentFromProperty(property), TimeToFrames(property.floatValue)); if (EditorGUI.EndChangeCheck()) { property.floatValue = FramesToTime(frame); } } } }