using UnityEngine; /* 本类用于动态加载狗 * 初始化狗的基础模型和颜色 * 场景中特殊的components 需要在场景单独的程序中加载 * 狗的breed和skin大小写需要注意 */ public class DogInitialize : MonoBehaviour { public Vector3 location, rotation, scale = new Vector3(1,1,1) ; // 狗初始化位置 public string dogId, dogDisplayName; // 狗的id指的是调用用户拥有的狗的id dogName是狗在场景中的显示名 public DogProperty dogProperty; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { // 如果输入dog id 就优先使用dog id的数据读取 if (!string.IsNullOrWhiteSpace(dogId)) { GetOwnedDogById(dogId); InitialDog(); } } // Update is called once per frame //void Update() //{ //} // 初始化狗,并显示在屏幕上 public void InitialDog() { if (this.dogProperty != null) { var puppy = this.dogProperty; GameObject dog = Instantiate(Resources.Load("Dog/Breed/" + puppy.breed)); dog.name = dogDisplayName; if (dog != null) { GameObject dogL2 = GameObject.Find(puppy.breed); Renderer renderer = dogL2.GetComponent(); //Texture skin = Resources.Load("Dog/Skin/" + puppy.breed + "/" + puppy.skin); Material mat = Resources.Load("Dog/Skin/" + puppy.breed + "/" + puppy.skin); if (mat != null && renderer != null) { //renderer.material.mainTexture = skin; renderer.material = mat; } } dog.transform.localPosition = location; dog.transform.localRotation = Quaternion.Euler(rotation); dog.transform.localScale = scale; } } // 查询用户名下所有的狗 private void GetOwnedDogById(string id) { DogProperty puppy = null; foreach (var p in UserProperty.dogs) { if (p.id == id) { puppy = p; } } this.dogProperty = puppy; } }