using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using System.Linq; using DG.Tweening; public class ZX4Controller : MonoBehaviour { /// /// 美食预设体 /// public GameObject preFood; /// /// 烹饪预设体 /// public GameObject preOp; /// /// 选中预设体 /// public GameObject preSelected; /// /// 食材预设体 /// public GameObject preFoodMat; //步骤框中心对象 public Transform tranStepCenter; public UICircle imgSemi; /// /// 美食容器 /// public Transform foodsContainer; /// /// 食材容器 /// public Transform matContainer; /// /// 烹饪方式容器 /// public Transform opContainer; /// /// 关闭按钮 /// public Button closeButton; /// /// 返回按钮 /// public Button backButton; /// /// 开始制作按钮 /// public Button startButton; /// /// 退出制作按钮 /// public Button quitGameButton; /// /// 完成制作按钮 /// public Button okButton; /// /// 详细_图片 /// public Image foodImage; /// /// 详细_美食名称 /// public Text foodNameText; /// /// 详细_描述文本 /// public Text descriptionText; /// /// 中间圆环的半径 /// public float radius = 50f; /// /// 食材背景 /// public Sprite bgMat; /// /// 操作背景 /// public Sprite bgOp; /// /// 中间选中区背景 /// public Sprite bgCenter; private bool IsShowTip; /// /// 文件路径 /// private string basePath; /// /// 选中的食材Food组件 /// private Food selectedFood; /// /// 美食数据列表 /// private List foodDataList; /// /// 动画组件 /// private Animator animator; //提示按钮 public Button btnTip; //关闭提示框 public Button btnCloseTip; //提示框 public GameObject pnlTip; //提示制作过程文本 public Text txtTip; //提示菜谱名称 public Text txtFoodNameTip; //列表滑动限位 public GameObject mainfoodlist; public GameObject foodlistmat; public GameObject foodlistop; //步骤提示 public GameObject StepCenter; public GameObject promptMat; public GameObject promptOp; //制作动画 public GameObject cookingAnimation; public int animationmark; //卤肉饭动画 public GameObject Chaoluroufan; public GameObject Luluroufan; public GameObject Zhuanpanluroufan; //蚵仔煎动画 public GameObject Jiaobankezaijian; public GameObject Jiaobankezaijian2; public GameObject Jiankezaijian; //担仔面动画 public GameObject Jiaobandanzaimian; public GameObject Zhudanzaimian; public GameObject Jiaobandanzaimian2; //肉粽动画 public GameObject Jinpaorouzong; public GameObject Zhurouzong; public GameObject Baorouzong; public GameObject Zhurouzong2; //大肠蚵仔面动画 public GameObject Lukezaimian; public GameObject Zhukezaimian; public GameObject Zhukezaimian2; //台湾炒米粉动画 public GameObject Chaochaomifen; public GameObject Jianchaomifen; public GameObject Chaochaomifen2; //鼎边锉动画 public GameObject Jiaobandingbiancuo; public GameObject Zhudingbiancuo; public GameObject Zhuangpandingbiancuo; //深坑豆腐动画 public GameObject Jiandoufu; public GameObject Chaodoufu; public GameObject Zhudoufu; //万峦猪蹄动画 public GameObject Zhuzhuti; public GameObject Chaozhuti; public GameObject Zhuzhuti2; //烧仙草动画 public GameObject Zhengshaoxiancao; public GameObject Zhushaoxiancao; public GameObject Zhuangpanshaoxiancao; //动画素材 //卤肉饭动画素材 public GameObject luroufanTransform; public GameObject onions; public GameObject mushroom; public GameObject pork; public GameObject rice; //蚵仔煎动画素材 public GameObject kezaijianTransform; public GameObject egg; public GameObject oysters; public GameObject eggliquid; public GameObject oyster2; public GameObject onionskezaijian; //担仔面动画素材 public GameObject danzaimianTransform; public GameObject porkdanzaimian; public GameObject noodles; public GameObject noodles2; public GameObject shrimpdanzaimian; public GameObject coriander; //肉粽动画素材 public GameObject zongziTransform; public GameObject ricezongzi; public GameObject onionszongzi; public GameObject mushroomzongzi; public GameObject porkzongzi; public GameObject ricezongzi2; public GameObject leaves; public GameObject zongzi; //大肠蚵仔面动画素材 public GameObject kezaimianTransform; public GameObject intestine; public GameObject oysterskezaimian; public GameObject nooldeskezaimian; //台湾炒米粉动画素材 public GameObject chaomifenTransform; public GameObject onionschaomifen; public GameObject mushroomchaomifen; public GameObject porkchaomifen; public GameObject eggchaomifen; public GameObject flour; public GameObject onionschaomifen2; public GameObject mushroomchaomifen2; public GameObject porkchaomifen2; //鼎边锉动画素材 public GameObject dingbiancuoTransform; public GameObject powderdingbiancuo; public GameObject mushroomdingbiancuo; public GameObject shrimpdingbiancuo; public GameObject oysterdingbiancuo; public GameObject chive; //深坑豆腐动画素材 public GameObject doufuTransform; public GameObject doufu; public GameObject garlic; public GameObject doufu2; public GameObject garlic2; //万峦猪蹄动画素材 public GameObject zhutiTransform; public GameObject trotters; public GameObject candy; public GameObject trotters2; public GameObject trotters3; //烧仙草动画素材 public GameObject shaoxiancaoTransform; public GameObject potato; public GameObject ball; public GameObject pearl; public GameObject grass; public GameObject milk; public GameObject ball2; public GameObject pearl2; //动画步骤标记 public int animationloadmark; //选择食谱标记 public int currentSelected; //食谱制作步骤 public int step; //灶台锅贴图 public GameObject wok; //半成品图 //卤肉饭半成品 public GameObject semiluroufan1; public GameObject semiluroufan2; //蚵仔煎半成品 public GameObject semikezaijian1; public GameObject semikezaijian2; //担仔面半成品 public GameObject semidaizaimian1; public GameObject semidanzaimian2; //肉粽半成品 public GameObject semirouzong1; public GameObject semirouzong2; public GameObject semirouzong3; //大肠蚵仔面半成品 public GameObject semikezaimian1; public GameObject semikezaimian2; //台湾炒米粉半成品 public GameObject semichaomifen1; public GameObject semichaomifen2; //鼎边锉半成品 public GameObject semidingbiancuo1; public GameObject semidingbiancuo2; //深坑豆腐半成品 public GameObject semidoufu1; public GameObject semidoufu2; //万峦猪蹄半成品 public GameObject semizhuti1; public GameObject semizhuti2; //烧仙草半成品 public GameObject semishaoxiancao1; public GameObject semishaoxaincao2; //步骤干扰项 //正确食材 public string[] Matlist; public int Matitem; public int MatTrue; public int MatTest; //正确烹饪方法 public string[] Oplist; public int Opitem; public int OpTrue; public int OpTest; void Start() { foodDataList = new List(); animator = this.GetComponent(); //文件路径 basePath = CommonData.DataServer + "/ZX4/Data/"; //按钮事件监听 closeButton.onClick.AddListener(onCloseButtonClick); backButton.onClick.AddListener(onBackButtonClick); startButton.onClick.AddListener(onStartButtonClick); quitGameButton.onClick.AddListener(onQuitGameButtonClick); okButton.onClick.AddListener(onQuitGameButtonClick); btnTip.onClick.AddListener(onBtnTipClick); btnCloseTip.onClick.AddListener(onBtnCloseTipClick); //读取配置文件 StartCoroutine(JsonLoader.LoadJSON(basePath + "Foods.json", onFoodDataReceived)); //锅初始位置更新 wok.transform.SetParent(txtFoodName.gameObject.transform); //动画对象初始位置更新 cookingAnimation.transform.SetParent(txtFoodName.gameObject.transform); } void Update() { //首页美食列表滑动限位 if (mainfoodlist.transform.localPosition.x>0) { mainfoodlist.transform.localPosition = new Vector2(0, 0); } else if (mainfoodlist.transform.localPosition.x<(- (mainfoodlist.transform.childCount/3)*254)) { mainfoodlist.transform.localPosition = new Vector2( - (mainfoodlist.transform.childCount/3) * 254,0); } //食材列表滑动限位 if (foodlistmat.transform.localPosition.y < 0) { foodlistmat.transform.localPosition = new Vector2(0, 0); } else if (foodlistmat.transform.localPosition.y > foodlistmat.transform.childCount*80) { foodlistmat.transform.localPosition = new Vector2(0, foodlistmat.transform.childCount * 80); } //烹饪方式列表滑动限位 if (foodlistop.transform.localPosition.y < 0) { foodlistop.transform.localPosition = new Vector2(0, 0); } else if (foodlistop.transform.localPosition.y > foodlistop.transform.childCount*80) { foodlistop.transform.localPosition = new Vector2(0,foodlistop.transform.childCount * 80); } } /// /// 首页美食加载 /// /// 美食数据 private void onFoodDataReceived(string data) { foodDataList = JsonTools.ListFromJson(data); foodsContainer.DetachChildren(); //首页美食列表数据生成 foreach (var fData in foodDataList) { GameObject foodItem = Instantiate(preFood, foodsContainer); Food food = foodItem.GetComponent(); food.Data = fData; food.button.onClick.AddListener(onFoodSelected); } //ShowFoods(); } int index; public void ShowFoods() { if (foodDataList != null && foodDataList.Count > 0) { GameObject foodItem = Instantiate(preFood, foodsContainer); Food food = foodItem.GetComponent(); food.Data = foodDataList[index]; food.button.onClick.AddListener(onFoodSelected); // 背景颜色 Tween myTween = foodItem.GetComponent().DOColor(new Color(255, 255, 255, 255), 0.5f); myTween.SetEase(Ease.OutQuint); // 缩放到1倍 Tween myTween2 = foodItem.transform.DOScale(Vector3.one, 0.5f); myTween2.SetEase(Ease.OutQuint); index++; if (index < foodDataList.Count) { Invoke("ShowFoods", 1f); } } } /// /// 选中美食加载详情 /// public void onFoodSelected() { GameObject gameObject = EventSystem.current.currentSelectedGameObject; currentSelected = int.Parse(gameObject.GetComponent().Data.id); Food food = gameObject.GetComponent(); if (selectedFood != null) { selectedFood.objBgImage.SetActive(false); } food.objBgImage.SetActive(true); foodImage.sprite = food.foodImage.sprite; foodNameText.text = food.nameText.text; txtFoodName.text = food.nameText.text; txtFoodName2.text = food.nameText.text; descriptionText.text = food.Data.description; descriptionText.rectTransform.anchoredPosition = new Vector2(0f, -699f); matContainer.GetComponent().anchoredPosition = Vector2.zero; opContainer.GetComponent().anchoredPosition = Vector2.zero; selectedFood = food; animator.SetTrigger("ShowFoodInfo1"); PlayAudioByName("页面切换"); } private void onBackButtonClick() { step = 0; animator.SetTrigger("ShowFoodList"); PlayAudioByName("页面切换"); } List allMatSteps; List allOpSteps; List selectedSteps; List objSteps; int currentStepIndex; int currentObjIndex; GameObject currentStepObj; //标题菜谱名称 public Text txtFoodName; //成功页面菜谱名称 public Text txtFoodName2; //成功页面菜品图片显示 public RawImage imgFood; //选择背景框 public GameObject objSelectedMatBg; public GameObject objSelectedOpBg; #region 操作提示 /// /// 食材提示 /// public GameObject objTipMat; /// /// 烹饪方式提示 /// public GameObject objTipOp; GameObject tip; bool isBegin = true; //提示框显示 void ShowTip(GameObject objTip, float showTime = 1.5f) { IsShowTip = true; tip = objTip; objTip.SetActive(true); Invoke("HideTip", showTime); } //提示框隐藏 void HideTip() { IsShowTip = false; tip.SetActive(false); if (isBegin) { ShowTip(objTipOp, 3f); isBegin = false; } } #endregion //游戏开始 private void onStartButtonClick() { //加载新菜品时进行初始化 if (step==0) { animator.SetTrigger("ShowGame"); PlayAudioByName("页面切换"); animationloadmark = 0; luroufanTransform.SetActive(false); kezaijianTransform.SetActive(false); danzaimianTransform.SetActive(false); zongziTransform.SetActive(false); chaomifenTransform.SetActive(false); dingbiancuoTransform.SetActive(false); doufuTransform.SetActive(false); zhutiTransform.SetActive(false); shaoxiancaoTransform.SetActive(false); } //初始化时清除子对象及数据 foreach (Transform child in matContainer) { Destroy(child.gameObject); } //选择背景框初始化 objSelectedMatBg.transform.SetParent(matContainer.parent); objSelectedMatBg.SetActive(false); //初始化时清除子对象及数据 foreach (Transform child in opContainer) { Destroy(child.gameObject); } //选择背景框初始化 objSelectedOpBg.transform.SetParent(opContainer.parent); objSelectedOpBg.SetActive(false); foreach (Transform child in tranStepCenter) { Destroy(child.gameObject); } allMatSteps = null; allOpSteps = null; selectedSteps = null; objSteps = null; Matitem = 0; MatTrue = 0; string[] Matlist = new string[10]; Opitem = 0; OpTrue = 0; string[] Oplist = new string[10]; tranStepCenter.gameObject.SetActive(false); // 第一次初始化食材及烹饪方式 if (allMatSteps == null || allOpSteps == null) { allMatSteps = new List(); allOpSteps = new List(); foreach (var fData in foodDataList) { foreach (var step in fData.steps) { //加载烹饪步骤 if (step.type == "1" && !allMatSteps.Any(tmpStep => tmpStep.name.Equals(step.name))) { allMatSteps.Add(step); } if (step.type == "2" && !allOpSteps.Any(tmpStep => tmpStep.name.Equals(step.name))) { allOpSteps.Add(step); } } } selectedSteps = selectedFood.Data.steps; objSteps = new List(); } //步骤框生成 //tranStepCenter.DetachChildren(); var step12 = selectedFood.Data.steps.Where(step => step.type == "1" || step.type == "2").ToList(); for (int i = 0; i < step12.Count; i++) { float angle = i * Mathf.PI * 2f / step12.Count; Vector3 pos = new Vector3(Mathf.Cos(angle) * radius, Mathf.Sin(angle) * radius, 0); GameObject obj = Instantiate(preSelected, pos, Quaternion.identity); obj.transform.SetParent(tranStepCenter, false); //obj.transform.GetChild(0).GetComponent().color = // = step12[i].type == "1" ? Color.green : Color.yellow; FoodStepInitial(obj, step12[i], false, i + 1); objSteps.Add(obj); } //食材预制体生成 //matContainer.DetachChildren(); foreach (var item in allMatSteps) { for (int i = 0; i < step12.Count; i++) { if (item.name==step12[i].name) { Matlist[MatTrue] = step12[i].name; MatTrue++; GameObject obj = Instantiate(preFoodMat, matContainer); FoodStepInitial(obj, item); } } Matitem++; } var matfalse = 0; foreach (var item in allMatSteps) { var matdifferent = 0; //随机生成干扰项 for (int i = 0; i < MatTrue; i++) { if (item.name != Matlist[i]) { matdifferent++; } } if (Random.Range(1, 100) > 70 && matfalse < 3&&matdifferent==MatTrue) { matfalse++; matdifferent = 0; GameObject obj = Instantiate(preFoodMat, matContainer); FoodStepInitial(obj, item); } MatTest++; } TransRandom(matContainer); //烹饪方式预制体生成 //opContainer.DetachChildren(); foreach (var item in allOpSteps) { for (int i = 0; i < step12.Count; i++) { if (item.name == step12[i].name) { Oplist[OpTrue] = step12[i].name; OpTrue++; GameObject obj = Instantiate(preOp, opContainer); FoodStepInitial(obj, item); } } Opitem++; } var opfalse = 0; foreach (var item in allOpSteps) { var opdifferent = 0; //随机生成干扰项 for (int i = 0; i < OpTrue; i++) { if (item.name != Oplist[i]) { opdifferent++; } } if (Random.Range(1, 100) > 60 && opfalse < 2 && opdifferent == OpTrue) { opfalse++; opdifferent = 0; GameObject obj = Instantiate(preOp, opContainer); FoodStepInitial(obj, item); } OpTest++; } TransRandom(opContainer); currentObjIndex = 0; currentStepIndex = 0; currentStepObj = objSteps[currentObjIndex]; //设定图片参数 Sprite sp = selectedFood.foodImage.sprite; Texture2D tex = new Texture2D((int)sp.rect.width, (int)sp.rect.height, TextureFormat.RGBA32, false); tex.filterMode = FilterMode.Point; tex.wrapMode = TextureWrapMode.Clamp; tex.SetPixels(sp.texture.GetPixels( (int)sp.textureRect.x, (int)sp.textureRect.y, (int)sp.textureRect.width, (int)sp.textureRect.height)); tex.Apply(); imgFood.texture = tex; Invoke("phantom", 0.001f); //第一步提示自动显示 //ShowTip(objTipMat, 3f); promptMat.SetActive(true); promptMat.GetComponentInChildren().text = "请加入:" + selectedSteps[currentStepIndex].name; Invoke("SetactivePromptMat", 2f); //加载提示菜谱 LoadTip(); } //加载提示菜谱 void LoadTip() { //提示文本注入 txtFoodNameTip.text = selectedFood.nameText.text; txtTip.text = selectedFood.Data.tip; } void HideAll() { objTipMat.SetActive(false); objTipOp.SetActive(false); } void FoodStepInitial(GameObject obj, FoodStepData item, bool canClick = true, int index = 0) { obj.name = item.name; Text txt = obj.GetComponentInChildren(); if (canClick) { //加载食材及烹饪方式图片 if (item.type == "1") { obj.GetComponent