DogInitialize.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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, dogName; // 狗的id
  11. // Start is called once before the first execution of Update after the MonoBehaviour is created
  12. void Start()
  13. {
  14. DogProperty puppy = new DogProperty();
  15. UserProperty.puppies.Add(puppy);
  16. dogId = "121212121";
  17. InitialDog();
  18. }
  19. // Update is called once per frame
  20. //void Update()
  21. //{
  22. //}
  23. // 初始化狗,并显示在屏幕上
  24. public void InitialDog()
  25. {
  26. DogProperty puppy = null;
  27. foreach (var p in UserProperty.puppies)
  28. {
  29. if (p.id == dogId) { puppy = p; break; }
  30. }
  31. if (puppy == null)
  32. {
  33. Debug.Assert(false); return;
  34. }
  35. GameObject dog = Instantiate(Resources.Load<GameObject>("Dog/" + puppy.breed));
  36. dog.name = dogName;
  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. }