public void SetData(PMsg_Plant plant) { FarmUnit fm = farms[FarmUnit.SeletedFarmID] as FarmUnit; Plant plant1; plant1 = DataSettingManager.SetAnwData(plant); GameObject plantGO = new GameObject("plant" + fm.FarmID); plantGO.transform.SetParent(fm.Renderer.transform, false); SpriteRenderer plantR = plantGO.AddComponent <SpriteRenderer>(); plant1.FarmID = FarmUnit.SeletedFarmID; plant1.Renderer = plantR; plant1.StartTime = plant.StartTime; plant1.IsSteal = plant.IsSteal; plant1.IsGrass = plant.IsGrass; plant1.IsWorm = plant.IsWorm; plant1.ID = plant.Id; fm.Plant = plant1; fm.Plant.Renderer.sortingLayerName = "plant"; fm.Plant.Renderer.sortingOrder = fm.Renderer.sortingOrder + 10; }
/// <summary> /// 获得unity中场景中所有创建的物体 /// </summary> public void GetFields() { foreach (Transform childTran in root.transform) { //此处作为地块ID int id = int.Parse(childTran.name.Substring(4)); SpriteRenderer LandRenderer = childTran.gameObject.AddComponent <SpriteRenderer>(); FarmUnit unit = new FarmUnit(); unit.FarmID = id; unit.Renderer = LandRenderer; farms.Add(unit.FarmID, unit); } int index = 0; //初始化位置,为层级计算做好准备 for (int i = 1; i < 7; i++) { for (int j = 1; j < 4; j++) { index++; (farms[index] as FarmUnit).Xposi = i; (farms[index] as FarmUnit).Yposi = j; } } }
/// <summary> /// 每块地被触摸调用此事件 /// </summary> /// <param name="args"></param> public void DispatchTouch(TouchEventArgs args) { if (args.Target.GetType() == typeof(FarmUnit)) { FarmUnit target = args.Target as FarmUnit; //如果当前土地已经开垦,可以点击 if (target.EnablePlant == 1) { //如果有植物,则点击植物 if (target.Plant != null) { target.Plant.Touched(args); } //点击农田 target.Touched(args); } else { if (target.Brand != null) { target.Brand.Touched(args); } } } else { WorldObject obj = args.Target as WorldObject; obj.Touched(args); } }
//地块信息输入?? /* public static Dictionary<int, FarmUnit> SetAnwData(IList<PMsg_FarmUnit> infoList) * { * Dictionary<int, FarmUnit> infos = new Dictionary<int, FarmUnit>(); * for (int i = 0; i < infoList.Count; i++) * { * PMsg_FarmUnit GenerateAnw = infoList[i]; * FarmUnit info = new FarmUnit(); * info.EnablePlant = GenerateAnw.EnablePlant; * info.Xposi = GenerateAnw.Xposi; * * info.Yposi = GenerateAnw.Yposi; * info.ReclaimCoin = GenerateAnw.ReclaimCoin; * * info.StartPlant = GenerateAnw.StartPlant; * info.Plant = SetAnwData(GenerateAnw.Plant); * SerializeProto(info,GenerateAnw.FarmField); * * * infos.Add(info.ID,info); * } * * return infos; * } */ /// <summary> /// 测试使用函数 /// </summary> /// <param name="infoList"></param> /// <returns></returns> public static Dictionary <int, FarmUnit> SetAnwData(List <FarmUnit> infoList) { Dictionary <int, FarmUnit> infos = new Dictionary <int, FarmUnit>(); for (int i = 0; i < infoList.Count; i++) { FarmUnit GenerateAnw = infoList[i]; FarmUnit info = new FarmUnit(); info.EnablePlant = GenerateAnw.EnablePlant; info.Xposi = GenerateAnw.Xposi; info.Yposi = GenerateAnw.Yposi; info.ReclaimCoin = GenerateAnw.ReclaimCoin; info.StartPlant = GenerateAnw.StartPlant; info.Plant = GenerateAnw.Plant; info.ID = GenerateAnw.ID; info.Name = GenerateAnw.Name; info.ObjectNum = GenerateAnw.ObjectNum; info.Price = GenerateAnw.Price; info.Url = GenerateAnw.Url; info.ShopTag = GenerateAnw.ShopTag; info.StoreShowTag = GenerateAnw.StoreShowTag; info.Des = GenerateAnw.Des; infos.Add(info.ID, info); } return(infos); }
//加载牌子 private void LoadBrand(int Current) { if (!brand && FieldsModel.Instance.farms.ContainsKey(Current)) { ResourceMgr.Instance.LoadResource("Prefab/brand", ( delegate(Resource resource, bool b) { if (FieldsModel.Instance.farms.ContainsKey(Current)) { //变化的选择ID,田从1开始数 FarmUnit unit = FieldsModel.Instance.farms[Current] as FarmUnit; GameObject go = GameObject.Instantiate(resource.UnityObj as GameObject); go.transform.SetParent(unit.Renderer.transform, false); go.transform.position = new Vector3(unit.Renderer.transform.position.x, unit.Renderer.transform.position.y, 1); //放入下一个地皮ID的brand unit.Brand = this; unit.Brand.Go = go; this.brand = go; } })); } else { //上一个值的农田牌子设置为空 FarmUnit unit = FieldsModel.Instance.farms[Current - 1]; unit.Brand = null; if (FieldsModel.Instance.farms.ContainsKey(Current)) { FarmUnit unit1 = FieldsModel.Instance.farms[Current]; unit1.Brand = this; Go.transform.position = unit1.Renderer.gameObject.transform.position; Go.transform.SetParent(unit1.Renderer.transform, true); Debug.Log(string.Format("<color=#ffffffff><---{0}-{1}----></color>", Current, SelectId)); } else { if (Go != null) { GameObject.Destroy(Go); } // Debug.Log(string.Format("<color=#ffffffff><---{0}-{1}----></color>", Current, "test1")); } } }
/// <summary> /// 初始化地图上的所有数据 /// </summary> /// <param name="GenerateAnw"></param> public void SetData(IList <PMsg_MapUnit> GenerateAnw) { int _brandId = 1; for (int i = 0; i < GenerateAnw.Count; i++) { PMsg_MapUnit mapUnit = GenerateAnw[i]; // Debug.Log(mapUnit.Id); FarmUnit fm = farms[mapUnit.Id] as FarmUnit; fm.EnablePlant = mapUnit.EnablePlant; if (mapUnit.EnablePlant == 1 && mapUnit.Id > _brandId) { _brandId = mapUnit.Id; } fm.FarmID = mapUnit.Id; SpriteRenderer sr = fm.Renderer; int baseOder = 100 * fm.Xposi + 50 * fm.Yposi; sr.sortingLayerName = "land"; sr.sortingOrder = baseOder; //传回来的地上植物不为空 if (mapUnit.Plant.ToString() != "") { if (fm.Plant != null) { fm.Plant.ID = mapUnit.Plant.Id; fm.Plant.FarmID = fm.FarmID; fm.Plant.IsSteal = mapUnit.Plant.IsSteal; fm.Plant.StartTime = mapUnit.Plant.StartTime; fm.Plant.IsGrass = mapUnit.Plant.IsGrass; fm.Plant.IsWorm = mapUnit.Plant.IsWorm; fm.Plant.IsWater = mapUnit.Plant.IsLessWater; BaseAtrribute ba = LoadObjctDateConfig.Instance.GetAtrribute(mapUnit.Plant.Id); fm.Plant.Name = ba.Name; } else { Plant plant = DataSettingManager.SetAnwData(mapUnit.Plant); GameObject plant1 = new GameObject("plant" + fm.FarmID); plant1.transform.SetParent(fm.Renderer.transform, false); SpriteRenderer plantR = plant1.AddComponent <SpriteRenderer>(); plant.FarmID = fm.FarmID; plant.ID = mapUnit.Plant.Id; plant.IsSteal = mapUnit.Plant.IsSteal; plant.StartTime = mapUnit.Plant.StartTime; plant.Renderer = plantR; plant.IsGrass = mapUnit.Plant.IsGrass; plant.IsWorm = mapUnit.Plant.IsWorm; plant.IsWater = mapUnit.Plant.IsLessWater; fm.Plant = plant; fm.Plant.Renderer.sortingLayerName = "plant"; fm.Plant.Renderer.sortingOrder = baseOder + 10; } } else { for (int j = 0; j < fm.Go.transform.childCount; j++) { if (fm.Go.transform.GetChild(j) && fm.Go.transform.GetChild(j).name != "brand(Clone)") { GameObject.Destroy(fm.Go.transform.GetChild(j).gameObject); } } //这块地上原来有植物 if (fm.Plant != null) { fm.Plant.destroyPlant(); fm.Plant.DisposeBadIcon(); } fm.Plant = null; } } // GC.Collect(); Brand brand = Brand.Instance; brand.SelectId = _brandId + 1; }