123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- using UnityEngine;
- /* 本类用于动态加载狗
- * 初始化狗的基础模型和颜色
- * 场景中特殊的components 需要在场景单独的程序中加载
- * 狗的breed和skin大小写需要注意
- * 本代码控制最大显示狗的数量,变量:maxDogQty
- * 本代码需要挂在到场景中任意Game Object下。建议挂载在Player下
- */
- public class DogInitialize: MonoBehaviour {
- public Vector3 position, rotation, scale = new Vector3(1,-10,1) ; // 狗初始化位置
- public string dogDisplayName; // 狗的id指的是调用用户拥有的狗的id dogName是狗在场景中的显示名
- private DogProperty dogProperty;
- public bool loadAllDogs = false; // 是否读取所有的狗,如果否就读取focusDog,否则读取全部狗
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- int maxDogQty = EnviromentSetting.maxDogQty; // 限制最大狗的数量
- while (UserProperty.dogs.Count > maxDogQty)
- {
- UserProperty.dogs.RemoveAt(UserProperty.dogs.Count - 1);
- }
- if (loadAllDogs)
- {
- foreach (var dog in UserProperty.dogs)
- {
- dogProperty = dog;
- InitialDog();
- }
- }
- else
- {
- dogProperty = UserProperty.dogs[GameData.focusDog];
- InitialDog();
- }
- }
- // Update is called once per frame
- //void Update()
- //{
-
- //}
- // 初始化狗,并显示在屏幕上
- public void InitialDog()
- {
- if (this.dogProperty != null)
- {
- var puppy = this.dogProperty;
- GameObject dogResource = Resources.Load<GameObject>("Dog/Breed/" + puppy.breed);
- if (dogResource == null)
- {
- dogResource = Resources.Load<GameObject>("Dog/Breed/shibaInu"); // 默认读取,防止后端传入数据错误
- }
- GameObject dog = Instantiate(dogResource);
- if (!string.IsNullOrWhiteSpace(dogDisplayName))
- {
- dog.name = dogDisplayName;
- }
- else
- {
- dog.name = puppy.dog_name;
- }
- if (dog != null)
- {
- GameObject dogL2;
- try
- {
- dogL2 = dog.transform.Find(puppy.breed).gameObject;
- }
- catch
- {
- dogL2 = dog.transform.Find("shibaInu").gameObject;
- }
- Renderer renderer = dogL2.GetComponent<Renderer>();
- //Texture skin = Resources.Load<Texture>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
- Material mat = Resources.Load<Material>("Dog/Skin/" + puppy.breed + "/" + puppy.skin);
- if (mat == null)
- {
- mat = Resources.Load<Material>("Dog/Skin/shibaInu/amber"); // 默认读取,防止后端传入数据错误
- }
- if (mat != null && renderer != null)
- {
- //renderer.material.mainTexture = skin;
- renderer.material = mat;
- }
- }
- dog.transform.localPosition = position;
- dog.transform.localRotation = Quaternion.Euler(rotation);
- dog.transform.localScale = scale;
- dog.tag = "dog"; // 设置tag为dog,方便后续查找
- }
- }
- }
|