Unity刀塔传奇视频教程 之 第三天
今天教程共90分钟。
效果图
课程笔记
/* 1.SendMessage: gameObject.SendMessage ("ApplyDamage", 5.0);// 用值为5的值调用ApplyDamage函数 2.NGUI SendMessage: UIButtonMessage 3.摧毁游戏对象: Destroy(object); 4.设置精灵图片: UISprite.spriteName = ""; 5.资源加载: Resources.Load("Game/Mail/PanelMail"); 6.脚本添加组件: panel.AddComponent<TweenScale>(); 7.Ngui 补间动画使用及回调 */
本节最终脚本如下:
ScneneMail.CS
using UnityEngine; using System.Collections; using System; public class SceneMail : MonoBehaviour { private GameObject mItem; private string[] mIconName = { "task_rmb_icon", "task_vit_icon", "task_gold_icon" }; void Start() { mItem = transform.Find("PanelMove/Items/Item").gameObject; BoxCollider[] boxArr = gameObject.GetComponentsInChildren<BoxCollider>(true); foreach(BoxCollider box in boxArr) { UIEventListener listener = UIEventListener.Get(box.gameObject); listener.onClick = ClickBtn; } StartCoroutine(ShowItems(50)); } void ClickBtn(GameObject click) { Debug.Log("Click:"+click.name); if (click.name.Equals("BtnClose")) { Destroy(gameObject);//this.gameObject == gameObject; this.transform == transform; } else if (click.transform.parent.name.Equals("Items")) { UILabel time = click.transform.Find("TimeLabel").GetComponent<UILabel>(); ResLoad(int.Parse(click.name), time.text); } } void ResLoad(int index,string time) { GameObject panel = Instantiate(Resources.Load("Game/Mail/PanelMail")) as GameObject; panel.transform.parent = transform.parent; panel.transform.localEulerAngles = Vector3.zero; panel.transform.localScale = Vector3.zero; panel.transform.localPosition = Vector3.zero; panel.name = "PanelMail"; PanelMail mail = panel.GetComponent<PanelMail>(); mail.InitPanel(index, time); TweenScale tween = panel.AddComponent<TweenScale>(); tween.from = Vector3.zero; tween.to = Vector3.one; tween.duration = 0.2f; tween.PlayForward(); } /// <summary> /// 显示滑动列表 /// </summary> IEnumerator ShowItems(int cloneNums) { if (mItem != null) { for (int i = 0; i < cloneNums; i++) { GameObject obj = Instantiate(mItem) as GameObject; obj.transform.parent = mItem.transform.parent; obj.SetActive(true); obj.transform.localScale = Vector3.one; obj.transform.localEulerAngles = Vector3.zero; obj.transform.localPosition = new Vector3(0, 145 - i * 105, 0); InitItem(obj, i); yield return 2; } } } void InitItem(GameObject item,int index) { item.name = index.ToString(); UILabel titleLabel = item.transform.Find("TitleLabel").GetComponent<UILabel>(); UILabel nameLabel = item.transform.Find("NameLabel").GetComponent<UILabel>(); UILabel timeLabel = item.transform.Find("TimeLabel").GetComponent<UILabel>(); UISprite icon = item.transform.Find("IconBack/Sprite").GetComponent<UISprite>(); titleLabel.text = "王麻子的情书:" + index.ToString(); nameLabel.text = "王麻子"; timeLabel.text = System.DateTime.Now.ToString(); int iconIndex = UnityEngine.Random.Range(0, 2); icon.spriteName = mIconName[iconIndex]; UIEventListener listener = UIEventListener.Get(item); listener.onClick = ClickBtn; } }
PanelMail.cs
using UnityEngine; using System.Collections; public class PanelMail : MonoBehaviour { #region 数据定义 private UILabel mTimeLabel; private UILabel mTitleLabel; private UILabel mContentLabel; #endregion public void InitPanel(int index,string time) { mTimeLabel = transform.Find("PanelBack/Label").GetComponent<UILabel>(); mTitleLabel = transform.Find("PanelMove/Title").GetComponent<UILabel>(); mContentLabel = transform.Find("PanelMove/Label").GetComponent<UILabel>(); mTitleLabel.text = "[b]王麻子的情书" + index.ToString(); mTimeLabel.text = time; mContentLabel.text = " 那是肯定就会立刻[b]就阿[/b][FFFF00]萨德联发[-]\n科技阿森纳的快放假啦可是大家还能看见阿斯蒂芬妮拉可是大家还能阿萨德看\n风景阿隆索的看风景阿隆索的看\n风景那是肯定房价拉萨的看风景阿\n萨德离开黑龙江撒旦法软考"; } void Close(GameObject click) { TweenScale tween = gameObject.GetComponent<TweenScale>(); if(tween != null) { tween.PlayReverse(); tween.onFinished.Add(new EventDelegate(PlayEnd)); } } void PlayEnd() { Destroy(gameObject); } }
视频地址
视频地址:http://www.bobsong.net/757.html
里面提到一点,“利用setActive()之后再进行赋值这样不现实”?为什么呢?
@郑佳淋 就是都先拷贝出来。要显示的激活。这样会有大量的游戏对象在游戏内存中