Unity之黑暗之光按教程学习第十天

        今天完成技能模块,教程也到了70多集,快结束了,技能模块用到的较为新的方法是一些关于UI的,比如UI滑块之类的,代码于前几个模块较为相似。在做物品模块与技能模块时,发现Txt文件非常重要,只要文档中有错误,即使代码码对了,还是会出现很多奇怪的问题。

public class SkillItem : MonoBehaviour {

		public int id;
		private SkillInfo info;

		private UISprite iconname_sprite;
		private UILabel name_lable;
		private UILabel applytype_lable;
		private UILabel des_lable;
		private UILabel mp_lable;
		private GameObject icon_mask;

	

		void InitProperty ()
		{
				iconname_sprite = transform.Find ("icon_name").GetComponent<UISprite> ();
				name_lable = transform.Find ("property/name_bg/name").GetComponent<UILabel> ();
				applytype_lable = transform.Find ("property/applytype_bg/applytype").GetComponent<UILabel> ();
				des_lable = transform.Find ("property/des_bg/des").GetComponent<UILabel> ();
				mp_lable = transform.Find ("property/mp_bg/mp").GetComponent<UILabel> ();
				icon_mask = transform.Find ("icon_mask").gameObject;
				icon_mask.SetActive (false);
		}


		public void UpdateShow (int level)
		{
				if (info.level <= level) {//技能可用
						icon_mask.SetActive (false);
						iconname_sprite.GetComponent<SkillItemIcon> ().enabled = true;
				} else {
						icon_mask.SetActive (true);
						iconname_sprite.GetComponent<SkillItemIcon> ().enabled = false;
				}
		}


		//通过调用此方法更新技能显示
		public void SetId(int id)
		{
				InitProperty ();
				this.id = id;
				info = SkillsInfo._instance.GetSkillInfoById (id);
				iconname_sprite.spriteName = info.icon_name;
				name_lable.text = info.name;
				switch (info.applyType) {
				case ApplyType.Passive:
						applytype_lable.text = "增益";
						break;
				case ApplyType.Buff:
						applytype_lable.text = "增强";
						break;
				case ApplyType.SingleTarget:
						applytype_lable.text = "单个目标";
						break;
				case ApplyType.MultiTarget:
						applytype_lable.text = "群体技能";
						break;
				}
				des_lable.text = info.des;
				mp_lable.text = info.mp + "MP";


		}
}
public class SkillItemIcon : UIDragDropItem {

		private int skillid;

		protected override void OnDragDropStart ()
		{//在克隆的Icon上应用的
				base.OnDragDropStart ();

				skillid = transform.parent.GetComponent<SkillItem> ().id;

				transform.parent = transform.root;
				this.GetComponent<UISprite> ().depth = 40;


		}
		protected override void OnDragDropRelease (GameObject surface)
		{
				base.OnDragDropRelease (surface);
				if(surface !=null && surface.tag == Tags.shortcut) {//当一个技能拖到了快捷方式的时候
						surface.GetComponent<ShortCutGrid> ().SetSkill (skillid);
				}
		}
}
public class SkillsInfo : MonoBehaviour {

		public TextAsset skillsInfoText;
		public static SkillsInfo _instance;

		private Dictionary<int, SkillInfo> SkillInfoDict = new Dictionary<int, SkillInfo>();

		private void Awake ()
		{
				_instance = this;
				InitSkillInfoDict ();//初始化技能字典的信息
		}

		//我们可以通过此方法通过ID查找
		public SkillInfo GetSkillInfoById(int id)
		{
				SkillInfo info = null;
				SkillInfoDict.TryGetValue (id, out info);
				return info;
		}

		void InitSkillInfoDict ()
		{
				string text = skillsInfoText.text;
				string [] skillInfoArray = text.Split ('\n');
				foreach (string skillInfoStr in skillInfoArray) {
						string [] pa = skillInfoStr.Split (',');
						SkillInfo info = new SkillInfo ();
						info.id = int.Parse (pa [0]);
						info.name = pa[1];
						info.icon_name = pa [2];
						info.des = pa [3];
						string str_applytype = pa [4];
						switch (str_applytype) {
						case "Passive":
								info.applyType = ApplyType.Passive;
								break;
						case "Buff":
								info.applyType = ApplyType.Buff;
								break;
						case "SingleTarget":
								info.applyType = ApplyType.SingleTarget;
								break;
						case "MultiTarget":
								info.applyType = ApplyType.MultiTarget;
								break;
						}
						string str_pro = pa [5];
						switch (str_pro) {
						case "Attack":
								info.applyProperty = ApplyProperty.Attack;
								break;
						case "Def":
								info.applyProperty = ApplyProperty.Def;
								break;
						case "AttackSpeed":
								info.applyProperty = ApplyProperty.AttackSpeed;
								break;
						case "Speed":
								info.applyProperty = ApplyProperty.Speed;
								break;
						case "HP":
								info.applyProperty = ApplyProperty.HP;
								break;
						case "MP":
								info.applyProperty = ApplyProperty.MP;
								break;
						}
						info.applyValue = int.Parse (pa [6]);
						info.appltTime = int.Parse (pa [7]);
						info.mp = int.Parse (pa [8]);
						info.coldTime = int.Parse (pa [9]);

						switch (pa [10]) {
						case "Swordman":
								info.applicableRole = ApplicableRole.Swordman;
								break;
						case "Magician":
								info.applicableRole = ApplicableRole.Magician;
								break;
						}
						info.level = int.Parse (pa [11]);
						switch (pa [12]) {
						case "Enemy":
								info.releaseType = ReleaseType.Enemy;
								break;
						case "Self":
								info.releaseType = ReleaseType.Self;
								break;
						case "Position":
								info.releaseType = ReleaseType.Position;
								break;
						}
						info.distance = float.Parse (pa [13]);
						SkillInfoDict.Add (info.id, info);
				}
		}
}


//适用角色
public enum ApplicableRole
{
		Swordman,
		Magician
}
//作用类型
public enum ApplyType
{
		Passive,
		Buff,
		SingleTarget,
		MultiTarget
}
 //作用属性
 public enum ApplyProperty
{
		Attack,
		Def,
		AttackSpeed,
		Speed,
		HP,
		MP
}
//释放类型
public enum ReleaseType
{
		Enemy,
		Self,
		Position
}
//技能信息
public class SkillInfo{
		public int id;
		public string name;
		public string icon_name;
		public string des;
		public ApplyType applyType;
		public ApplyProperty applyProperty;
		public int applyValue;
		public int appltTime;
		public int mp;
		public int coldTime;
		public ApplicableRole applicableRole;
		public int level;
		public ReleaseType releaseType;
		public float distance;


}
public class SkillUI : MonoBehaviour {


		public static SkillUI _instance;
		private TweenPosition tween;
		private PlayerStatus playerStatus;

		public UIGrid grid;
		public GameObject skillItemPrefab;

		private bool isShow = false;
		public int [] magicianSkillIdList;
		public int [] swordmanSkillIdList;


		void Awake ()
		{
				_instance = this;
				tween = this.GetComponent<TweenPosition> ();

		}

		[System.Obsolete]
		private void Start ()
		{
				playerStatus = GameObject.FindGameObjectWithTag (Tags.player).GetComponent<PlayerStatus> ();
				int [] idList = null;
				switch (playerStatus.heroType) {
				case HeroType.Magician:
						idList = magicianSkillIdList;
						break;
				case HeroType.Swordman:
						idList = swordmanSkillIdList;
						break;
				}

				foreach(int id in idList){
					GameObject itemGo = NGUITools.AddChild (grid.gameObject, skillItemPrefab);
						
						grid.AddChild (itemGo.transform);//过时了
						itemGo.GetComponent<SkillItem> ().SetId (id);

				}
				
		}

		public void TransformState ()
		{
				if(isShow) {
						tween.PlayForward ();isShow = false;
					

				} else {
						
						tween.PlayReverse ();isShow = true;
						UpdateShow ();

				}
		}

		void UpdateShow ()
		{
				SkillItem [] items = this.GetComponentsInChildren<SkillItem> ();
				foreach (SkillItem item in items) {
						item.UpdateShow (playerStatus.level);
				}
		}
}

            主要只是完成了技能栏,与技能快捷栏的设置,技能与等级的匹配。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李曼竹

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值