A method for playing random dialogues of NPCs in a Unity3D game
Patent Information
- Application Number
- CN202311721450.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-14
- Publication Date
- 2026-09-22
- Estimated Expiration
- 2043-12-14
AI Technical Summary
如果使用AI的方案来实现台词演播,又会带来比较高的开发成本和运营成本,性价比不高
[0024]采用上述技术方案后,本发明与背景技术相比,具有如下优点:本发明使用秒更新函数来进行台词组冷却时间的计算,而不是使用定时器,减少定时器的使用,性能更好,不需要对定时器进行移除,减少内存泄漏的可能性。使用台词组的类实例进行同一台词组下单位的管理,不需要关心单位自身的移除,耦合性较低。加入台词播放条件、台词播放概率、台词播放初始延时、随机播放间隔、随机播放单位个数等要素,在不使用AI的情况下能够给台词播放带来比较大的随机性,使表现上更加随机、丰富、不机械,场景中的表现能更加灵动。同样的逻辑能够用在单一单位上,可减少开发成本。
Smart Images

Figure CN117679730B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of game technology, and in particular to a method for NPCs to play random dialogue in Unity3D games. Background Technology
[0002] Traditional script playback methods involve pre-setting several lines of dialogue and then designating a specific unit to play a randomized version. If multiple units share the same set of pre-set lines, each unit needs to perform a randomization calculation, and since units operate independently, this can result in multiple repetitive lines being performed simultaneously, appearing mechanical and unnatural. Using AI-based script playback solutions incurs high development and operational costs, making it inefficient. Furthermore, removing a unit requires timer cleanup, which can easily lead to memory leaks and other bugs, resulting in poor stability. Summary of the Invention
[0003] To address the aforementioned problems, this invention provides a method for NPCs in Unity3D games to play random dialogue.
[0004] The present invention adopts the following technical solution:
[0005] A method for NPCs to play random dialogue in a Unity3D game includes the following steps:
[0006] S1. Configure the dialogue groups and dialogues separately;
[0007] S2. When the game starts, the unit dialogue manager initializes the single unit loop dialogue list and the dialogue group dialogue list; wherein, the single unit loop dialogue list and the dialogue group dialogue list each store several dialogue class instances;
[0008] S3. The unit dialogue manager binds a second update function. When executed, it iterates through the dialogue class instances in the single unit loop dialogue list and the dialogue group dialogue list, and calls the second update function of the instance.
[0009] S4. Several units call the unit dialogue manager according to the content configured in the dialogue group, so that the dialogue of several units can be played randomly.
[0010] Preferably, the content configured in step S1 for the dialogue group includes playback probability, dialogue interval, initial delay, and the number of units spoken each time; the playback probability is the probability of playing the dialogue after each cooldown period; the dialogue interval is the time interval between each dialogue playback, which is a fixed time or a random time within an interval; the initial delay is a delay after the dialogue group is initialized before it starts working; and the number of units spoken each time is the number of units randomly selected from the dialogue group each time a dialogue is played.
[0011] Preferably, the dialogue configuration in step S1 includes the dialogue group to which the dialogue belongs, weight, dialogue type, text, image bubble, duration, and playback conditions; the dialogue group to which the dialogue belongs is the designated dialogue group; the weight is the weight value during random playback; the dialogue type is text or image; the text is the content of the dialogue; the image bubble is a configuration reference for the image dialogue, used to read the image path of the image dialogue and scale it to display the image; the duration is the duration of a single line of dialogue; and the playback conditions are the conditions in the random options for the dialogue to enter the dialogue group.
[0012] Preferably, step S3 includes the following steps:
[0013] S31. In the game's global second update function, call the second call function OnSecUpdate in the unit dialogue manager;
[0014] S32. In the second-call function OnSecUpdate, iterate through the single-unit loop list of lines and the list of lines in the group of lines respectively, and get the line class instance in each of the two lists.
[0015] S33. Call the GetTalkerCount method of each dialogue class instance to determine if there is a unit in the single-unit loop dialogue list of the dialogue class instance that needs to play dialogue. If so, call the second call function OnSecUpdate of the dialogue class instance.
[0016] Preferably, the second-call function OnSecUpdate in step S33 performs the following steps:
[0017] S331. A dialogue class instance records a time. Each time the second is called, the recorded time is incremented by 1 second. When the recorded time exceeds the buffer time, the dialogue class instance will play the dialogue once.
[0018] S332. Iterate through the talker list once and check if any units have been destroyed. If they have been destroyed, remove the destroyed units from the talker list to prevent invalid units from participating in the dialogue playback and to achieve unit filtering.
[0019] S333. After unit filtering, if there are still units that can play the lines, the Talk() method in the line class instance will be called, the recorded time will be reset to 0, and RandomTalkIntervalTime will be called to randomly obtain the buffer time for the next line playback.
[0020] Preferably, the Talk() method in step S333 is as follows: Based on the playback probability configured in the dialogue group, determine whether a dialogue will be played this time; if it is determined that a dialogue will be played this time, call the member method RandomTalkerCount; obtain a random number of dialogue units to be played according to the configuration, and then call RandomTalker to randomly obtain that number of units from the talker unit list according to the corresponding number; if the random number exceeds the total number of units, then remove all units; obtain dialogues that meet the playback conditions according to the configuration of the dialogue group, select the dialogue configuration to be played by this unit according to the configured weight, assemble the data, call the unit's ShowBubble method, and display it in the game.
[0021] Preferably, in step S4, a single unit calls the AddSingleLoopTalker(unit,group,isOnly) method in the unit dialogue manager according to the content configured in the dialogue group. This creates a dialogue class instance belonging to the specified unit in the single unit's loop dialogue list and creates dialogue class instances of different dialogue groups for the unit, so that the single unit can randomly play the dialogue in multiple dialogue groups.
[0022] Preferably, in step S4, multiple units call the AddGroupTalker(group,talker) method in the unit dialogue manager according to the content configured in the dialogue group. First, it is determined whether there is already a dialogue class instance corresponding to the dialogue group. If not, a dialogue class instance is created, and then the specified unit is added to the unit list of the dialogue class instance so that multiple units can play the dialogue in a dialogue group randomly. The units are coupled. If the number of units specified to play the dialogue is less than the number of units in the unit list, a number of units will be randomly specified to play the dialogue.
[0023] Preferably, in step S4, when a unit needs to be added to a certain dialogue group, the AddGroupTalker(group, talker) method in the unit dialogue manager is called to determine whether a dialogue class instance of the dialogue group already exists. If not, a dialogue class instance is created, and then the specified unit is added to the dialogue group. When a unit needs to be removed from a certain dialogue group in the game, the RemoveGroupTalker(group, talker) method in the unit dialogue manager is called to remove the unit from the dialogue class instance of the dialogue group. When a dialogue class instance needs to be removed directly in the game, the RemoveAGroupTalk(group) method in the unit dialogue manager is called to destroy the corresponding dialogue class instance from the dialogue list of the dialogue group.
[0024] By adopting the above technical solution, this invention has the following advantages compared with the prior art: This invention uses a second-update function to calculate the cooldown time of the dialogue group, instead of a timer, reducing the use of timers, improving performance, and eliminating the need to remove timers, thus reducing the possibility of memory leaks. Using a class instance of the dialogue group to manage units within the same dialogue group eliminates the need to worry about removing units themselves, resulting in lower coupling. By incorporating elements such as dialogue playback conditions, dialogue playback probability, initial dialogue playback delay, random playback interval, and the number of random playback units, significant randomness can be introduced into dialogue playback without using AI, making the performance more random, richer, and less mechanical, and allowing for more dynamic scene presentation. The same logic can be applied to a single unit, reducing development costs. Attached Figure Description
[0025] Figure 1 This is a flowchart of the method of the present invention;
[0026] Figure 2 This is a schematic diagram illustrating the working principle of the dialogue set in this invention. Detailed Implementation
[0027] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0028] like Figures 1 to 2 As shown, a method for NPCs to play random dialogue in a Unity3D game includes the following steps:
[0029] S1. Configure the dialogue groups and dialogues separately;
[0030] The content configured in step S1 for the dialogue group includes the playback probability, dialogue interval, initial delay, and number of units spoken each time; the playback probability is the probability of playing the dialogue after each cooldown period; the dialogue interval is the time interval between each dialogue playback, which can be a fixed time or a random time within an interval; the initial delay is a delay after the dialogue group is initialized before it starts working; the number of units spoken each time is the number of units randomly selected from the dialogue group each time a dialogue is played.
[0031] The dialogue configuration in step S1 includes the dialogue group to which the dialogue belongs, weight, dialogue type, text, image bubble, duration, and playback conditions; the dialogue group to which the dialogue belongs is the designated dialogue group; the weight is the weight value during random playback; the dialogue type is text or image; the text is the content of the dialogue; the image bubble is a configuration reference for the image dialogue, used to read the image path of the image dialogue and scale it to display the image; the duration is the duration of a single line of dialogue; and the playback conditions are the conditions in the random options for the dialogue to enter the dialogue group.
[0032] S2. When the game starts, the unit dialogue manager initializes the single unit loop dialogue list and the dialogue group dialogue list; wherein, the single unit loop dialogue list and the dialogue group dialogue list each store several dialogue class instances;
[0033] S3. The unit dialogue manager binds a second update function. When executed, it iterates through the dialogue class instances in the single unit loop dialogue list and the dialogue group dialogue list, and calls the second update function of the instance.
[0034] Step S3 includes the following steps:
[0035] S31. In the game's global second update function, call the second call function OnSecUpdate in the unit dialogue manager;
[0036] S32. In the second-call function OnSecUpdate, iterate through the single-unit loop list of lines and the list of lines in the group of lines respectively, and get the line class instance in each of the two lists.
[0037] S33. Call the GetTalkerCount method of each dialogue class instance to determine if there is a unit in the single unit loop dialogue list in the dialogue class instance that needs to play dialogue. If so, call the second call function OnSecUpdate of the dialogue class instance.
[0038] The second-by-second call function OnSecUpdate described in step S33 performs the following steps:
[0039] S331. A dialogue class instance records a time. Each time the second is called, the recorded time is incremented by 1 second. When the recorded time exceeds the buffer time, the dialogue class instance will play the dialogue once.
[0040] S332. Iterate through the talker list once and check if any units have been destroyed. If they have been destroyed, remove the destroyed units from the talker list to prevent invalid units from participating in the dialogue playback and to achieve unit filtering.
[0041] S333. After unit filtering, if there are still units that can play the lines, the Talk() method in the line class instance will be called, the recorded time will be reset to 0, and RandomTalkIntervalTime will be called to randomly obtain the buffer time for the next line playback.
[0042] The Talk() method described in step S333 is as follows: Based on the playback probability configured in the dialogue group, determine whether a dialogue will be played this time; if it is determined that a dialogue will be played this time, call the member method RandomTalkerCount; obtain a random number of dialogue units to play based on the configuration, and then call RandomTalker to randomly obtain that number of units from the talker unit list based on the corresponding number. If the random number exceeds the total number of units, then all units are retrieved; based on the dialogue group configuration, obtain the dialogues that meet the playback conditions, select the dialogue configuration to play for this unit based on the configured weight, assemble the data, call the unit's ShowBubble method, and display it in the game;
[0043] S4. Several units call the unit dialogue manager according to the content configured in the dialogue group, so that the dialogue of several units can be played randomly.
[0044] In step S4, each unit calls the AddSingleLoopTalker(unit,group,isOnly) method in the unit dialogue manager according to the content configured in the dialogue group. This creates a dialogue class instance belonging to the specified unit in the single unit's loop dialogue list and creates dialogue class instances of different dialogue groups for the unit, so that the single unit can randomly play the dialogue in multiple dialogue groups.
[0045] In step S4, multiple units call the AddGroupTalker(group,talker) method in the unit dialogue manager according to the content configured in the dialogue group. First, it checks whether there is already a dialogue class instance for the corresponding dialogue group. If not, it creates a dialogue class instance and then adds the specified unit to the unit list of the dialogue class instance so that multiple units can play the dialogue in a dialogue group randomly. The units are coupled. If the number of units specified to play the dialogue is less than the number of units in the unit list, a number of units will be randomly specified to play the dialogue.
[0046] In step S4, when a unit needs to be added to a dialogue group, the AddGroupTalker(group, talker) method in the unit dialogue manager is called to determine if a dialogue class instance of the dialogue group already exists. If not, a dialogue class instance is created, and the specified unit is added to the dialogue group. When a unit needs to be removed from a dialogue group in the game, the RemoveGroupTalker(group, talker) method in the unit dialogue manager is called to remove the unit from the dialogue class instance of the dialogue group. When a dialogue class instance needs to be removed directly in the game, the RemoveAGroupTalk(group) method in the unit dialogue manager is called to destroy the corresponding dialogue class instance from the dialogue list of the dialogue group.
[0047] The above description is merely a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.
Claims
1. A method for NPCs to play random dialogue in a Unity3D game, characterized by: Includes the following steps: S1. Configure the dialogue groups and dialogues separately; S2. When the game starts, the unit dialogue manager initializes the single unit loop dialogue list and the dialogue group dialogue list; wherein, the single unit loop dialogue list and the dialogue group dialogue list each store several dialogue class instances; S3. The unit dialogue manager binds a second update function. When executed, it iterates through the dialogue class instances in the single unit loop dialogue list and the dialogue group dialogue list, and calls the second update function of the instance. Step S3 includes the following steps: S31. In the game's global second update function, call the second call function OnSecUpdate in the unit dialogue manager; S32. In the second-call function OnSecUpdate, iterate through the single-unit loop list of lines and the list of lines in the group of lines respectively, and get the line class instance in each of the two lists. S33. Call the GetTalkerCount method of each dialogue class instance to determine if there is a unit in the single unit loop dialogue list in the dialogue class instance that needs to play dialogue. If so, call the second call function OnSecUpdate of the dialogue class instance. S4. Several units call the unit dialogue manager according to the content configured in the dialogue group, so that the dialogue of several units can be played randomly. In step S4, multiple units call the AddGroupTalker(group, talker) method in the unit dialogue manager according to the content configured in the dialogue group. First, it checks whether there is already a dialogue class instance for the corresponding dialogue group. If not, it creates a dialogue class instance and then adds the specified unit to the unit list of the dialogue class instance so that multiple units can randomly play the dialogue in a dialogue group. The units are coupled. If the number of units specified to play the dialogue is less than the number of units in the unit list, a number of units will be randomly specified to play the dialogue.
2. The method for NPCs to play random dialogue in a Unity3D game as described in claim 1, characterized in that: The content configured in step S1 for the dialogue group includes the playback probability, dialogue interval, initial delay, and number of units spoken each time; the playback probability is the probability of playing the dialogue after each cooldown period; the dialogue interval is the time interval between each dialogue playback, which can be a fixed time or a random time within an interval; the initial delay is a delay after the dialogue group is initialized before it starts working; the number of units spoken each time is the number of units randomly selected from the dialogue group each time a dialogue is played.
3. The method for NPCs to play random dialogue in a Unity3D game as described in claim 1, characterized in that: The dialogue configuration in step S1 includes the dialogue group to which it belongs, weight, dialogue type, text, image bubble, duration, and playback conditions; the dialogue group to which it belongs is the designated dialogue group; the weight is the weight value during random playback; the dialogue type is text or image; the text is the content of the dialogue; the image bubble is a configuration reference for the image dialogue, used to read the image path of the image dialogue and scale it to display the image; the duration is the duration of a single line of dialogue; The playback conditions are the conditions in the random options for the dialogue to enter the dialogue group.
4. The method for NPCs to play random dialogue in a Unity3D game as described in claim 1, characterized in that, The second-by-second call function OnSecUpdate described in step S33 performs the following steps: S331. A dialogue class instance records a time. Each time the second is called, the recorded time is incremented by 1 second. When the recorded time exceeds the buffer time, the dialogue class instance will play the dialogue once. S332. Iterate through the talker list once and check if any units have been destroyed. If they have been destroyed, remove the destroyed units from the talker list to prevent invalid units from participating in the dialogue playback and to achieve unit filtering. S333. After unit filtering, if there are still units that can play the lines, the Talk() method in the line class instance will be called, the recorded time will be reset to 0, and RandomTalkIntervalTime will be called to randomly obtain the buffer time for the next line playback.
5. A method for NPCs to play random dialogue in a Unity3D game as described in claim 4, characterized in that, The Talk() method described in step S333 is as follows: Based on the playback probability configured in the dialogue group, determine whether a dialogue will be played this time; if it is determined that a dialogue will be played this time, call the member method RandomTalkerCount; obtain a random number of dialogue units to play based on the configuration, and then call RandomTalker to randomly obtain that number of units from the talker unit list based on the corresponding number. If the random number exceeds the total number of units, then all units are retrieved; based on the dialogue group configuration, obtain the dialogues that meet the playback conditions, select the dialogue configuration to play for this unit based on the configured weight, assemble the data, call the unit's ShowBubble method, and display it in the game.
6. The method for NPCs to play random dialogue in a Unity3D game as described in claim 1, characterized in that, In step S4, each unit calls the AddSingleLoopTalker(unit, group, isOnly) method in the unit dialogue manager according to the content configured in the dialogue group. This creates a dialogue class instance belonging to the specified unit in the single unit's loop dialogue list and creates dialogue class instances of different dialogue groups for the unit, so that the single unit can randomly play the dialogue in multiple dialogue groups.
7. A method for NPCs to play random dialogue in a Unity3D game as described in claim 1, characterized in that: In step S4, when a unit needs to be added to a certain dialogue group, the AddGroupTalker(group, talker) method in the unit dialogue manager is called to determine whether there is already a dialogue class instance of the dialogue group. If not, a dialogue class instance is created, and then the specified unit is added to the dialogue group. When a unit needs to be removed from a dialogue group in the game, the RemoveGroupTalker(group, talker) method in the unit dialogue manager is called to remove the unit from the dialogue class instance of the dialogue group; when a dialogue class instance needs to be removed directly in the game, the RemoveAGroupTalk(group) method in the unit dialogue manager is called to destroy the corresponding dialogue class instance from the dialogue list of the dialogue group.
Citation Information
Patent Citations
Program and computer system
JP2018042749A
Individual Game Modules Collectively Unified Into A Composite Novel Word Game With Optional Gaim Variations And Word Threads And Interactive Player Formats
US20150290552A1