简体   繁体   English

在 Unity 中创建游戏时间系统 C#

[英]Creating a game time system in Unity C#

I'm trying to make a game system for my game in Unity.It's a 2D fishing game that uses an image of a lake.You click somewhere to cast your rod and start fishing.I want to have time system in the game mainly because I want to represent fish "hourly activity" behavior.我正在尝试在 Unity 中为我的游戏制作一个游戏系统。这是一个使用湖泊图像的 2D 钓鱼游戏。您单击某处以投掷鱼竿并开始钓鱼。我想在游戏中加入时间系统,主要是因为我想代表鱼的“每小时活动”行为。 I wanna make it where if its early morning the fish is more active and you get more baits, or if its night time - specific types of fishes are more active.我想在清晨鱼更活跃并且你得到更多鱼饵的地方,或者如果是夜间 - 特定类型的鱼更活跃的地方。 Also, I wanna make it when the clock hit lets say 20:00 for example, to slowly start to decrease the SpriteRenderer RGB color values.另外,我想在时钟敲响时实现它,例如 20:00,慢慢开始降低 SpriteRenderer RGB 颜色值。 That "dims" the picture and it gives the immersion that is getting darker.这会使画面“变暗”,并提供越来越暗的沉浸感。 By the time its 23:00 the picture is almost entirely dark.到 23:00 时,画面几乎完全黑了。

That being said here is what im trying to do in my scripts.这里所说的是我在我的脚本中试图做的。 I have a script called TimeSystem.我有一个名为 TimeSystem 的脚本。 In there i have 2 properties "Hour" and "Minute" and 2 float fields called "timer" and minuteToRealTime to represent in game minute.在那里我有 2 个属性“Hour”和“Minute”以及 2 个名为“timer”和 minuteToRealTime 的浮点字段以表示游戏分钟。

Then, in the Update method im setting timer -= Time.deltaTime;然后,在 Update 方法中设置 timer -= Time.deltaTime; and then i begin some checks:然后我开始检查:

     if (timer <= 0)
     {
        Minute++;

        OnMinuteChanged?.Invoke();
        if (Minute >= 60)
        {
            Hour++;
            Minute = 0;

            if (Hour >= 24)
            {
                Hour = 0;
                
            }
            OnHourChanged?.Invoke();

        }

and then setting timer = minuteToRealTime;然后设置 timer = minuteToRealTime;

I do these checks to set my minute back to 0 if it hits 60 or to set the hour back to 0 if it hits 24.如果它达到 60,我会进行这些检查以将我的分钟设置回 0,或者如果它达到 24,则将小时设置回 0。

Also, im trying to use Action delegate to fire events at certain hour or minute.另外,我正在尝试使用 Action delegate 在特定的时间或分钟触发事件。 (dont know if this is the right approach at all) (根本不知道这是否是正确的方法)

So here is my problem.所以这是我的问题。

After that i do a check to see what hour it is currently and im trying to do the following:之后我检查一下现在是几点,然后我尝试执行以下操作:

if (Hour >= 20 || Hour <= 4) // if the hour is between 20:00 and 04:00 i do the darkening process
            {
                float oldRValue = _gameObject.GetComponent<SpriteRenderer>().color.r;
                float oldGValue = _gameObject.GetComponent<SpriteRenderer>().color.g;
                float oldBValue = _gameObject.GetComponent<SpriteRenderer>().color.b;

                _gameObject.GetComponent<SpriteRenderer>().color = new Color(oldRValue - 0.004f, oldGValue - 0.004f, oldBValue - 0.004f,255);
            }
            else 
            {
               //same thing here but increasing color values so the picture brighens up again until 255,255,255,255 RGB and alpha values
            }
            timer = minuteToRealTime

Basically storing the old RGB values in local variables and each frame decreasing or increasing them by the speciefied amount (i use -0.004f in the example but its just an example. if everything works correctly its easy to balance those numbers with trying different ones so the picture darkening is smoother).基本上将旧的 RGB 值存储在局部变量中,并且每帧按指定的数量减少或增加它们(我在示例中使用 -0.004f 但这只是一个示例。如果一切正常,很容易通过尝试不同的数字来平衡这些数字,所以图片变暗更平滑)。

So i start my game from lets say 12:00 clock goes up to 20:00 and then it hits the first check where the hour is >= 20:00 so it begins to darken the picture.所以我从假设 12:00 时钟上升到 20:00 开始我的游戏,然后它在第一个检查时间 >= 20:00 的地方开始,所以它开始使图片变暗。 Which works perfectly fine.哪个工作得很好。 Then in the morning when its 5:00AM the picture starts to brighten up slowly.然后在早上 5:00 时,图片开始慢慢变亮。 Tried it and tested it yesterday and it looked and felt nice everything was working accordingly... until i checked to see what will happen in the second cycle.昨天尝试并测试了它,它看起来和感觉都很好,一切都在相应地工作……直到我检查第二个周期会发生什么。 So after 5:00AM the picutre got brighter and everything was cool but when the game hit 20:00 for the second time nothing happened.因此,在凌晨 5:00 之后,画面变得更亮,一切都很酷,但是当比赛第二次打到 20:00 时,什么也没有发生。 My code only executes for the first 24 hours and when i reset the hours to 0 it stops working.. I have a textmesh attached also that shows the clock in the UI and the text representation of the time is correct.我的代码仅在前 24 小时内执行,当我将小时数重置为 0 时,它停止工作。我还附加了一个文本网格,它在 UI 中显示时钟,并且时间的文本表示是正确的。 Text is working as it should it couts the minutes to 60 then resets them to 0 and adds +1 to hour.When the hours hit 24 they are reset to 0 and the same process repeat indef.netly.文本正在正常工作,它将分钟计算为 60,然后将它们重置为 0,并将 +1 加到小时。当小时数达到 24 时,它们将重置为 0,并且相同的过程在 indef.netly 重复。 So i would've expect the "changing of the colors of the picture" to work accordingly to the hour but somehow it only works the first time... It only works one time and thats it.. when the clock hits again 20:00 the next day the picture colors dont get darker as expected.所以我希望“改变图片的 colors”能够按小时工作,但不知何故它只在第一次工作......它只工作一次,就是这样......当时钟再次敲响 20: 00 第二天图片 colors 果然没有变暗。

I wanna note that im fairly new to programming and all, i've been mainly making console apps like TicTacToe and PingPong or w/e similar games.我想指出,我对编程和所有方面都还很陌生,我主要制作控制台应用程序,例如 TicTacToe 和 PingPong 或类似的游戏。 But i wanted to step it up a little and start learning using Unity.但我想更进一步,开始学习使用 Unity。 So yeah right now I have a simple scene with the background, ui panels with 1 button that shows a map when you click it and a text mesh for the clock(which is working perfectly fine,atleast it seems).所以是的,现在我有一个简单的背景场景,带有 1 个按钮的 ui 面板,当您单击它时显示 map 和时钟的文本网格(它工作得很好,至少看起来)。 Right now im working on the Time that i want to make in my game but im stuck and cant find a solution.现在我正在处理我想在我的游戏中制作的时间,但我卡住了并且无法找到解决方案。 I've read online that its possible to use DateTime and Timespan built in structures in .NET and then updating the time each frame.. but i have no idea how to do that.我在网上读到可以使用 .NET 中内置的 DateTime 和 Timespan 结构,然后更新每一帧的时间。但我不知道该怎么做。 IF there is an easier way to make game date and time system PLEASE let me know.如果有更简单的方法来制作游戏日期和时间系统,请告诉我。 I would love to have seconds and years as well.. maybe even months could be nice, or a whole calendar system built in my game.我也希望有几秒钟和几年......也许几个月也不错,或者在我的游戏中内置一个完整的日历系统。 But.. i have no idea how to build that so i opted to try with Minute and Hour first..但是..我不知道如何构建它所以我选择先尝试使用分钟和小时..

Anyway thats a long wall of text idk if anyone will actually read this since its long AF but i hope to get some help.Please let me know if you need more info about my code but i think that i covered the most important stuff.无论如何,如果有人真正阅读它,因为它的长 AF,那是一长串文本 IDK,但我希望得到一些帮助。如果您需要有关我的代码的更多信息,请告诉我,但我认为我涵盖了最重要的内容。

In an ideal scenario i would like my clock in game to have Time represented as Hour:minute and maybe a text next to it saying Monday,Tuesday, Wednesday.. That way i could one day make in game events where if for example right now your time is 14:00 Monday i could introduce a "tournament" that starts maybe Friday 14:00.在一个理想的场景中,我希望我的游戏时钟将时间表示为小时:分钟,并且旁边可能有一个文本说星期一,星期二,星期三..这样我有一天可以在游戏活动中制作,例如现在你的时间是星期一 14:00,我可以介绍一个可能在星期五 14:00 开始的“锦标赛”。 So when it becomes Friday 14:00 ingame you could go to the tournament location and begin the fishing tournament.因此,当游戏中变成星期五 14:00 时,您可以 go 到比赛地点并开始钓鱼比赛。 But yeah thats just in my dreams for now:D但是,是的,那只是我现在的梦想:D

Anyways thank you for you time and have a great day: :)无论如何,谢谢你的时间,祝你有美好的一天::)

I got it to work with DateTime.我让它与 DateTime 一起工作。 Turned out it was easy as saying AddMinutes(1) to my current time and updating it accordingly: I got all i wanted in one script with 50 lines!I feel proud of myself and thank you again Jay for pointing me to the right direction :D事实证明,只需将 AddMinutes(1) 添加到我的当前时间并相应地更新它就很容易了:我在一个 50 行的脚本中得到了我想要的一切!我为自己感到自豪,再次感谢 Jay 为我指明了正确的方向:丁

One important thing I have to note tho, about the SpriteRenderer.Color R GB properties, they start at 1 by default.我必须注意一件重要的事情,关于 SpriteRenderer.Color R GB 属性,它们默认从 1 开始。 I thought they start at 255 and i was trying to decrease that value but that messed up my code.我以为它们从 255 开始,我试图降低该值,但这弄乱了我的代码。 Instead what you want is to substract/add (depending on your need) floating numbers like 0.004f between 0 and 1 in order to brighten or darken the image:)相反,您想要的是减去/添加(取决于您的需要)浮动数字,例如 0 和 1 之间的 0.004f,以便使图像变亮或变暗:)

Just saying that for future reference to anyone encountering that problem they might find that useful:) So this topic is resolved!只是说供将来遇到该问题的任何人参考,他们可能会发现这很有用:) 所以这个话题已经解决了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM