using System; using System.Collections; using System.Collections.Generic; using Unity.Collections.LowLevel.Unsafe; using Unity.VisualScripting; using UnityEditor.Animations; using UnityEngine; using UnityEngine.Rendering.PostProcessing; /* 本代码控制室内场景 * 控制宠物在Home场景动画 * !!!特别注意:Dog Initializer 必须挂载在同一个组件下,并且必须在本组价下方。确保比本组件先执行 * 主要调节参数在FixedUpdate代码段里面 */ public class HomeController : MonoBehaviour { private List dogsInScene = new List(); private List existCouroutines; public static bool listenBreak = false; // 当按下说话按键后,所有狗停止行动,立刻切换到监听状态。 private bool isInitialDone = false; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { } // Update is called once per frame void FixedUpdate() { if (!isInitialDone) { InitialScene(); // 判断是否在睡觉时间 DateTime dateTime = DateTime.Now; foreach (var dog in dogsInScene) { if (dateTime.Hour >= 22 || dateTime.Hour <= 5) // 深夜模式,狗默认在睡觉状态 { dog.Sleep(); } else { dog.StartAnimation(); } } isInitialDone = true; } // 生成一个数据数用于随机开启动画,如果和狗的randomFactor相同就开启动画 int randomCheck = UnityEngine.Random.Range(0, 101); foreach (var dog in dogsInScene) { if (listenBreak) // 如果用户按下说话按键,立刻切换到监听状态 { dog.Listen(); } else if (dog.isMoving) { dog.Move(); } else if (randomCheck == dog.randomFactor) // 当狗自身的随机数和系统随机数相同时候触发。约100秒触发一次。 { TimeSpan ts = DateTime.Now - dog.animationStartTime; if (ts.Seconds >= 30) // 如果距离上一个动作超过30秒就可以开始新的动作 { float r = UnityEngine.Random.Range(0, 1f); if (r > 0.6) // 随机选择开始动画,或者移动 { dog.StartAnimation(); } else // 狗狗开始步行移动 { dog.SetMoveSpeed(0); dog.Move(); } } } } } //void AniOrWalk(DogInScene dog) // 狗在普通状态下,随机或播放动画,或移动 //{ //} // 初始化场景,加载所有的狗,并配置components,添加到dogsInScene List里面去 void InitialScene() { // foreach (var dog in UserProperty.dogs) { DogInScene dogInScene = new DogInScene(dog); float x = UnityEngine.Random.Range(-5f, 5f); // 随机生成位置 float z = UnityEngine.Random.Range(-5f, 5f); float y = UnityEngine.Random.Range(0, 360f); var initPosition = new Vector3(x, 0, z); StartCoroutine(DogComponentAdd(dog)); // 加载狗的其他组件 var dogGameObject = GameObject.Find(dog.name); dogGameObject.transform.position = initPosition; dogGameObject.transform.rotation = Quaternion.Euler(0, y, 0); dogGameObject.transform.localScale = new Vector3(2, 2, 2); dogInScene.SetGameObject(dogGameObject); dogsInScene.Add(dogInScene); } } // 加载狗的其他组件 IEnumerator DogComponentAdd(DogProperty dogProperty) { // 等待一帧,确保所有 Start() 方法都执行完成 yield return null; // 第一帧以后开始执行 GameObject dog = GameObject.Find(dogProperty.name); // 加载指定的Animator controller Animator animator = dog.GetComponent(); AnimatorController animatorController = new AnimatorController(); if (dogProperty.breed == "shibaInu") { animatorController = Resources.Load("Dog/AnimatorController/shibaInu/HomeDogAnimatorController"); } animator.runtimeAnimatorController = animatorController; // 加载bbx collider BoxCollider boxCollider = dog.AddComponent(); boxCollider.isTrigger = true; boxCollider.center = new Vector3(0, 0.225f, 0); boxCollider.size = new Vector3(0.2f, 0.45f, 0.6f); } } // 本类用于管理场景中所有狗的状态包括动画状态,随机数分配等 public class DogInScene { public DogProperty dogProperty; //public Vector3 location, rotation, scale; public int randomFactor = UnityEngine.Random.Range(0, 101); // 生成一个随机整数(0 到 100 之间),用于时间校验 public DateTime animationStartTime; // 记录上一个动画开始时间的,每个动作间隔至少30秒。 private int activeIndex; // 动物的活动指数 private GameObject gameObject { set; get; } private Animator animator; private Vector3 moveToLocation; public bool isMoving; private float moveSpeed; // 用来控制狗的移动速度 0 0.5跑,0.75快跑,1跳 public void SetGameObject(GameObject gameObject) { this.gameObject = gameObject; this.animator = gameObject.GetComponent(); } public DogInScene(DogProperty property) { this.dogProperty = property; this.activeIndex = (int)Math.Round((property.liveliness + property.intimate) * UnityEngine.Random.Range(0.3f, 0.7f)); this.isMoving = false; } public void StartAnimation() { this.animationStartTime = DateTime.Now; this.animator.SetInteger("activeIndex", activeIndex); Debug.Log("activeIndex:" + this.activeIndex); float randomIndex = UnityEngine.Random.Range(0, 1f); this.animator.SetFloat("randomIndex", randomIndex); Debug.Log("randomIndex:" + randomIndex); } public void SetMoveSpeed(float speed) { this.moveSpeed = speed; } public void Move() { if (isMoving == false) { animationStartTime = DateTime.Now; // 设置动画开始时间 float x = UnityEngine.Random.Range(-5f, 5f); float z = UnityEngine.Random.Range(-5f, 5f); this.moveToLocation = new Vector3(x, 0, z); //Debug.Log("move to location:" + x + ", " + z); this.isMoving = true; this.animator.SetTrigger("move"); this.animator.SetBool("isMoving", true); this.animator.SetFloat("moveSpeed", this.moveSpeed); } this.gameObject.transform.LookAt(moveToLocation); this.gameObject.transform.position = Vector3.MoveTowards(gameObject.transform.position, moveToLocation, dogProperty.runSpeed * 0.02f * 0.01f); // 第一个0.02对应50帧fixupdate画面,后面一个数字对应速度调整 //Debug.Log("current position:" + gameObject.transform.position.x + "z:" + gameObject.transform.position.z); // 如果狗距离到达重点就停止跑步动画 float distance = Vector3.Distance(gameObject.transform.position, moveToLocation); if (distance < 0.1) { this.animator.SetBool("isMoving", false); this.isMoving = false; StartAnimation(); } } public void Sleep() { this.animator.SetTrigger("sleep"); this.animator.SetBool("isSleeping", true); } public void Listen() { //StopCoroutine(movingCoroutine); this.animator.SetTrigger("listen"); this.animator.SetBool("isListening", true); this.animator.SetBool("isMoving", false) ; this.animator.SetBool("isBarking", false); this.animator.SetBool("isSleeping", false); this.gameObject.transform.LookAt(new Vector3(0, 0, -6)); } // 用来出来音频返回结果 public void PostListen(bool result) { if (result) { // TODO 成功,狗狗跑过来, } else { this.animator.SetBool("isListening", false); } } }