DogComponentInstall.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using UnityEditor.Animations;
  2. using UnityEngine;
  3. using System.Collections;
  4. /* 加载playground场景下狗的特定的component
  5. */
  6. public class DogComponentInstall : MonoBehaviour
  7. {
  8. public string dogName;
  9. // Start is called once before the first execution of Update after the MonoBehaviour is created
  10. void Start()
  11. {
  12. StartCoroutine(DogComponentAdd());
  13. }
  14. // Update is called once per frame
  15. //void Update()
  16. //{
  17. //}
  18. IEnumerator DogComponentAdd()
  19. {
  20. // 等待一帧,确保所有 Start() 方法都执行完成
  21. yield return null;
  22. // 第一帧以后开始执行
  23. GameObject dog = GameObject.Find(dogName);
  24. // 加载指定的Animator controller
  25. Animator animator = dog.GetComponent<Animator>();
  26. AnimatorController animatorController = Resources.Load<AnimatorController>("Dog/AnimatorController/PlaygroundDogBehavior");
  27. animator.runtimeAnimatorController = animatorController;
  28. // 加载bbx collider
  29. BoxCollider boxCollider = dog.AddComponent<BoxCollider>();
  30. boxCollider.isTrigger = true;
  31. boxCollider.center = new Vector3(0, 0.225f, 0);
  32. boxCollider.size = new Vector3(0.2f, 0.45f, 0.6f);
  33. // 加载script <DogCatchDetection>
  34. DogCatchDetection dogScript = dog.AddComponent<DogCatchDetection>();
  35. }
  36. }