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;//添加Key
resultRUL += "&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);
}
}
聚合数据(菜谱大全)
最新推荐文章于 2024-08-16 12:06:09 发布