Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Shoxuja · Feb 10, 2016 at 01:35 PM · editor-scriptingbeginnerevent

How to use DateTime.now (Local) with multiple daily Time Ranges to use for Day/Night Cycle?

I want to apply a sky change every 6 hours that changes with the Local time under DateTime.now. The DateTime.now will update the quarters of a 24 hr cycle of "Dawn, Day, Dusk, Night" I am clueless on how to type the script up.

This is what I have:

 using UnityEditor;
 using System;
 public class DayNightController : MonoBehaviour
 
 {
     public static void Main()
     
     {
         DateTime now = DateTime.now
         
         
     }
 }
 
 

I'm very confused when trying to learn Scripting.

If Possible how can I edit the following code that I found online to work with DateTime.Now?: Code:

 using System;
 using System.Collections.Generic;
 using System.Text;
 using UnityEditor;
 /// <summary>
 /// Represents a time range which is defined with a START and END time (bounds).
 /// Both bounds are stored internally as Timespan objects.
 /// - it a range for one day (00:00-24:00) which means it is date independend!
 /// - precision is reduced to hours and minutes
 /// - bounds can be set directly with start and end properties or with SetEnd() und SetStart() methods.
 /// - 0:00 at the end is displayed as 24:00
 /// <creator>Michal Gabrukiewicz</creator>
 /// </summary>
 public class TimeRange {
 
     /// <summary>
     /// Gets the amount of minutes of a whole day (24 hours)
     /// </summary>
     public const int MinutesOfTheDay = 1440;
     private bool _initialized = false;
 
     /// <summary>
     /// Instantiates a new time range with a given start- and end-timespan
     /// </summary>
     /// <param name="start"></param>
     /// <param name="end"></param>
     public TimeRange(TimeSpan start, TimeSpan end)
         : this() {
         Start = start;
         End = end;
     }
 
     /// <summary>
     /// Instantiates a new time range for a given Start and End with hours and minutes
     /// </summary>
     /// <param name="startH"></param>
     /// <param name="startM"></param>
     /// <param name="endH"></param>
     /// <param name="endM"></param>
     public TimeRange(int startH, int startM, int endH, int endM)
         : this() {
         Start = new TimeSpan(startH, startM, 0);
         End = new TimeSpan(endH, endM, 0);
     }
 
     /// <summary>
     /// Instantiates a new timerange instance with the smallest possible lower and highest possible upper bound (00:00-24:00)
     /// </summary>
     public TimeRange() {
         Start = new TimeSpan(0, 0, 0);
         End = new TimeSpan(24, 0, 0);
         _initialized = true;
     }
 
     /// <summary>
     /// represents the timerange in a format like: 12:00-14:00
     /// </summary>
     /// <returns></returns>
     public override string ToString() {
         return FormatTime(Start) + '-' + FormatTime(End);
     }
 
     /// <summary>
     /// Formats the time properly
     /// </summary>
     /// <param name="timefield"></param>
     /// <returns></returns>
     public static string FormatTime(TimeSpan timefield) {
         if (timefield.Hours == 0 && timefield.Minutes == 0) {
             return "24:00";
         } else {
             return timefield.Hours.ToString().PadLeft(2, '0') + ":" + timefield.Minutes.ToString().PadLeft(2, '0');
         }
     }
 
     /// <summary>
     /// Sets the hours and minutes of the start
     /// </summary>
     /// <param name="h"></param>
     /// <param name="m"></param>
     public void SetStart(int h, int m) {
         Start = new TimeSpan(h, m, 0);
     }
 
     /// <summary>
     /// Sets the hours and minutes of the end
     /// </summary>
     /// <param name="h"></param>
     /// <param name="m"></param>
     public void SetEnd(int h, int m) {
         End = new TimeSpan(h, m, 0);
     }
 
     /// <summary>
     /// Checks equality
     /// </summary>
     /// <param name="obj">another TimeRange instance</param>
     /// <returns></returns>
     public override bool Equals(object obj) {
         if (obj == null) return false;
         if (obj.GetType() != GetType()) return false;
         TimeRange tr = (TimeRange)obj;
         return ToString() == tr.ToString();
     }
 
     /// <summary>
     /// Checks if this timerange clashes with another one.
     /// <constraint>bounds are exclusive</constraint>
     /// </summary>
     /// <param name="other"></param>
     /// <returns>true if there is a clash</returns>
     public bool Clashes(TimeRange other) {
         return Clashes(other, false);
     }
 
     /// <summary>
     /// Checks if this timerange clashes with another one.
     /// </summary>
     /// <param name="other">The timerange to compare</param>
     /// <param name="inclusive">Use Inclusive or SemiExclusive Boundaries</param>
     /// <returns></returns>
     public bool Clashes(TimeRange other, bool inclusive) {
         if (inclusive) {
             return (other.Start <= Start && other.End >= End) ||
                 (other.Start < Start && other.End >= Start) ||
                 (other.End > End && other.Start <= End) ||
                 (other.Start >= Start && other.End <= End);
         } else {
             return (other.Start < Start && other.End > End) ||
                 (other.Start < Start && other.End > Start) ||
                 (other.End > End && other.Start < End) ||
                 (other.Start >= Start && other.End <= End);
         }
     }
 
     /// <summary>
     /// if Equals is overriden this operator needs to overloaded
     /// </summary>
     /// <param name="left"></param>
     /// <param name="right"></param>
     /// <returns></returns>
     public static bool operator ==(TimeRange left, TimeRange right) {
         if ((object)left == null && (object)right == null) {
             return true;
         } else if ((object)left != null && (object)right != null) {
             return left.ToString() == right.ToString();
         }
         return false;
     }
 
     /// <summary>
     /// if Equals is overriden this operator needs to overloaded
     /// </summary>
     /// <param name="left"></param>
     /// <param name="right"></param>
     /// <returns></returns>
     public static bool operator !=(TimeRange left, TimeRange right) {
         return !(left == right);
     }
 
     /// <summary>
     /// Checks if a given time value is within the bounds of the instance
     /// <constraint>Bounds are inclusive!</constraint>
     /// </summary>
     /// <param name="timespan"></param>
     /// <returns></returns>
     public bool IsIn(TimeSpan timespan) {
         return (Start.Ticks <= timespan.Ticks && End.Ticks >= timespan.Ticks);
     }
 
     /// <summary>
     /// Checks if a given timerange is within the bounds of the instance
     /// <constraint>Bounds are inclusive</constraint>
     /// </summary>
     /// <param name="timespan"></param>
     /// <returns></returns>
     public bool IsIn(TimeRange timerange) {
         return IsIn(timerange.Start) && IsIn(timerange.End);
     }
 
     /// <summary>
     /// Tries to parse a given string into a TimeRange object
     /// Strings of the following format are parsed:
     /// - 1:00-12:20
     /// - 1:00:10-12:20:00
     /// - 01:00-12:30
     /// - 1:0-12:30
     /// - 100-1230
     /// - 0100-1230
     /// - 1-1230
     /// </summary>
     /// <param name="timeRangeString"></param>
     /// <returns>null if could not be parsed</returns>
     public static TimeRange Parse(string input) {
         string[] parts = input.Split('-');
         if (parts.Length == 2) {
             TimeSpan? start = ParseTimeSpan(parts[0]);
             TimeSpan? end = ParseTimeSpan(parts[1]);
             if (start == null || end == null || start.Value > end.Value) return null;
             return new TimeRange((start ?? new TimeSpan()), (end ?? new TimeSpan()));
         }
         return null;
     }
 
     /// <summary>
     /// Takes a given string which represents a time (hours and minutes) and tries to parse it into a Timespan.
     /// It recognizes strings in the following formats: 10:30, 10, 10:0, 10:3, 1030
     /// </summary>
     /// <param name="input">your string which should be parsed</param>
     /// <returns>if cannot parse then null is returned</returns>
     public static TimeSpan? ParseTimeSpan(string input) {
         string[] parts = input.Split(':');
         int h = 0, m = 0;
         if (parts.Length >= 2) {
             if (int.TryParse(parts[0], out h) && int.TryParse(parts[1], out m)) {
                 if (h > 24 || m > 60) return null;
                 return new TimeSpan(h, m, 0);
             }
             return null;
         } else if (input.Length > 0) {
             switch (input.Length) {
                 case 1:
                     if (int.TryParse(input, out h)) {
                         if (h > 24) return null;
                         return new TimeSpan(h, 0, 0);
                     }
                     break;
                 case 2:
                     goto case 1;
                 case 3:
                     goto case 4;
                 case 4:
                     string minutes = input.Substring(input.Length - 2, 2);
                     string hours = input.Substring(0, ((input.Length == 3) ? 1 : 2));
                     if (int.TryParse(hours, out h) && int.TryParse(minutes, out m)) {
                         if (h > 24 || m > 60) return null;
                         return new TimeSpan(h, m, 0);
                     }
                     break;
             }
         }
         return null;
     }
 
     #region Properties
 
     private TimeSpan _start;
     private TimeSpan _end;
 
     /// <summary>
     /// Gets/Sets the Start bound
     /// <constraint>Must be before end</constraint>
     /// <constraint>Minimum is 00:00; Maximum is 23:59</constraint>
     /// </summary>
     public TimeSpan Start {
         get { return _start; }
         set {
             if (_initialized) {
                 if (value.TotalMinutes > TimeRange.MinutesOfTheDay - 1) throw new ArgumentException("Min. 00:00, Max. 23:59");
                 if (value >= End) throw new ArgumentException("Start must be before End.");
             }
             _start = value;
         }
     }
 
     /// <summary>
     /// Gets/Sets the End bound
     /// <constraint>must be after start</constraint>
     /// <constraint>Minimum is 00:01; Maximum is 24:00</constraint>
     /// </summary>
     public TimeSpan End {
         get { return _end; }
         set {
             if (_initialized) {
                 if (value.TotalMinutes < 1 || value.TotalMinutes > TimeRange.MinutesOfTheDay) throw new ArgumentException("Min. 00:01, Max. 24:00");
                 if (value <= Start) throw new ArgumentException("End must be after Start.");
             }
             _end = value;
         }
     }
 
     #endregion
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Need help with adding a function to EventTrigger from editor script 0 Answers

Multiple collider/areas on Board ?, 0 Answers

inspector button to capture a new selected object in scene or hierarchy view 0 Answers

Information about when it is destroyed any GameObject on scene. 0 Answers

Select GameObjects OnSceneView without Colliders 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges