Pārlūkot izejas kodu

7/26 更新

2025/7/26 修改狗退出监听模式的代码错误
优化狗动画加载代码逻辑,未来只需要维护dogDB.json即可加入新的狗,而不需要修改代码。
Jees 20 stundas atpakaļ
vecāks
revīzija
91e80453e3

+ 10 - 2
Assets/Resources/Data/dogDB.json

@@ -14,7 +14,7 @@
                 "zh-cn": "柴犬是一种日本本土的小型犬种,以其可爱的外观和活泼的性格而闻名。"
             },
             "createDate": "2025/2/8",
-            "lastDpdate": "2025/2/8",
+            "lastDpdate": "2025/7/26",
             "cost": 1000,
             "prefab": "shibaInu",
             "skin": [
@@ -22,7 +22,15 @@
                 "black",
                 "yellow",
                 "grey"
-            ]
+            ],
+            "animation_controller": {
+                "create_dog": "Dog/AnimatorController/shibaInu/CreateDogAnimController",
+                "home_default": "Dog/AnimatorController/shibaInu/HomeDogAnimatorController",
+                "home_interact": "Dog/AnimatorController/shibaInu/HomeDogInteractController",
+                "playground": "Dog/AnimatorController/shibaInu/PlaygroundDogBehavior",
+                "walk_dog": "WalkDogs/Animation/shibaInu",
+                "bathroom": "Dog/AnimatorController/shibaInu/BathroomDogBehavior"
+            }
         }
     ]
 }

+ 6 - 2
Assets/Resources/VoiceAndManu/MenuController.cs

@@ -290,8 +290,12 @@ public class MenuController : MonoBehaviour
                 if (dog.dogProperty.d_id == focusDogId)
                 {
                     HomeController.Instance.SetInteractDog(dog.gameObject);
-                    // focusdog 开启互动模式
-                    HomeController.dogsInScene[GameData.focusDog].SetupInteract();
+                    DogInScene longPressedDog = HomeController.dogsInScene[GameData.focusDog];
+                    if (longPressedDog.dogProperty.voiceCallEnable)
+                    {
+                        // focusdog 开启互动模式
+                        longPressedDog.SetupInteract();
+                    }
                     // 其他狗进入隐藏模式
                     //foreach (var otherDog in HomeController.dogsInScene)
                     //{

+ 6 - 0
Assets/Resources/VoiceAndManu/VoiceController.cs

@@ -98,6 +98,7 @@ public class VoiceController : MonoBehaviour
         {
             if (dog.dogState == DogState.IDLE || dog.dogState == DogState.SLEEP)
             {
+                dog.dogState = DogState.IDLE; // 狗被唤醒了
                 dog.RemoveZzzParticle();
                 dog.animator.SetTrigger("listen");
                 dog.animator.SetBool("isListening", true);
@@ -136,6 +137,11 @@ public class VoiceController : MonoBehaviour
             {
                 dog.animator.SetBool("Listen_status", false);
             }
+            // 如果狗在idle状态,或者睡眠状态,设置为听从状态
+            if (dog.animator.GetBool("isListening"))
+            {
+                dog.animator.SetBool("isListening", false);
+            }
         }
         StopRecording();
     }

+ 1 - 3
Assets/Scripts/Bathroom/BathroomDogComponentInstaller.cs

@@ -32,9 +32,7 @@ public class BathroomDogComponentInstaller : MonoBehaviour
 
         // 加载指定的Animator controller
         Animator animator = dog.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/BathroomDogBehavior");
-        if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/BathroomDogBehavior"); }
-
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "bathroom"));
         animator.runtimeAnimatorController = animatorController;
 
         // 加载bbx collider

+ 26 - 4
Assets/Scripts/GameControllers/DogProperty.cs

@@ -67,14 +67,15 @@ public class DogProperty
 
 public class DogBreed
 {
-    public string breed {  get; set; }
+    public string breed { get; set; }
     public Dictionary<string, string> name { get; set; }
     public Dictionary<string, string> description { get; set; }
     public DateTime createDate { get; set; }
     public DateTime lastDpdate { get; set; }
-    public int cost {  get; set; }  
-    public string prefab {  get; set; } 
+    public int cost { get; set; }
+    public string prefab { get; set; }
     public string[] skin { get; set; }
+    public Dictionary<string, string> animation_controller { get; set; }
 }
 
 public class DogBreedController
@@ -82,7 +83,6 @@ public class DogBreedController
     // 读取狗的数据并放入EnviromentSetting.dogBreeds
     public static void LoadDogBreed()
     {
-        
         string filePath = EnviromentSetting.dogDBFilePath;
         //string jsonText = File.ReadAllText(filePath);
         // 正确的方式(跨平台工作)
@@ -102,6 +102,28 @@ public class DogBreedController
         string json2 = dogDB["dogDB"].ToString();
         EnviromentSetting.dogBreeds = JsonConvert.DeserializeObject<DogBreed[]>(json2);
     }
+
+    // 根据狗的品种和anmiation类型获取动画控制器
+    public static string GetDogAnimationController(string breed, string animationType)
+    {
+        foreach (var dogBreed in EnviromentSetting.dogBreeds)
+        {
+            if (dogBreed.breed == breed)
+            {
+                if (dogBreed.animation_controller != null && dogBreed.animation_controller.ContainsKey(animationType))
+                {
+                    return dogBreed.animation_controller[animationType];
+                }
+                else
+                {
+                    Debug.LogWarning($"动画类型 '{animationType}' 在品种 '{breed}' 中未找到。");
+                    return string.Empty;
+                }
+            }
+        }
+        Debug.LogWarning($"品种 '{breed}' 未找到。");
+        return string.Empty;
+    }
 }
 
 

+ 2 - 4
Assets/Scripts/Home/DogInScene.cs

@@ -131,8 +131,7 @@ public class DogInScene //: ScriptableObject
         }
         // 加载Interact animator controller,避免过于复杂的Animator Controller
         Animator animator = gameObject.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController");
-        if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogInteractController"); }
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "home_interact"));
         animator.runtimeAnimatorController = animatorController;
 
         float randomIndex = UnityEngine.Random.Range(0, 1f);
@@ -229,8 +228,7 @@ public class DogInScene //: ScriptableObject
         HomeController.Instance.SetDogsIsTrigger(true);
         // 恢复场景idle对应的Animator Controller
         Animator animator = gameObject.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController");
-        if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); }
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "home_default"));
         animator.runtimeAnimatorController = animatorController;
         this.RandomMove();
     }

+ 1 - 5
Assets/Scripts/Home/HomeController.cs

@@ -17,9 +17,7 @@ using UnityEngine.InputSystem;
 
 public class HomeController : MonoBehaviour
 {
-
     public static HomeController Instance;
-
     public static List<DogInScene> dogsInScene = new List<DogInScene>();
     public static bool listenBreak = false;       // 当按下说话按键后,所有狗停止行动,立刻切换到监听状态。
     public static CinemachineVirtualCamera playerCam, dogCam;
@@ -408,9 +406,7 @@ public class HomeController : MonoBehaviour
 
         // 加载指定的Animator controller
         Animator animator = dog.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController");
-        if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); }
-
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "home_default"));
         animator.runtimeAnimatorController = animatorController;
 
         // 加载Rigidbody

+ 1 - 4
Assets/Scripts/Login/InitDogUIController.cs

@@ -351,10 +351,7 @@ public class InitDogUIController : MonoBehaviour
 
         // 加载指定的Animator controller
         Animator animator = dog.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/CreateDogAnimController");
-        //AnimatorController animatorController = new AnimatorController();
-        if (InitDogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/CreateDogAnimController"); }
-
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(InitDogProperty.breed, "create_dog"));
         animator.runtimeAnimatorController = animatorController;
     }
 }

+ 1 - 4
Assets/Scripts/Playground/DogPlaygroundComponentInstall.cs

@@ -33,10 +33,7 @@ public class DogPlaygroundComponentInstall : MonoBehaviour
 
         // 加载指定的Animator controller
         Animator animator = dog.GetComponent<Animator>();
-        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/PlaygroundDogBehavior");
-        //AnimatorController animatorController = new AnimatorController();
-        if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load<RuntimeAnimatorController>("Dog/AnimatorController/shibaInu/PlaygroundDogBehavior"); }
-        
+        RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "playground"));
         animator.runtimeAnimatorController = animatorController;
 
         // 加载bbx collider

+ 2 - 5
Assets/Scripts/WalkDogs/SoundGameController.cs

@@ -76,11 +76,8 @@ public class SoundGameController : MonoBehaviour
                     break;
                 }
             }
-            RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>("WalkDogs/Animation/shibaInu");
-            if (dogProperty.breed == "shibaInu")
-            {
-                animatorController = Resources.Load<RuntimeAnimatorController>("WalkDogs/Animation/shibaInu");
-            }
+
+            RuntimeAnimatorController animatorController = Resources.Load<RuntimeAnimatorController>(DogBreedController.GetDogAnimationController(dogProperty.breed, "walk_dog"));
             animator.runtimeAnimatorController = animatorController;
 
             // 加载Rigidbody