using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Dish : MonoBehaviour {//解析菜品图片事件public ParseImgEventHandle parseImgEvent;//当前菜品信息对象public MenuData currentDishData;/// <summary>/// 将图片显示到Dish中的DishImg/// </summary>/// <returns>The dish image.</returns>/// <param name="img">Image.</param>public void ShowDishImg(Texture img){transform.GetChild (0).GetComponent<RawImage> ().texture = img;}
}using UnityEngine;
using System.Collections;
using LitJson;public delegate void ParseDataEventHandle(MenuData[] menuData);
public delegate void ParseImgEventHandle(Texture img);
public class GameManager : MonoBehaviour {public static GameManager instance;void Awake(){instance = this;}public string appID;//API用户序列号public string apiUrL = "http://apis.juhe.cn/cook/query";//API地址public MenuData[] menuData;//菜谱数据public event ParseDataEventHandle TransforDataEvent;//传输数据事件//当前选中的菜品public MenuData currentClickDish;/// <summary>/// 根据菜名返回查询内容的地址URL/// </summary>/// <returns></returns>public string AssemblyRequestURL(string menuName){string resultRUL = apiUrL;//添加API地址resultRUL += "?key=";//添加Key标识符resultRUL += appID;//添加KeyresultRUL += "&menu=";//添加Menu标识符resultRUL += menuName+"&pn=1";//添加菜名Debug.Log(resultRUL);return resultRUL;//返回}/// <summary>/// 获取当前菜品的所有数据/// </summary>/// <param name="requestRUL"></param>/// <returns></returns>public IEnumerator GetMenuData(string requestURL) { WWW www = new WWW(requestURL);//请求数据yield return www;//等待下载string json = www.text;//获取文本Debug.Log(json);if (json.Contains("Success")) {MenuFramework menu =JsonMapper.ToObject<MenuFramework>(json);//获取数据menuData = menu.result.data;//获取到所有菜名数组TransforDataEvent(menuData);//将菜品数组传输到外界}else {Debug.Log ("输入错误,请重新输入");}}}using UnityEngine;
using System.Collections;public class MenuFramework : MonoBehaviour {public string resultcode;public string reason;public MenuResult result;    public int error_code;}
public class MenuResult{public MenuData[] data;public string totalNum;public string pn;public int rn;
}
public class MenuData {public string id;public string title;//菜名标题public string tags;//菜名标签public string imtro;//菜名介绍public string ingredients;//菜名主材料public string burden;//菜名配料public string[] albums;//菜品图片public MenuStep[] steps;//菜品制作步骤
}
public class MenuStep
{public string img;//当前步骤图片public string step;//当前步骤描述
}using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class SearchDish : MonoBehaviour {public GameObject dishPrefab;/// <summary>/// 在搜索输入框输入完毕/// </summary>public void OnSearchInputEditEnd(string menuStr){//清空子对象for (int i = 0; i < transform.childCount; i++) {Destroy (transform.GetChild (i).gameObject);}//拼凑请求地址string url=GameManager.instance.AssemblyRequestURL(menuStr);//绑定数据获取到之后的事件GameManager.instance.TransforDataEvent += InitDish;//启动请求数据协程StartCoroutine(GameManager.instance.GetMenuData(url));}/// <summary>/// 生成菜品列表/// </summary>/// <param name="data"></param>public void InitDish(MenuData[] data){ //遍历所有菜品for (int i = 0; i <data.Length; i++){//生成当前菜品GameObject  currentDish=(GameObject)Instantiate(dishPrefab,Vector3.zero,Quaternion.identity);//设置菜品名称currentDish.transform.GetChild (1).GetComponent<Text> ().text = data [i].title;//设置菜品标签currentDish.transform.GetChild(2).GetComponent<Text>().text=data[i].tags;//当前菜名脚本Dish currentDS=currentDish.GetComponent<Dish>();//绑定显示菜品图片事件currentDS.parseImgEvent += currentDS.ShowDishImg;//开启下载协程,传参下载地址,以及委托对象StartCoroutine (DownloadDishImg (data [i].albums [0], currentDS.parseImgEvent));//设置父物体currentDish.transform.SetParent (transform);//存储每道菜的信息currentDS.currentDishData = data [i];}}IEnumerator DownloadDishImg(string imgURL,ParseImgEventHandle e){WWW www = new WWW (imgURL);//遍历图片下载yield return www;//等待下载
//      //获取图片 (将纹理类型的图片转换成精灵图片)
//      Sprite currentSpr=Sprite.Create(www.texture,new Rect
//          (0,0,www.texture.width,www.texture.height),new Vector2 (0.5f,0.5f));Texture currentTex=www.texture;e (currentTex);//执行委托事件}
}using UnityEngine;
using System.Collections;
using UnityEngine.UI;public class ShowDetail : MonoBehaviour {public GameObject stepPrefab;//预设体/// <summary>/// 激活时显示信息/// </summary>void OnEnable(){ShowBaseImg ();ShowStep();}public void ShowBaseImg(){transform.GetChild(0).GetChild(0).GetChild(1).GetComponent<Text>().text=GameManager.instance.currentClickDish.title;transform.GetChild(0).GetChild(0).GetChild(2).GetComponent<Text>().text=GameManager.instance.currentClickDish.tags;StartCoroutine (Show (GameManager.instance.currentClickDish.albums[0]));//开启协程}IEnumerator Show(string imgURL){WWW www = new WWW (imgURL);  //传入单例中的图片链接yield return www;      //等待下载transform.GetChild (0).GetChild (0).GetChild (0).GetComponent<RawImage> ().texture=www.texture;  }public void ShowStep(){transform.GetChild(1).GetChild(0).GetComponent<Text>().text=GameManager.instance.currentClickDish.imtro;transform.GetChild(1).GetChild(2).GetComponent<Text>().text=GameManager.instance.currentClickDish.ingredients;transform.GetChild(1).GetChild(3).GetComponent<Text>().text=GameManager.instance.currentClickDish.burden; Transform steps = transform.Find ("DishDetail/Steps");for (int i = 0; i < steps.childCount; i++) {Destroy (steps.GetChild (i).gameObject);}for (int i = 0; i < GameManager.instance.currentClickDish.steps.Length; i++) {GameObject currentStep = Instantiate (stepPrefab,Vector3.zero,Quaternion.identity) as GameObject;currentStep.transform.SetParent (steps);currentStep.transform.GetChild (1).GetComponent<Text> ().text = GameManager.instance.currentClickDish.steps [i].step;string url = GameManager.instance.currentClickDish.steps [i].img;StartCoroutine (ShowStepImg (url, currentStep.transform.GetChild (0).GetComponent<RawImage> ()));}}IEnumerator ShowStepImg(string url,RawImage img){WWW www = new WWW (url);yield return www;img.texture= www.texture;}void Update(){if (Input.GetKeyDown(KeyCode.Escape)) {Transform background=GameObject.FindWithTag("Bg").transform;background.GetChild (1).gameObject.SetActive (true);background.GetChild (2).gameObject.SetActive (false);     }}}using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ShowDishDetail : MonoBehaviour {public Transform background;void Start(){background = GameObject.FindWithTag ("Bg").transform;}public void OnDishButtonClick(){//将当前菜品保存到单例中GameManager.instance.currentClickDish = GetComponent<Dish> ().currentDishData;//显示详情页面background.Find ("MenuDetalContentScroll View").gameObject.SetActive (true);//隐藏当前页面background.GetChild (1).gameObject.SetActive (false);}
}

聚合数据(菜谱大全)相关推荐

  1. TP5后端,VUE前端请求聚合数据成语大全

    PS: 聚合接口上描述的是成语大全,其实只是以用户查找字为开头的成语而已.先上演示效果: 1: VUE 前端代码 <template><div class="content ...

  2. ios 聚合数据接口调用demo :菜谱大全

    https://www.juhe.cn/  注册 申请 菜谱大全 我的数据:  菜谱大全 左上角会看到AppKey 接口地址:http://apis.juhe.cn/cook/query.php 支持 ...

  3. TP5后端,VUE前端请求京东万象菜谱大全

    写这个代码的收获: 1: http 请求 https 有一个证书验证(我这里给它关了,接口的数据就进来了) 2: 对后端的一些参数过滤(给默认值, 或者直接拒绝服务) 代码演示效果: 1: 前端VUE ...

  4. TP5后端,VUE前端请求聚合数据新闻接口

    问题描述: TP5当后端,VUE当前端, 请求聚合数据新闻接口 演示效果如下: ps: 最开始加载页面的时候,只加载本地的文件(因为请求次数有限制) 问题解决: 1: vue 文件: <temp ...

  5. TP5后端,VUE前端请求聚合数据驾照题库

    选择效果: 演示效果: 1: Vue 配置: /config/index.js 'use strict' // Template version: 1.3.1 // see http://vuejs- ...

  6. TP5后端,VUE前端请求聚合数据天气接口

    问题描述: TP5 当后端 VUE 当前端 请求聚合数据天气接口 问题解决: 演示效果 前端 VUE 代码: <template><div class="whether-t ...

  7. TP5后端,VUE前端请求聚合数据过去的今天

    先上效果: 1: 前端 vue 文件: <template><!--接口地址 http://v.juhe.cn/todayOnhistory/queryEvent.php参数名 类型 ...

  8. php 菜谱 源码,基于php的菜谱大全api调用代码实例

    代码描述:基于php的菜谱大全api调用代码实例 接口地址:http://www.juhe.cn/docs/api/id/46 PHP代码 // +-------------------------- ...

  9. java用代码实现星期菜谱_基于JAVA的菜谱大全接口调用代码实例

    基于JAVA的菜谱大全接口调用代码实例 代码描述:基于JA V A的菜谱大全接口调用代码实例 接口平台:聚合数据 import java.io.BufferedReader; import java. ...

  10. 免费API接口整理(聚合数据和API Store)

    各类无次数限制的免费API接口整理,主要是聚合数据上和API Store上的一些,还有一些其他的. 聚合数据提供30大类,160种以上基础数据API服务,国内最大的基础数据API服务,下面就罗列一些免 ...

最新文章

  1. 字符串算法--KMP--Java实现
  2. 回首向来萧瑟处,也无风雨也无晴~小祁的2018
  3. html5视频播放器脚本怎么用,HTML5 video标签(播放器)学习笔记(一):使用入门...
  4. java版本号管理_微服务项目中如何管理依赖版本号?
  5. 我TM快疯了,在博客园开博短短2个月,经历博客园数次故障。。。
  6. ubuntu之安装typora
  7. 温度传感器的一些比较
  8. 视频教程-excel提高效率的实用技巧-Office/WPS
  9. rimraf与windows的rmdir简单使用命令方法
  10. 概念模型、数据模型、关系数据模型
  11. Ubuntu支持LinuxONE大型机:为云而生的强强新组合
  12. 数据库SQL入门学习
  13. H5实现一键复制微信并打开微信跳转好友添加页
  14. MP530注墨 连供及传真的心得体会
  15. 浅谈数据清洗的一些要素
  16. 使用 GCM 网络管理工具优化电池使用
  17. bootstrap中col-*-offset、col-*-push和col-*-pull的使用说明
  18. 网络学习day04_子网划分
  19. GPT2自动写作实战
  20. JavaWeb框架-SSH-整合流程!

热门文章

  1. layer.confirm 快速点击会重复触发事件问题
  2. pip添加到环境变量
  3. BorderLayout布局
  4. Magenta - Namespace
  5. MySQL学习笔记(8) 创建用户和赋权,备份与恢复,日志
  6. 路由器安装cloudflared进行内网穿透,安全访问内网
  7. git基础:本地仓库创建和远程连接
  8. 《谁动了我的奶酪》笔记
  9. 基于VR技术的旅游纪念币原型
  10. Golang Winows下编译Linux可执行文件