For AI agents: the complete documentation index is available at /npc-mentality/zh/llms.txt, the full documentation bundle is available at /npc-mentality/zh/llms-full.txt, and this page is available as Markdown at /npc-mentality/zh/usage/world-time.md.
  • 简体中文
  • 时间流逝世界系统

    根据游戏内时间流逝自动管理商店开关和 NPC 时间表。

    概述

    08:00 → 商店开门
    12:00 → NPC 用餐
    22:00 → 商店关门
    02:00 → 小偷出现

    组件

    WorldTimeSystem — MonoBehaviour 单例。每个场景只放一个。

    字段类型默认值说明
    TimeScalefloat1f时间倍速
    CurrentHourfloat0f当前时间(0~24,只读)

    API

    WorldTimeSystem.Instance.Register(
        "Shop",
        openHour: 8,
        closeHour: 22,
        onOpen:  () => shopkeeper.Open(),
        onClose: () => shopkeeper.Close()
    );
    
    if (WorldTimeSystem.Instance.IsOpen("Shop"))
        ShowShopUI();
    
    WorldTimeSystem.Instance.OnHourChanged += hour =>
        Debug.Log($"当前时间: {Mathf.FloorToInt(hour):D2}:00");

    示例

    using NpcMentality;
    
    public class VillageScheduler : MonoBehaviour
    {
        public CrowdNPC[] villagers;
        public GameObject thiefPrefab;
    
        private void Start()
        {
            var wt = WorldTimeSystem.Instance;
            wt.Register("Day", openHour: 6, closeHour: 22,
                onOpen:  () => { foreach (var v in villagers) v.enabled = true; },
                onClose: () => { foreach (var v in villagers) v.enabled = false; });
    
            wt.Register("Thief", openHour: 2, closeHour: 4,
                onOpen:  () => Instantiate(thiefPrefab, GetAlleyPosition(), Quaternion.identity),
                onClose: () => DespawnThieves());
        }
    }