DogInitialize.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // Start is called once before the first execution of Update after the MonoBehaviour is created
  13. void Start()
  14. {
  15. // 如果输入dog id 就优先使用dog id的数据读取
  16. if (!string.IsNullOrWhiteSpace(dogId))
  17. {
  18. GetOwnedDogById(dogId);
  19. InitialDog();
  20. }
  21. }
  22. // Update is called once per frame
  23. //void Update()
  24. //{
  25. //}
  26. // 初始化狗,并显示在屏幕上
  27. public void InitialDog()
  28. {
  29. if (this.dogProperty != null)
  30. {
  31. var puppy = this.dogProperty;
  32. GameObject dog = Instantiate(Resources.Load<GameObject>("Dog/Breed/" + puppy.breed));
  33. dog.name = dogDisplayName;
  34. if (dog != null)
  35. {
  36. GameObject dogL2 = GameObject.Find(puppy.breed);
  37. Renderer renderer = dogL2.GetComponent<Renderer>();
  38. //Texture skin = Resources.Load<Texture>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
  39. Material mat = Resources.Load<Material>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
  40. if (mat != null && renderer != null)
  41. {
  42. //renderer.material.mainTexture = skin;
  43. renderer.material = mat;
  44. }
  45. }
  46. dog.transform.localPosition = location;
  47. dog.transform.localRotation = Quaternion.Euler(rotation);
  48. dog.transform.localScale = scale;
  49. }
  50. }
  51. // 查询用户名下所有的狗
  52. private void GetOwnedDogById(string id)
  53. {
  54. DogProperty puppy = null;
  55. foreach (var p in UserProperty.dogs)
  56. {
  57. if (p.id == id) {
  58. puppy = p;
  59. }
  60. }
  61. this.dogProperty = puppy;
  62. }
  63. }