DogInitialize.cs 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using UnityEngine;
  2. /* 本类用于动态加载狗
  3. * 初始化狗的基础模型和颜色
  4. * 场景中特殊的components 需要在场景单独的程序中加载
  5. * 狗的breed和skin大小写需要注意
  6. */
  7. public class DogInitialize : MonoBehaviour
  8. {
  9. public Vector3 location, rotation, scale = new Vector3(1,1,1) ; // 狗初始化位置
  10. public string dogId, dogDisplayName; // 狗的id指的是调用用户拥有的狗的id dogName是狗在场景中的显示名
  11. public DogProperty dogProperty;
  12. private void Awake()
  13. {
  14. }
  15. // Start is called once before the first execution of Update after the MonoBehaviour is created
  16. void Start()
  17. {
  18. // 如果输入dog id 就优先使用dog id的数据读取
  19. if (!string.IsNullOrWhiteSpace(dogId))
  20. {
  21. GetOwnedDogById(dogId);
  22. InitialDog();
  23. }
  24. }
  25. // Update is called once per frame
  26. //void Update()
  27. //{
  28. //}
  29. // 初始化狗,并显示在屏幕上
  30. public void InitialDog()
  31. {
  32. if (this.dogProperty != null)
  33. {
  34. var puppy = this.dogProperty;
  35. GameObject dog = Instantiate(Resources.Load<GameObject>("Dog/Breed/" + puppy.breed));
  36. dog.name = dogDisplayName;
  37. if (dog != null)
  38. {
  39. GameObject dogL2 = GameObject.Find(puppy.breed);
  40. Renderer renderer = dogL2.GetComponent<Renderer>();
  41. //Texture skin = Resources.Load<Texture>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
  42. Material mat = Resources.Load<Material>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
  43. if (mat != null && renderer != null)
  44. {
  45. //renderer.material.mainTexture = skin;
  46. renderer.material = mat;
  47. }
  48. }
  49. dog.transform.localPosition = location;
  50. dog.transform.localRotation = Quaternion.Euler(rotation);
  51. dog.transform.localScale = scale;
  52. }
  53. }
  54. // 查询用户名下所有的狗
  55. private void GetOwnedDogById(string id)
  56. {
  57. DogProperty puppy = null;
  58. foreach (var p in UserProperty.dogs)
  59. {
  60. if (p.id == id) {
  61. puppy = p;
  62. }
  63. }
  64. this.dogProperty = puppy;
  65. }
  66. }