static void CreatePrefabs() { TableFish fishTable = (TableFish)GameTableManager.getInstance().GetTable("table_fish"); foreach (Object selectobj in Selection.objects) { GameObject newobj = new GameObject(); string fishname = selectobj.name.Split('_')[0]; TableFish.FishRecord record = fishTable.getRecordByName(fishname); if (record == null) { continue; } newobj.name = "Prefab_Fish_" + fishname; UITexture uitex = newobj.AddComponent <UITexture>(); uitex.mainTexture = (Texture)selectobj; uitex.MakePixelPerfect(); uitex.transform.localScale = new Vector3(record.scaleFactor, record.scaleFactor, 1); string prefabpath = "Assets" + "/Resources/FishPrefabs/" + newobj.name + ".prefab"; PrefabUtility.CreatePrefab(prefabpath, newobj); } }
// Use this for initialization void Start() { AiPathManager.getInstance().initialize(Application.dataPath + "/Resources/Configs/table_ai.bytes"); GameTableManager.getInstance().initialize(); }
public void onEventFishSeason(int seasonIndex, bool modifyFlag = false) { if (modifyFlag) { this.onModifyOneSeason(seasonIndex); return; } GameObject sourcePoint = GameObject.Find("Anchor_BottomLeft"); Transform objBottomLeft = sourcePoint.transform; int childCnt = objBottomLeft.childCount; for (int i = 0; i < childCnt;) { Transform child = objBottomLeft.GetChild(i); GameObject.DestroyImmediate(child.gameObject); childCnt = objBottomLeft.childCount; } OneFishSeason season = FishConfigManager.getInstance().getOneSeason(seasonIndex); TableFish fishtable = (TableFish)GameTableManager.getInstance().GetTable("table_fish"); foreach (FishSeasonInfo seasoninfo in season.seasonInfoList) { foreach (SingleFishOfSeason singlefish in seasoninfo.fishList) { TableFish.FishRecord record = fishtable.getRecordByFishKindId(singlefish.mFishKindId); if (record == null) { continue; } float fFishLength = record.width; //Debug.Log(record.name); GameObject fishObj = (GameObject)GameObject.Instantiate(Resources.Load("FishPrefabs/Prefab_Fish_" + record.name)); fishObj.transform.parent = sourcePoint.transform; fishObj.transform.localScale = Vector3.one * record.scaleFactor; //fishObj.transform.localPosition = new Vector3(singlefish.mFishPos.x + seasoninfo.mCenterPoint.x-1200,singlefish.mFishPos.y + seasoninfo.mCenterPoint.y,0); Fish fishComponent = fishObj.AddComponent <Fish>(); fishComponent.Rotation = seasoninfo.mAngle; fishComponent.AiPath = AiPathManager.getInstance().getPath(seasoninfo.mAiId); fishComponent.BaseSpeed = seasoninfo.mSpeed; fishComponent.FishWidth = record.width; float fDelay = 0.0f; if (seasoninfo.mCenterPoint.x <= 0) { fishObj.transform.localPosition = new Vector3(-fFishLength, seasoninfo.mCenterPoint.y + singlefish.mFishPos.y, 0); //fishObj.transform.localPosition.y = seasoninfo.mCenterPoint.y + singlefish.mFishPos.y; fDelay = (singlefish.mFishPos.x - seasoninfo.mCenterPoint.x) / seasoninfo.mSpeed; } else if (seasoninfo.mCenterPoint.x >= 1280) { fishObj.transform.localPosition = new Vector3(1280 + fFishLength, seasoninfo.mCenterPoint.y + singlefish.mFishPos.y, 0); //fishObj.transform.localPosition.y = seasoninfo.mCenterPoint.y + singlefish.mFishPos.y; fDelay = (singlefish.mFishPos.x + seasoninfo.mCenterPoint.x - 1280) / seasoninfo.mSpeed; } else if (seasoninfo.mCenterPoint.y <= 0) { fishObj.transform.localPosition = new Vector3(seasoninfo.mCenterPoint.x + singlefish.mFishPos.x, -fFishLength, 0); //fishObj.transform.localPosition.y = (-1) * fFishLength; fDelay = (singlefish.mFishPos.y - seasoninfo.mCenterPoint.y) / seasoninfo.mSpeed; } else if (seasoninfo.mCenterPoint.y >= 720) { fishObj.transform.localPosition = new Vector3(seasoninfo.mCenterPoint.x + singlefish.mFishPos.x, 720 + fFishLength, 0); //fishObj.transform.localPosition.y = 720 + fFishLength; fDelay = (singlefish.mFishPos.y + seasoninfo.mCenterPoint.y - 720) / seasoninfo.mSpeed; } else { fishObj.transform.localPosition = new Vector3(seasoninfo.mCenterPoint.x + singlefish.mFishPos.x, seasoninfo.mCenterPoint.y + singlefish.mFishPos.y, 0); //fishObj.transform.localPosition.y = (seasoninfo.mCenterPoint.y + singlefish.mFishPos.y); fDelay = 0.0f; } Vector3 pos = fishObj.transform.localPosition; fishObj.transform.localPosition = new Vector3(pos.x, 720 - pos.y, 0); if (seasonIndex == 4) { Debug.Log(fishObj.transform.localPosition + " " + fDelay); } fishComponent.DelayActiveTime = fDelay; } } }
public void onModifyOneSeason(int seasonIndex) { GameObject seasonRoot = GameObject.FindWithTag("SeasonRoot"); Transform objRoot = seasonRoot.transform; int childCnt = objRoot.childCount; for (int i = 0; i < childCnt;) { Transform child = objRoot.GetChild(i); GameObject.DestroyImmediate(child.gameObject); childCnt = objRoot.childCount; } OneFishSeason season = FishConfigManager.getInstance().getOneSeason(seasonIndex); TableFish fishtable = (TableFish)GameTableManager.getInstance().GetTable("table_fish"); GameObject oneSeasonObj = new GameObject(); oneSeasonObj.transform.parent = objRoot; oneSeasonObj.AddComponent <OneSeasonComponent>(); oneSeasonObj.name = "OneSeason"; oneSeasonObj.transform.localScale = Vector3.one; oneSeasonObj.transform.localPosition = Vector3.zero; foreach (FishSeasonInfo seasoninfo in season.seasonInfoList) { GameObject seasonInfoObj = new GameObject(); seasonInfoObj.transform.parent = oneSeasonObj.transform; seasonInfoObj.name = "OneSeasonInfo"; seasonInfoObj.transform.localScale = Vector3.one; FishSeasonInfoComponent seasonInfoCom = seasonInfoObj.AddComponent <FishSeasonInfoComponent>(); seasonInfoCom.centerPoint.Set(seasoninfo.mCenterPoint.x, seasoninfo.mCenterPoint.y); seasonInfoCom.speed = seasoninfo.mSpeed; seasonInfoCom.aiId = seasoninfo.mAiId; seasonInfoCom.angle = seasoninfo.mAngle; seasonInfoCom.transform.localPosition = new Vector3(seasoninfo.mCenterPoint.x, -(seasoninfo.mCenterPoint.y), 0); foreach (SingleFishOfSeason singlefish in seasoninfo.fishList) { TableFish.FishRecord record = fishtable.getRecordByFishKindId(singlefish.mFishKindId); if (record == null) { continue; } float fFishLength = record.width; GameObject fishObj = (GameObject)GameObject.Instantiate(Resources.Load("FishPrefabs/Prefab_Fish_" + record.name)); fishObj.name = "Prefab_Fish_" + record.name; fishObj.transform.parent = seasonInfoObj.transform; fishObj.transform.localScale = Vector3.one * record.scaleFactor; fishObj.transform.localPosition = new Vector3(singlefish.mFishPos.x, singlefish.mFishPos.y, 0); if (seasoninfo.mCenterPoint.x <= 0) { fishObj.transform.localPosition = new Vector3(-singlefish.mFishPos.x, singlefish.mFishPos.y, 0); } else if (seasoninfo.mCenterPoint.x >= 1280) { fishObj.transform.localPosition = new Vector3(singlefish.mFishPos.x, singlefish.mFishPos.y, 0); } else if (seasoninfo.mCenterPoint.y <= 0) { fishObj.transform.localPosition = new Vector3(singlefish.mFishPos.x, -singlefish.mFishPos.y, 0); } else if (seasoninfo.mCenterPoint.y >= 720) { fishObj.transform.localPosition = new Vector3(singlefish.mFishPos.x, singlefish.mFishPos.y, 0); } } UnityEditor.EditorApplication.MarkSceneDirty(); } }
public XmlElement getElement(XmlDocument xmlDoc, XmlNode oneseason) { //创建根节点 XmlElement root = xmlDoc.CreateElement("FishSeasonInfo"); oneseason.AppendChild(root); root.SetAttribute("speed", speed.ToString()); root.SetAttribute("angle", angle.ToString()); root.SetAttribute("AIID", aiId.ToString()); root.SetAttribute("centerPt", centerPoint.x + "," + (centerPoint.y)); TableFish fishTable = (TableFish)GameTableManager.getInstance().GetTable("table_fish"); int childcnt = transform.childCount; float maxWidth = 0; int maxWidthId = 0; float minDistanceToView = float.MaxValue; float nearestFishWidth = 0; for (int i = 0; i < childcnt; i++) { Transform childTransform = transform.GetChild(i); string name = childTransform.gameObject.name; if (name.Contains("Prefab_Fish_") == false) { continue; } name = name.Remove(0, 12); int fishkindid = fishTable.getFishKindIdByName(name); if (fishkindid == -1) { continue; } TableFish.FishRecord record = fishTable.getRecordByFishKindId(fishkindid); if (record.width > maxWidth) { maxWidthId = record.fishId; maxWidth = record.width; } if (minDistanceToView > Mathf.Abs(centerPoint.x - childTransform.localPosition.x)) { minDistanceToView = childTransform.localPosition.x; nearestFishWidth = record.width; } } float radian = Mathf.Deg2Rad * (-angle); for (int i = 0; i < childcnt; i++) { Transform childTransform = transform.GetChild(i); Vector3 pos = childTransform.localPosition; string name = childTransform.gameObject.name; if (name.Contains("Prefab_Fish_") == false) { continue; } name = name.Remove(0, 12); XmlElement singlefish = xmlDoc.CreateElement("SingleFishOfSeason"); int fishkindid = fishTable.getFishKindIdByName(name); if (fishkindid == -1) { continue; } singlefish.SetAttribute("FishKindID", fishkindid.ToString()); TableFish.FishRecord record = fishTable.getRecordByFishKindId(fishkindid); float x0 = (pos.x) * Mathf.Cos(radian) - (pos.y) * Mathf.Sin(radian); float y0 = (pos.x) * Mathf.Sin(radian) + (pos.y) * Mathf.Cos(radian); float dh = 0; //childTransform.localPosition = new Vector3(x0,y0,0); float temp = maxWidth - record.width - (maxWidth - nearestFishWidth); if (fishkindid == maxWidthId) { temp = -(maxWidth - nearestFishWidth); } if (centerPoint.x <= 0) { //singlefish.SetAttribute("FishPos", (-(x0 / Mathf.Cos(radian))).ToString() + "," + (-(y0 + Mathf.Tan(radian) * Mathf.Abs(x0) - record.width * Mathf.Tan(radian))).ToString()); singlefish.SetAttribute("FishPos", (-pos.x + temp).ToString() + "," + (-pos.y).ToString()); } else if (centerPoint.x >= 1280) { singlefish.SetAttribute("FishPos", (pos.x + temp).ToString() + "," + (-pos.y).ToString()); } else if (centerPoint.y <= 0) { singlefish.SetAttribute("FishPos", (pos.x).ToString() + "," + (pos.y + temp).ToString()); } else if (centerPoint.y >= 720) { singlefish.SetAttribute("FishPos", (pos.x).ToString() + "," + (-pos.y + temp).ToString()); } root.AppendChild(singlefish); } return(root); }