S老师 Top-Down RPG Starter Kit 学习

S老师 Top-Down RPG Starter Kit 学习

character creation

 using UnityEngine;
 using System.Collections;

 public class CharacterCreation : MonoBehaviour {

     public GameObject[] characterPrefabs;
     public UIInput nameInput;//用来得到输入的文本
     private GameObject[] characterGameObjects;
     ;
     private int length;//所有可供选择的角色的个数

     // Use this for initialization
     void Start () {
         length = characterPrefabs.Length;
         characterGameObjects = new GameObject[length];
         ; i < length; i++) {
             characterGameObjects[i] = GameObject.Instantiate(characterPrefabs[i], transform.position, transform.rotation) as GameObject;
         }
         UpdateCharacterShow();
     }

     void UpdateCharacterShow() {//更新所有角色的显示
         characterGameObjects[selectedIndex].SetActive(true);
         ; i < length; i++) {
             if (i != selectedIndex) {
                 characterGameObjects[i].SetActive(false);//把为选择的角色设置为隐藏
             }
         }
     }

     public void OnNextButtonClick() {//当我们点击了下一个按钮
         selectedIndex++;
         selectedIndex %= length;
         UpdateCharacterShow();
     }
     public void OnPrevButtonClick() {//当我们点击了上一个按钮
         selectedIndex--;
         ) {
             selectedIndex = length - ;
         }
         UpdateCharacterShow();
     }
     public void OnOkButtonClick() {
         PlayerPrefs.SetInt("SelectedCharacterIndex", selectedIndex);//存储选择的角色
         PlayerPrefs.SetString("name", nameInput.value);//存储输入的名字
         //加载下一个场景
         Application.LoadLevel();
     }

 }

CharacterCreation

custom

 using UnityEngine;
 using System.Collections;

 public class CursorManager : MonoBehaviour {

     public static CursorManager _instance;

     public Texture2D cursor_normal;
     public Texture2D cursor_npc_talk;
     public Texture2D cursor_attack;
     public Texture2D cursor_lockTarget;
     public Texture2D cursor_pick;

     private Vector2 hotspot = Vector2.zero;
     private CursorMode mode = CursorMode.Auto;

     void Start() {
         _instance = this;
     }

     public void SetNormal() {
         Cursor.SetCursor(cursor_normal, hotspot, mode);
     }
     public void SetNpcTalk() {
         Cursor.SetCursor(cursor_npc_talk, hotspot, mode);
     }
     public void SetAttack() {
         Cursor.SetCursor(cursor_attack, hotspot, mode);
     }
     public void SetLockTarget() {
         Cursor.SetCursor(cursor_lockTarget, hotspot, mode);
     }
 }

CursorManager

 using UnityEngine;
 using System.Collections;

 public class GameLoad : MonoBehaviour {

     public GameObject magicianPrefab;
     public GameObject swordmanPrefab;

     void Awake() {
         int selectdIndex = PlayerPrefs.GetInt("SelectedCharacterIndex");
         string name = PlayerPrefs.GetString("name");

         GameObject go = null;
         ) {
             go = GameObject.Instantiate(magicianPrefab) as GameObject;
         } ) {
             go = GameObject.Instantiate(swordmanPrefab) as GameObject;
         }
         go.GetComponent<PlayerStatus>().name = name;
     }
 }

GameLoad

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class ObjectsInfo : MonoBehaviour {

     public static ObjectsInfo _instance;

     private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int, ObjectInfo>();

     public TextAsset objectsInfoListText;

     void Awake() {
         _instance = this;
         ReadInfo();

     }

     public ObjectInfo GetObjectInfoById(int id) {
         ObjectInfo info=null;

         objectInfoDict.TryGetValue(id, out info);

         return info;
     }

     void ReadInfo() {
         string text = objectsInfoListText.text;
         string[] strArray = text.Split('\n');

         foreach (string str in strArray) {
             string[] proArray = str.Split(',');
             ObjectInfo info = new ObjectInfo();

             ]);
             ];
             ];
             ];
             ObjectType type = ObjectType.Drug;
             switch (str_type) {
                 case "Drug":
                     type = ObjectType.Drug;
                     break;
                 case "Equip":
                     type = ObjectType.Equip;
                     break;
                 case "Mat":
                     type = ObjectType.Mat;
                     break;
             }
             info.id = id; info.name = name; info.icon_name = icon_name;
             info.type = type;
             if (type == ObjectType.Drug) {
                 ]);
                 ]);
                 ]);
                 ]);
                 info.hp = hp; info.mp = mp;
                 info.price_buy = price_buy; info.price_sell = price_sell;
             } else if (type == ObjectType.Equip) {
                 info.attack = ]);
                 info.def = ]);
                 info.speed = ]);
                 info.price_sell = ]);
                 info.price_buy = ]);
                 ];
                 switch (str_dresstype) {
                     case "Headgear":
                         info.dressType = DressType.Headgear;
                         break;
                     case "Armor":
                         info.dressType = DressType.Armor;
                         break;
                     case "LeftHand":
                         info.dressType = DressType.LeftHand;
                         break;
                     case "RightHand":
                         info.dressType = DressType.RightHand;
                         break;
                     case "Shoe":
                         info.dressType = DressType.Shoe;
                         break;
                     case "Accessory":
                         info.dressType = DressType.Accessory;
                         break;
                 }
                 ];
                 switch (str_apptype) {
                     case "Swordman":
                         info.applicationType = ApplicationType.Swordman;
                         break;
                     case "Magician":
                         info.applicationType = ApplicationType.Magician;
                         break;
                     case "Common":
                         info.applicationType = ApplicationType.Common;
                         break;
                 }

             }

             objectInfoDict.Add(id, info);//添加到字典中,id为key,可以很方便的根据id查找到这个物品信息
         }
     }

 }

 //id
 //名称
 //icon名称
 //类型(药品drug)
 //加血量值
 //加魔法值
 //出售价
 //购买
 public enum ObjectType {
     Drug,
     Equip,
     Mat
 }
 public enum DressType {
     Headgear,
     Armor,
     RightHand,
     LeftHand,
     Shoe,
     Accessory
 }
 public enum ApplicationType{
     Swordman,//剑士
     Magician,//魔法师
     Common//通用
 }

 public class ObjectInfo {
     public int id;
     public string name;
     public string icon_name;//这个名称是存储在图集中的名称
     public ObjectType type;
     public int hp;
     public int mp;
     public int price_sell;
     public int price_buy;

     public int attack;
     public int def;
     public int speed;
     public DressType dressType;
     public ApplicationType applicationType;
 }

ObjectsInfo

 using UnityEngine;
 using System.Collections;

 public class Status : MonoBehaviour {

     public static Status _instance;
     private TweenPosition tween;
     private bool isShow = false;

     private UILabel attackLabel;
     private UILabel defLabel;
     private UILabel speedLabel;
     private UILabel pointRemainLabel;
     private UILabel summaryLabel;

     private GameObject attackButtonGo;
     private GameObject defButtonGo;
     private GameObject speedButtonGo;

     private PlayerStatus ps;

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

         attackLabel = transform.Find("attack").GetComponent<UILabel>();
         defLabel = transform.Find("def").GetComponent<UILabel>();
         speedLabel = transform.Find("speed").GetComponent<UILabel>();
         pointRemainLabel = transform.Find("point_remain").GetComponent<UILabel>();
         summaryLabel = transform.Find("summary").GetComponent<UILabel>();
         attackButtonGo = transform.Find("attack_plusbutton").gameObject;
         defButtonGo = transform.Find("def_plusbutton").gameObject;
         speedButtonGo = transform.Find("speed_plusbutton").gameObject;

     }
     void Start() {
         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
     }

     public void TransformState() {
         if (isShow == false) {
             UpdateShow();
             tween.PlayForward(); isShow = true;
         } else {
             tween.PlayReverse(); isShow = false;
         }
     }

     void UpdateShow() {// 更新显示 根据ps playerstatus的属性值,去更新显示
         attackLabel.text = ps.attack + " + " + ps.attack_plus;
         defLabel.text = ps.def + " + " + ps.def_plus;
         speedLabel.text = ps.speed + " + " + ps.speed_plus;

         pointRemainLabel.text = ps.point_remain.ToString();

         summaryLabel.text = "伤害:" + (ps.attack + ps.attack_plus)
             + "  " + "防御:" + (ps.def + ps.def_plus)
             + "  " + "速度:" + (ps.speed + ps.speed_plus);

         ) {
             attackButtonGo.SetActive(true);
             defButtonGo.SetActive(true);
             speedButtonGo.SetActive(true);
         } else {
             attackButtonGo.SetActive(false);
             defButtonGo.SetActive(false);
             speedButtonGo.SetActive(false);
         }

     }

     public void OnAttackPlusClick() {
         bool success = ps.GetPoint();
         if (success) {
             ps.attack_plus++;
             UpdateShow();
         }
     }
     public void OnDefPlusClick() {
         bool success = ps.GetPoint();
         if (success) {
             ps.def_plus++;
             UpdateShow();
         }
     }
     public void OnSpeedPlusClick() {
         bool success = ps.GetPoint();
         if (success) {
             ps.speed_plus++;
             UpdateShow();
         }
     }

 }

Status

 using UnityEngine;
 using System.Collections;

 public class Tags : MonoBehaviour {

     public const string ground = "Ground";//const 标明这个是一个共有的不可变的变量
     public const string player = "Player";
     public const string inventory_item = "InventoryItem";
     public const string inventory_item_grid = "InventoryItemGrid";
     public const string shortcut = "ShortCut";
     public const string minimap = "Minimap";
     public const string enemy = "Enemy";
 }

Tags

enemy

 using UnityEngine;
 using System.Collections;

 public enum WolfState{
     Idle,
     Walk,
     Attack,
     Death
 }

 public class WolfBaby : MonoBehaviour {

     public WolfState state = WolfState.Idle;
     ;
     ;
     ;
     public float miss_rate = 0.2f;
     public string aniname_death;

     public string aniname_idle;
     public string aniname_walk;
     public string aniname_now;
     ;
     ;

     private CharacterController cc;
     ;

     private bool is_attacked = false;
     private Color normal;
     public AudioClip miss_sound;

     private GameObject hudtextFollow;
     private GameObject hudtextGo;
     public GameObject hudtextPrefab;

     private HUDText hudtext;
     private UIFollowTarget followTarget;
     public GameObject body;

     public string aniname_normalattack;
     public float time_normalattack;

     public string aniname_crazyattack;
     public float time_crazyattack;
     public float crazyattack_rate;

     public string aniname_attack_now;
     ;//攻击速率 每秒
     ;

     public Transform target;

     ;
     ;

     public WolfSpawn spawn;
     private PlayerStatus ps;

     void Awake() {
         aniname_now = aniname_idle;

         cc = this.GetComponent<CharacterController>();
         normal = body.GetComponent<Renderer>().material.color;
         hudtextFollow = transform.Find("HUDText").gameObject;
     }
     void Start() {
         //hudtextGo = GameObject.Instantiate(hudtextPrefab, Vector3.zero, Quaternion.identity) as GameObject;
         //hudtextGo.transform.parent = HUDTextParent._instance.gameObject.transform;
         hudtextGo= NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab);

         hudtext = hudtextGo.GetComponent<HUDText>();
         followTarget = hudtextGo.GetComponent<UIFollowTarget>();
         followTarget.target = hudtextFollow.transform;
         followTarget.gameCamera = Camera.main;

         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
         //followTarget.uiCamera = UICamera.current.GetComponent<Camera>();
     }

     void Update() {

         if (state == WolfState.Death) {//死亡
             GetComponent<Animation>().CrossFade(aniname_death);
         } else if (state == WolfState.Attack) {//自动攻击状态
             AutoAttack();
         } else {//巡逻
             GetComponent<Animation>().CrossFade(aniname_now);//播放当前动画
             if (aniname_now == aniname_walk) {
                 cc.SimpleMove(transform.forward * speed);
             }

             timer += Time.deltaTime;
             if (timer >= time) {//计时结束  切换状态
                 timer = ;
                 RandomState();
             }

         }

     }

     void RandomState() {
         , );
         ) {
             aniname_now = aniname_idle;
         } else {
             if (aniname_now != aniname_walk) {
                 transform.Rotate(transform.up * Random.Range(, ));//当状态切换的时候,重新生成方向
             }
             aniname_now = aniname_walk;
         }
     }

     public void TakeDamage(int attack) {//受到伤害
         if (state == WolfState.Death) return;
         target = GameObject.FindGameObjectWithTag(Tags.player).transform;
         state = WolfState.Attack;
         float value = Random.Range(0f, 1f);
         if (value < miss_rate) {// Miss效果
             AudioSource.PlayClipAtPoint(miss_sound, transform.position);
             hudtext.Add();
         } else {//打中的效果
             hudtext.Add();
             this.hp -= attack;
             StartCoroutine( ShowBodyRed() );
             ) {
                 state = WolfState.Death;
                 Destroy();
             }
         }
     }

     IEnumerator ShowBodyRed() {
         body.GetComponent<Renderer>().material.color = Color.red;
         yield return new WaitForSeconds(1f);
         body.GetComponent<Renderer>().material.color = normal;
     }

     void AutoAttack() {
         if (target != null) {
             PlayerState playerState = target.GetComponent<PlayerAttack>().state;
             if (playerState == PlayerState.Death) {
                 target = null;
                 state = WolfState.Idle; return;
             }
             float distance = Vector3.Distance(target.position, transform.position);
             if (distance > maxDistance) {//停止自动攻击
                 target = null;
                 state = WolfState.Idle;
             } else if (distance <= minDistance) {//自动攻击
                 attack_timer+=Time.deltaTime;
                 GetComponent<Animation>().CrossFade(aniname_attack_now);
                 if (aniname_attack_now == aniname_normalattack) {
                     if (attack_timer > time_normalattack) {
                         //产生伤害
                         target.GetComponent<PlayerAttack>().TakeDamage(attack);
                         aniname_attack_now = aniname_idle;
                     }
                 } else if (aniname_attack_now == aniname_crazyattack) {
                     if (attack_timer > time_crazyattack) {
                         //产生伤害
                         target.GetComponent<PlayerAttack>().TakeDamage(attack);
                         aniname_attack_now = aniname_idle;
                     }
                 }
                 if (attack_timer > (1f / attack_rate)) {
                     RandomAttack();
                     attack_timer = ;
                 }
             } else {//朝着角色移动
                 transform.LookAt(target);
                 cc.SimpleMove(transform.forward * speed);
                 GetComponent<Animation>().CrossFade(aniname_walk);
             }
         } else {
             state = WolfState.Idle;
         }
     }

     void RandomAttack() {
         float value = Random.Range(0f, 1f);
         if (value < crazyattack_rate) {//进行疯狂攻击
             aniname_attack_now = aniname_crazyattack;
         } else {//进行普通攻击
             aniname_attack_now = aniname_normalattack;
         }
     }

     void OnDestroy() {
         spawn.MinusNumber();
         ps.GetExp(exp);
         BarNPC._instance.OnKillWolf();
         GameObject.Destroy(hudtextGo);
     }

     void OnMouseEnter() {
         if(PlayerAttack._instance.isLockingTarget==false)
         CursorManager._instance.SetAttack();
     }
     void OnMouseExit() {
         if (PlayerAttack._instance.isLockingTarget == false)
         CursorManager._instance.SetNormal();
     }

 }

WolfBaby

 using UnityEngine;
 using System.Collections;

 public class WolfSpawn : MonoBehaviour {

     ;
     ;
     ;
     ;
     public GameObject prefab;
     void Update() {
         if (currentnum < maxnum) {
             timer += Time.deltaTime;
             if (timer > time) {
                 Vector3 pos = transform.position;
                 pos.x += Random.Range(-, );
                 pos.z += Random.Range(-, );
                 GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject;
                 go.GetComponent<WolfBaby>().spawn = this;
                 timer = ;
                 currentnum++;
             }
         }
     }

     public void MinusNumber() {
         this.currentnum--;
     }

 }

WolfSpawn

inventory

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class Inventory : MonoBehaviour {

     public static Inventory _instance;

     private TweenPosition tween;
     ;//金币数量

     public List<InventoryItemGrid> itemGridList = new List<InventoryItemGrid>();
     public UILabel coinNumberLabel;
     public GameObject inventoryItem;

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

     void Update() {
         if (Input.GetKeyDown(KeyCode.X)) {
             GetId(Random.Range(, ));
         }
     }

     //拾取到id的物品,并添加到物品栏里面
     //处理拾取物品的功能
     ) {
         //第一步是查找在所有的物品中是否存在该物品
             //第二 如果存在,让num +1

         InventoryItemGrid grid = null;
         foreach (InventoryItemGrid temp in itemGridList) {
             if (temp.id == id) {
                 grid = temp; break;
             }
         }
         if (grid != null) {//存在的情况
             grid.PlusNumber(count);
         } else {//不存在的情况
             foreach (InventoryItemGrid temp in itemGridList) {
                 ) {
                     grid = temp; break;
                 }
             }
             if (grid != null) {//第三 不过不存在,查找空的方格,然后把新创建的Inventoryitem放到这个空的方格里面
                 GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem);
                 itemGo.transform.localPosition = Vector3.zero;
                 itemGo.GetComponent<UISprite>().depth = ;
                 grid.SetId(id,count);
             }
         }
     }

     ) {
         InventoryItemGrid grid = null;
         foreach (InventoryItemGrid temp in itemGridList) {
             if (temp.id == id) {
                 grid = temp; break;
             }
         }
         if (grid == null) {
             return false;
         } else {
             bool isSuccess = grid.MinusNumber(count);
             return isSuccess;
         }
     }

     private bool isShow = false;

     void Show() {
         isShow = true;
         tween.PlayForward();
     }

     void Hide() {
         isShow = false;
         tween.PlayReverse();
     }

     public void TransformState() {// 转变状态
         if (isShow == false) {
             Show();
         } else {
             Hide();
         }
     }

     public void AddCoin(int count) {
         coinCount += count;
         coinNumberLabel.text = coinCount.ToString();//更新金币的显示
     }

     //这个是取款方法,返回true表示取款成功,返回false取款失败
     public bool GetCoin(int count) {
         if (coinCount >= count) {
             coinCount -= count;
             coinNumberLabel.text = coinCount.ToString();//更新金币的显示
             return true;
         }
         return false;
     }

 }

Inventory

 using UnityEngine;
 using System.Collections;

 public class InventoryDes : MonoBehaviour {

     public static InventoryDes _instance;
     private UILabel label;
     ;

     void Awake() {
         _instance = this;
         label = this.GetComponentInChildren<UILabel>();
         this.gameObject.SetActive(false);
     }

     // Update is called once per frame
     void Update () {
         if (this.gameObject.activeInHierarchy == true) {
             timer -= Time.deltaTime;
             ) {
                 this.gameObject.SetActive(false);
             }
         }
     }

     public void Show(int id) {
         this.gameObject.SetActive(true); timer = 0.1f;
         transform.position = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition);
         ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
         string des = "";
         switch (info.type) {
             case ObjectType.Drug:
                 des = GetDrugDes(info);
                 break;
             case ObjectType.Equip:
                 des = GetEquipDes(info);
                 break;
         }
         label.text = des;
     }

     string GetDrugDes(ObjectInfo info) {
         string str = "";
         str += "名称:" + info.name + "\n";
         str += "+HP : " + info.hp + "\n";
         str += "+MP:" + info.mp + "\n";
         str += "出售价:" + info.price_sell + "\n";
         str += "购买价:" + info.price_buy;

         return str;
     }

     string GetEquipDes(ObjectInfo info) {
         string str = "";
         str += "名称:" + info.name + "\n";
         switch (info.dressType) {
             case DressType.Headgear:
                 str += "穿戴类型:头盔\n";
                 break;
             case DressType.Armor:
                 str += "穿戴类型:盔甲\n";
                 break;
             case DressType.LeftHand:
                 str += "穿戴类型:左手\n";
                 break;
             case DressType.RightHand:
                 str += "穿戴类型:右手\n";
                 break;
             case DressType.Shoe:
                 str += "穿戴类型:鞋\n";
                 break;
             case DressType.Accessory:
                 str += "穿戴类型:饰品\n";
                     break;
         }
         switch (info.applicationType) {
             case ApplicationType.Swordman:
                 str += "适用类型:剑士\n";
                 break;
             case ApplicationType.Magician:
                 str += "适用类型:魔法师\n";
                 break;
             case ApplicationType.Common:
                 str += "适用类型:通用\n";
                 break;
         }

         str += "伤害值:" + info.attack + "\n";
         str += "防御值:" + info.def + "\n";
         str += "速度值:" + info.speed + "\n";

         str += "出售价:" + info.price_sell + "\n";
         str += "购买价:" + info.price_buy;

         return str;
     }
 }

InventoryDes

 using UnityEngine;
 using System.Collections;

 public class InventoryItem : UIDragDropItem {

     private UISprite sprite;
     private int id;
     void Awake() {
         sprite = this.GetComponent<UISprite>();
     }

     void Update() {
         if (isHover) {
             //显示提示信息
             InventoryDes._instance.Show(id);

             )) {
                 //出来穿戴功能
                 bool success = EquipmentUI._instance.Dress(id);
                 if (success) {
                     transform.parent.GetComponent<InventoryItemGrid>().MinusNumber();
                 }
             }
         }
     }

     protected override void OnDragDropRelease(GameObject surface) {
         base.OnDragDropRelease(surface);
         if (surface != null) {
             if (surface.tag == Tags.inventory_item_grid) {//当拖放到了一个空的格子里面
                 if (surface == this.transform.parent.gameObject) {//拖放到了自己的格子里面

                 } else {
                     InventoryItemGrid oldParent = this.transform.parent.GetComponent<InventoryItemGrid>();

                     this.transform.parent = surface.transform; ResetPosition();
                     InventoryItemGrid newParent = surface.GetComponent<InventoryItemGrid>();
                     newParent.SetId(oldParent.id, oldParent.num);

                     oldParent.ClearInfo();
                 }

             } else if (surface.tag == Tags.inventory_item) {//当拖放到了一个有物品的格子里面
                 InventoryItemGrid grid1 = this.transform.parent.GetComponent<InventoryItemGrid>();
                 InventoryItemGrid grid2 = surface.transform.parent.GetComponent<InventoryItemGrid>();
                 int id = grid1.id; int num = grid1.num;
                 grid1.SetId(grid2.id, grid2.num);
                 grid2.SetId(id, num);
             } else if (surface.tag == Tags.shortcut) {//拖到的快捷方式里面
                 surface.GetComponent<ShortCutGrid>().SetInventory(id);
             }

         }

         ResetPosition();
     }

     void ResetPosition() {
         transform.localPosition = Vector3.zero;
     }

     public void SetId(int id) {
         ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
         sprite.spriteName = info.icon_name;
     }
     public void SetIconName(int id,string icon_name) {
         sprite.spriteName = icon_name;
         this.id = id;
     }
     private bool isHover = false;
     public void OnHoverOver() {
         isHover = true;
     }
     public void OnHoverOut() {
         isHover = false;
     }
 }

InventoryItem

 using UnityEngine;
 using System.Collections;

 public class InventoryItemGrid : MonoBehaviour {

     ;
     private ObjectInfo info = null;
     ;

     public UILabel numLabel;

     // Use this for initialization
     void Start () {
         numLabel = this.GetComponentInChildren<UILabel>();
     }

     ) {
         this.id = id;
         info = ObjectsInfo._instance.GetObjectInfoById(id);
         InventoryItem item = this.GetComponentInChildren<InventoryItem>();
         item.SetIconName(id,info.icon_name);
         numLabel.enabled = true;
         this.num = num;
         numLabel.text = num.ToString();
     }

     ) {
         this.num += num;
         numLabel.text = this.num.ToString();
     }
     //用来减去数量的,可以用来装备的穿戴,返回值,表示是否减去成功
     ) {
         if (this.num >= num) {
             this.num -= num;
             numLabel.text = this.num.ToString();
             ) {
                 //要清空这个物品格子
                 ClearInfo();//清空存储的信息
                 GameObject.Destroy(this.GetComponentInChildren<InventoryItem>().gameObject);//销毁物品格子
             }
             return true;
         }
         return false;
     }

     //清空 格子存的物品信息
     public void ClearInfo() {
         id = ;
         info = null;
         num = ;
         numLabel.enabled = false;
     }

 }

InventoryItemGrid

npc

 using UnityEngine;
 using System.Collections;

 public class BarNPC : NPC {

     public static BarNPC _instance;
     public TweenPosition questTween;
     public UILabel desLabel;
     public GameObject acceptBtnGo;
     public GameObject okBtnGo;
     public GameObject cancelBtnGo;

     public bool isInTask = false;//表示是否在任务中
     ;//表示任务进度,已经杀死了几只小野狼

     private PlayerStatus status;

     void Awake() {
         _instance = this;
     }
     void Start() {
         status = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
     }

     void OnMouseOver() {//当鼠标位于这个collider之上的时候,会在每一帧调用这个方法
         )) {//当点击了老爷爷
             GetComponent<AudioSource>().Play();
             if (isInTask) {
                 ShowTaskProgress();
             } else {
                 ShowTaskDes();
             }
             ShowQuest();
         }
     }

     void ShowQuest() {
         questTween.gameObject.SetActive(true);
         questTween.PlayForward();
     }
     void HideQuest() {
         questTween.PlayReverse();
     }
     public void OnKillWolf() {
         if (isInTask) {
             killCount++;
         }
     }

     void ShowTaskDes(){
         desLabel.text = "任务:\n杀死了10只狼\n\n奖励:\n1000金币";
         okBtnGo.SetActive(false);
         acceptBtnGo.SetActive(true);
         cancelBtnGo.SetActive(true);
     }
     void ShowTaskProgress(){
         desLabel.text = "任务:\n你已经杀死了" + killCount + "\\10只狼\n\n奖励:\n1000金币";
         okBtnGo.SetActive(true);
         acceptBtnGo.SetActive(false);
         cancelBtnGo.SetActive(false);
     }

     //任务系统 任务对话框上的按钮点击时间的处理
     public void OnCloseButtonClick() {
         HideQuest();
     }

     public void OnAcceptButtonClick() {
         ShowTaskProgress();
         isInTask = true;//表示在任务中
     }
     public void OnOkButtonClick() {
         ){//完成任务
             Inventory._instance.AddCoin();
             killCount = ;
             ShowTaskDes();
         }else{
             //没有完成任务
             HideQuest();
         }
     }
     public void OnCancelButtonClick() {
         HideQuest();
     }
 }

BarNPC

 using UnityEngine;
 using System.Collections;

 public class NPC : MonoBehaviour {

     void OnMouseEnter() {
         CursorManager._instance.SetNpcTalk();
     }
     void OnMouseExit() {
         CursorManager._instance.SetNormal();
     }

 }

NPC

 using UnityEngine;
 using System.Collections;

 public class ShopDrugNPC : NPC {

     public void OnMouseOver() {//当鼠标在这个游戏物体之上的时候,会一直调用这个方法
         )) {//弹出来药品购买列表
             GetComponent<AudioSource>().Play();
             ShopDrug._instance.TransformState();
         }
     }

 }

ShopDrugNPC

 using UnityEngine;
 using System.Collections;

 public class ShopWeaponNPC : NPC {
     public void OnMouseOver() {//当鼠标在这个游戏物体之上的时候,会一直调用这个方法
         )) {//弹出来武器商店
             GetComponent<AudioSource>().Play();
             ShopWeaponUI._instance.TransformState();
         }
     }
 }

ShopWeaponNPC

player

 using UnityEngine;
 using System.Collections;

 public class FollowPlayer : MonoBehaviour {

     private Transform player;
     private Vector3 offsetPosition;//位置偏移
     private bool isRotating = false;

     ;
     ;
     ;

     // Use this for initialization
     void Start () {
         player = GameObject.FindGameObjectWithTag(Tags.player).transform;
         transform.LookAt(player.position);
         offsetPosition = transform.position - player.position;
     }

     // Update is called once per frame
     void Update () {
         transform.position = offsetPosition + player.position;
         //处理视野的旋转
         RotateView();
         //处理视野的拉近和拉远效果
         ScrollView();
     }

     void ScrollView() {
         //print(Input.GetAxis("Mouse ScrollWheel"));//向后 返回负值 (拉近视野) 向前滑动 返回正值(拉远视野)
         distance = offsetPosition.magnitude;
         distance += Input.GetAxis("Mouse ScrollWheel")*scrollSpeed;
         distance = Mathf.Clamp(distance, , );
         offsetPosition = offsetPosition.normalized * distance;
     }

     void RotateView() {
         //Input.GetAxis("Mouse X");//得到鼠标在水平方向的滑动
         //Input.GetAxis("Mouse Y");//得到鼠标在垂直方向的滑动
         )) {
             isRotating = true;
         }
         )) {
             isRotating = false;
         }

         if (isRotating) {
             transform.RotateAround(player.position,player.up, rotateSpeed * Input.GetAxis("Mouse X"));

             Vector3 originalPos = transform.position;
             Quaternion originalRotation = transform.rotation;

             transform.RotateAround(player.position,transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));//影响的属性有两个 一个是position 一个是rotation
             float x = transform.eulerAngles.x;
              || x > ) {//当超出范围之后,我们将属性归位原来的,就是让旋转无效
                 transform.position = originalPos;
                 transform.rotation = originalRotation;
             }

         }

         offsetPosition = transform.position - player.position;
     }
 }

FollowPlayer

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class MagicSphere : MonoBehaviour {

     ;

     private List<WolfBaby> wolfList = new List<WolfBaby>();

     public void OnTriggerEnter(Collider col) {
         if (col.tag == Tags.enemy) {
             WolfBaby baby = col.GetComponent<WolfBaby>();
             int index = wolfList.IndexOf(baby);
             ) {
                 baby.TakeDamage((int) attack);
                 wolfList.Add(baby);
             }
         }
     }

 }

MagicSphere

 using UnityEngine;
 using System.Collections;

 public class PlayerAnimation : MonoBehaviour {

     private PlayerMove move;
     private PlayerAttack attack;

     // Use this for initialization
     void Start () {
         move = this.GetComponent<PlayerMove>();
         attack = this.GetComponent<PlayerAttack>();
     }

     // Update is called once per frame
     void LateUpdate () {
         if (attack.state == PlayerState.ControlWalk) {
             if (move.state == ControlWalkState.Moving) {
                 PlayAnim("Run");
             } else if (move.state == ControlWalkState.Idle) {
                 PlayAnim("Idle");
             }
         } else if (attack.state == PlayerState.NormalAttack) {
             if (attack.attack_state == AttackState.Moving) {
                 PlayAnim("Run");
             }
         }
     }

     void PlayAnim(string animName) {
         GetComponent<Animation>().CrossFade(animName);
     }
 }

PlayerAnimation

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public enum PlayerState {
     ControlWalk,
     NormalAttack,
     SkillAttack,
     Death
 }
 public enum AttackState {//攻击时候的状态
     Moving,
     Idle,
     Attack
 }

 public class PlayerAttack : MonoBehaviour {

     public static PlayerAttack _instance;

     public PlayerState state = PlayerState.ControlWalk;
     public AttackState attack_state = AttackState.Idle;

     public string aniname_normalattack;//普通攻击的动画
     public string aniname_idle;
     public string aniname_now;
     public float time_normalattack;//普通攻击的时间
     ;
     ;
     ;//默认攻击的最小距离
     private Transform target_normalattack;

     private PlayerMove move;
     public GameObject effect;
     private bool showEffect = false;
     private PlayerStatus ps;
     public float miss_rate = 0.25f;
     public GameObject hudtextPrefab;
     private GameObject hudtextFollow;
     private GameObject hudtextGo;
     private HUDText hudtext;
     public AudioClip miss_sound;
     public GameObject body;
     private Color normal;
     public string aniname_death;

     public GameObject[] efxArray;
     private Dictionary<string, GameObject> efxDict = new Dictionary<string, GameObject>();

     public bool isLockingTarget = false;//是否正在选择目标
     private SkillInfo info = null;

     void Awake() {
         _instance = this;
         move = this.GetComponent<PlayerMove>();
         ps = this.GetComponent<PlayerStatus>();
         normal = body.GetComponent<Renderer>().material.color;

         hudtextFollow = transform.Find("HUDText").gameObject;

         foreach (GameObject go in efxArray) {
             efxDict.Add(go.name, go);
         }
     }

     void Start() {

         hudtextGo = NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab);

         hudtext = hudtextGo.GetComponent<HUDText>();
         UIFollowTarget followTarget = hudtextGo.GetComponent<UIFollowTarget>();
         followTarget.target = hudtextFollow.transform;
         followTarget.gameCamera = Camera.main;

     }

     void Update() {

         ) &&  state != PlayerState.Death ) {
             //做射线检测
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hitInfo;
             bool isCollider = Physics.Raycast(ray, out hitInfo);
             if (isCollider && hitInfo.collider.tag == Tags.enemy) {
                 //当我们点击了一个敌人的时候
                 target_normalattack = hitInfo.collider.transform;
                 state = PlayerState.NormalAttack;//进入普通攻击的模式
                 timer = ; showEffect = false;
             } else {
                 state = PlayerState.ControlWalk;
                 target_normalattack = null;
             }
         }

         if (state == PlayerState.NormalAttack) {
             if (target_normalattack == null) {
                 state = PlayerState.ControlWalk;
             } else {
                 float distance = Vector3.Distance(transform.position, target_normalattack.position);
                 if (distance <= min_distance) {//进行攻击
                     transform.LookAt(target_normalattack.position);
                     attack_state = AttackState.Attack;

                     timer += Time.deltaTime;
                     GetComponent<Animation>().CrossFade(aniname_now);
                     if (timer >= time_normalattack) {
                         aniname_now = aniname_idle;
                         if (showEffect == false) {
                             showEffect = true;
                             //播放特效
                             GameObject.Instantiate(effect, target_normalattack.position, Quaternion.identity);
                             target_normalattack.GetComponent<WolfBaby>().TakeDamage(GetAttack());
                         }

                     }
                     if (timer >= (1f / rate_normalattack)) {
                         timer = ; showEffect = false;
                         aniname_now = aniname_normalattack;
                     }

                 } else {//走向敌人
                     attack_state = AttackState.Moving;
                     move.SimpleMove(target_normalattack.position);
                 }
             }
         } else if (state == PlayerState.Death) {//如果死亡就播放死亡动画
             GetComponent<Animation>().CrossFade(aniname_death);
         }

         )) {
             OnLockTarget();
         }
     }

     public int GetAttack() {
         return (int)(EquipmentUI._instance.attack + ps.attack + ps.attack_plus);
     }

     public void TakeDamage(int attack) {
         if (state == PlayerState.Death) return;
         float def = EquipmentUI._instance.def + ps.def + ps.def_plus;
          - def) / );
         ) temp = ;

         float value = Random.Range(0f, 1f);
         if (value < miss_rate) {//MISS
             AudioSource.PlayClipAtPoint(miss_sound, transform.position);
             hudtext.Add();
         } else {
             hudtext.Add();
             ps.hp_remain -= (int)temp;
             StartCoroutine(ShowBodyRed());
             ) {
                 state = PlayerState.Death;
             }
         }
         HeadStatusUI._instance.UpdateShow();
     }

     IEnumerator ShowBodyRed() {
         body.GetComponent<Renderer>().material.color = Color.red;
         yield return new WaitForSeconds(1f);
         body.GetComponent<Renderer>().material.color = normal;
     }

     void OnDestroy() {
         GameObject.Destroy(hudtextGo);

     }

     public void UseSkill(SkillInfo info) {
         if (ps.heroType == HeroType.Magician) {
             if (info.applicableRole == ApplicableRole.Swordman) {
                 return;
             }
         }
         if (ps.heroType == HeroType.Swordman) {
             if (info.applicableRole == ApplicableRole.Magician) {
                 return;
             }
         }
         switch (info.applyType) {
             case ApplyType.Passive:
                 StartCoroutine( OnPassiveSkillUse(info));
                 break;
             case ApplyType.Buff:
                 StartCoroutine(OnBuffSkillUse(info));
                 break;
             case ApplyType.SingleTarget:
                 OnSingleTargetSkillUse(info) ;
                 break;
             case ApplyType.MultiTarget:
                 OnMultiTargetSkillUse(info);
                 break;
         }

     }
     //处理增益技能
     IEnumerator OnPassiveSkillUse(SkillInfo info ) {
         state = PlayerState.SkillAttack;
         GetComponent<Animation>().CrossFade(info.aniname);
         yield return new WaitForSeconds(info.anitime);
         state = PlayerState.ControlWalk;
         , mp = ;
         if (info.applyProperty == ApplyProperty.HP) {
             hp = info.applyValue;
         } else if (info.applyProperty == ApplyProperty.MP) {
             mp = info.applyValue;
         }

         ps.GetDrug(hp,mp);
         //实例化特效
         GameObject prefab = null;
         efxDict.TryGetValue(info.efx_name, out prefab);
         GameObject.Instantiate(prefab, transform.position, Quaternion.identity);
     }
     //处理增强技能
     IEnumerator OnBuffSkillUse(SkillInfo info) {
         state = PlayerState.SkillAttack;
         GetComponent<Animation>().CrossFade(info.aniname);
         yield return new WaitForSeconds(info.anitime);
         state = PlayerState.ControlWalk;

         //实例化特效
         GameObject prefab = null;
         efxDict.TryGetValue(info.efx_name, out prefab);
         GameObject.Instantiate(prefab, transform.position, Quaternion.identity);

         switch (info.applyProperty) {
             case ApplyProperty.Attack:
                 ps.attack *= (info.applyValue / 100f);
                 break;
             case ApplyProperty.AttackSpeed:
                 rate_normalattack *= (info.applyValue / 100f);
                 break;
             case ApplyProperty.Def:
                 ps.def *= (info.applyValue / 100f);
                 break;
             case ApplyProperty.Speed:
                 move.speed *= (info.applyValue / 100f);
                 break;
         }
         yield return new WaitForSeconds(info.applyTime);
         switch (info.applyProperty) {
             case ApplyProperty.Attack:
                 ps.attack /= (info.applyValue / 100f);
                 break;
             case ApplyProperty.AttackSpeed:
                 rate_normalattack /= (info.applyValue / 100f);
                 break;
             case ApplyProperty.Def:
                 ps.def /= (info.applyValue / 100f);
                 break;
             case ApplyProperty.Speed:
                 move.speed /= (info.applyValue / 100f);
                 break;
         }
     }
     //准备选择目标
     void OnSingleTargetSkillUse(SkillInfo info) {
         state = PlayerState.SkillAttack;
         CursorManager._instance.SetLockTarget();
         isLockingTarget = true;
         this.info = info;
     }
     //选择目标完成,开始技能的释放
     void OnLockTarget() {
         isLockingTarget = false;
         switch (info.applyType) {
             case ApplyType.SingleTarget:
                 StartCoroutine( OnLockSingleTarget());
                 break;
             case ApplyType.MultiTarget:
                 StartCoroutine(OnLockMultiTarget());
                 break;
         }
     }

     IEnumerator OnLockSingleTarget() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitInfo;
         bool isCollider = Physics.Raycast(ray, out hitInfo);
         if (isCollider && hitInfo.collider.tag == Tags.enemy) {//选择了一个敌人
             GetComponent<Animation>().CrossFade(info.aniname);
             yield return new WaitForSeconds(info.anitime);
             state = PlayerState.ControlWalk;
             //实例化特效
             GameObject prefab = null;
             efxDict.TryGetValue(info.efx_name, out prefab);
             GameObject.Instantiate(prefab, hitInfo.collider.transform.position, Quaternion.identity);

             hitInfo.collider.GetComponent<WolfBaby>().TakeDamage((int)(GetAttack() * (info.applyValue / 100f)));
         } else {
             state = PlayerState.NormalAttack;
         }
         CursorManager._instance.SetNormal();
     }

     void OnMultiTargetSkillUse(SkillInfo info) {
         state = PlayerState.SkillAttack;
         CursorManager._instance.SetLockTarget();
         isLockingTarget = true;
         this.info = info;
     }
     IEnumerator OnLockMultiTarget() {
         CursorManager._instance.SetNormal();
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hitInfo;
         );
         if (isCollider) {
             GetComponent<Animation>().CrossFade(info.aniname);
             yield return new WaitForSeconds(info.anitime);
             state = PlayerState.ControlWalk;

             //实例化特效
             GameObject prefab = null;
             efxDict.TryGetValue(info.efx_name, out prefab);
             GameObject go = GameObject.Instantiate(prefab, hitInfo.point + Vector3.up * 0.5f, Quaternion.identity) as GameObject;
             go.GetComponent<MagicSphere>().attack = GetAttack() * (info.applyValue / 100f);
         } else {
             state = PlayerState.ControlWalk;
         }

     }
 }

PlayerAttack

 using UnityEngine;
 using System.Collections;

 public class PlayerDir : MonoBehaviour {

     public GameObject effect_click_prefab;
     public Vector3 targetPosition = Vector3.zero;
     private bool isMoving = false;//表示鼠标是否按下
     private PlayerMove playerMove;
     private PlayerAttack attack;

     void Start() {
         targetPosition = transform.position;
         playerMove = this.GetComponent<PlayerMove>();
         attack = this.GetComponent<PlayerAttack>();
     }

     // Update is called once per frame
     void Update () {
         if (attack.state == PlayerState.Death) return;
         ) && UICamera.hoveredObject == null) {
             Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hitInfo;
             bool isCollider = Physics.Raycast(ray, out hitInfo);
             if (isCollider && hitInfo.collider.tag == Tags.ground) {
                 isMoving = true;
                 ShowClickEffect(hitInfo.point);
                 LookAtTarget(hitInfo.point);
             }
         }

         )) {
             isMoving = false;
         }

         if (isMoving) {
             //得到要移动的目标位置
             //让主角朝向目标位置
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             RaycastHit hitInfo;
             bool isCollider = Physics.Raycast(ray, out hitInfo);
             if (isCollider && hitInfo.collider.tag == Tags.ground) {
                 LookAtTarget(hitInfo.point);
             }
         } else {
             if (playerMove.isMoving) {
                 LookAtTarget(targetPosition);
             }
         }
     }

     //实例化出来点击的效果
     void ShowClickEffect( Vector3 hitPoint ) {
         hitPoint = new Vector3( hitPoint.x,hitPoint.y + 0.1f ,hitPoint.z );
         GameObject.Instantiate(effect_click_prefab, hitPoint, Quaternion.identity);
     }
     //让主角朝向目标位置
     void LookAtTarget(Vector3 hitPoint) {
         targetPosition = hitPoint;
         targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z);
         this.transform.LookAt(targetPosition);
     }
 }

PlayerDir

 using UnityEngine;
 using System.Collections;

 public enum ControlWalkState {
     Moving,
     Idle
 }

 public class PlayerMove : MonoBehaviour {

     ;
     public ControlWalkState state = ControlWalkState.Idle;
     private PlayerDir dir;
     private PlayerAttack attack;
     private CharacterController controller;
     public bool isMoving = false;

     void Start() {
         dir = this.GetComponent<PlayerDir>();
         controller = this.GetComponent<CharacterController>();
         attack = this.GetComponent<PlayerAttack>();
     }
     // Update is called once per frame
     void Update () {
         if (attack.state == PlayerState.ControlWalk) {
             float distance = Vector3.Distance(dir.targetPosition, transform.position);
             if (distance > 0.3f) {
                 isMoving = true;
                 state = ControlWalkState.Moving;
                 controller.SimpleMove(transform.forward * speed);
             } else {
                 isMoving = false;
                 state = ControlWalkState.Idle;
             }
         }
     }

     public void SimpleMove(Vector3 targetPos) {
         transform.LookAt(targetPos);
         controller.SimpleMove(transform.forward * speed);
     }
 }

PlayerMove

 using UnityEngine;
 using System.Collections;

 public enum HeroType {
     Swordman,
     Magician
 }

 public class PlayerStatus : MonoBehaviour {

     public HeroType heroType;

     ; // 100+level*30

     public string name = "默认名称";
     ;//hp最大值
     ;//mp最大值
     ;
     ;
     ;//当前已经获得的经验

     ;
     ;
     ;
     ;
     ;
     ;

     ;//剩余的点数

     void Start() {
         GetExp();
     }

     public void GetDrug(int hp,int mp) {//获得治疗
         hp_remain += hp;
         mp_remain += mp;
         if (hp_remain > this.hp) {
             hp_remain = this.hp;
         }
         if (mp_remain > this.mp) {
             mp_remain = this.mp;
         }
         HeadStatusUI._instance.UpdateShow();
     }

     ) {//获得点数
         if (point_remain >= point) {
             point_remain -= point;
             return true;
         }
         return false;
     }

     public void GetExp(int exp) {
         this.exp += exp;
          + level * ;
         while (this.exp >= total_exp) {
             //TODO 升级
             this.level++;
             this.exp -= total_exp;
             total_exp =  + level * ;
         }

         ExpBar._instance.SetValue(this.exp/total_exp );
     }

     public bool TakeMP(int count) {
         if (mp_remain >= count) {
             mp_remain -= count;
             HeadStatusUI._instance.UpdateShow();
             return true;
         } else {
             return false;
         }
     }
 }

PlayerStatus

skill

 using UnityEngine;
 using System.Collections;

 public class SkillItem : MonoBehaviour {

     public int id;
     private SkillInfo info;

     private UISprite iconname_sprite;
     private UILabel name_label;
     private UILabel applytype_label;
     private UILabel des_label;
     private UILabel mp_label;

     private GameObject icon_mask;

     void InitProperty() {
         iconname_sprite = transform.Find("icon_name").GetComponent<UISprite>();
         name_label = transform.Find("property/name_bg/name").GetComponent<UILabel>();
         applytype_label = transform.Find("property/applytype_bg/applytype").GetComponent<UILabel>();
         des_label = transform.Find("property/des_bg/des").GetComponent<UILabel>();
         mp_label = 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_label.text = info.name;
         switch (info.applyType) {
             case ApplyType.Passive:
                 applytype_label.text = "增益";
                 break;
             case ApplyType.Buff:
                 applytype_label.text = "增强";
                 break;
             case ApplyType.SingleTarget:
                 applytype_label.text = "单个目标";
                 break;
             case ApplyType.MultiTarget:
                 applytype_label.text = "群体技能";
                 break;
         }
         des_label.text = info.des;
         mp_label.text = info.mp + "MP";
     }

 }

SkillItem

 using UnityEngine;
 using System.Collections;

 public class SkillItemIcon : UIDragDropItem {

     private int skillId;

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

         skillId = transform.parent.GetComponent<SkillItem>().id;
         transform.parent = transform.root;
         ;
     }

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

SkillItemIcon

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class SkillsInfo : MonoBehaviour {

     public static SkillsInfo _instance;
     public TextAsset skillsInfoText;

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

     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 = ]);
             info.name = pa[];
             info.icon_name = pa[];
             info.des = pa[];
             ];
             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;
             }
             ];
             switch (str_applypro) {
                 case "Attack":
                     info.applyProperty = ApplyProperty.Attack;
                     break;
                 case "Def":
                     info.applyProperty = ApplyProperty.Def;
                     break;
                 case "Speed":
                     info.applyProperty = ApplyProperty.Speed;
                     break;
                 case "AttackSpeed":
                     info.applyProperty = ApplyProperty.AttackSpeed;
                     break;
                 case "HP":
                     info.applyProperty = ApplyProperty.HP;
                     break;
                 case "MP":
                     info.applyProperty = ApplyProperty.MP;
                     break;
             }
             info.applyValue = ]);
             info.applyTime = ]);
             info.mp = ]);
             info.coldTime = ]);
             ]) {
                 case "Swordman":
                     info.applicableRole = ApplicableRole.Swordman;
                     break;
                 case "Magician":
                     info.applicableRole = ApplicableRole.Magician;
                     break;
             }
             info.level = ]);
             ]) {
                 case "Self":
                     info.releaseType = ReleaseType.Self;
                     break;
                 case "Enemy":
                     info.releaseType = ReleaseType.Enemy;
                     break;
                 case "Position":
                     info.releaseType = ReleaseType.Position;
                     break;
             }
             info.distance = ]);
             info.efx_name = pa[];
             info.aniname = pa[];
             info.anitime = ]);
             skillInfoDict.Add(info.id, info);
         }
     }

 }
 //适用角色
 public enum ApplicableRole {
     Swordman,
     Magician
 }
 //作用类型
 public enum ApplyType {
     Passive,
     Buff,
     SingleTarget,
     MultiTarget
 }
 //作用属性
 public enum ApplyProperty {
     Attack,
     Def,
     Speed,
     AttackSpeed,
     HP,
     MP
 }
 //释放类型
 public enum ReleaseType {
     Self,
     Enemy,
     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 applyTime;
     public int mp;
     public int coldTime;
     public ApplicableRole applicableRole;
     public int level;
     public ReleaseType releaseType;
     public float distance;
     public string efx_name;
     public string aniname;
     ;
 }

SkillsInfo

 using UnityEngine;
 using System.Collections;

 public class SkillUI : MonoBehaviour {

     public static SkillUI _instance;
     private TweenPosition tween;
     private bool isShow = false;
     private PlayerStatus ps;

     public UIGrid grid;
     public GameObject skillItemPrefab;
     public int[] magicianSkillIdList;
     public int[] swordmanSkillIdList;

     void Awake() {
         _instance = this;
         tween = this.GetComponent<TweenPosition>();
     }
     void Start() {
         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
         int[] idList = null;
         switch (ps.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.PlayReverse(); isShow = false;
         } else {
             tween.PlayForward(); isShow = true;
             UpdateShow();
         }
     }

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

SkillUI

start

 using UnityEngine;
 using System.Collections;

 public class ButtonContainer : MonoBehaviour {

     //1,游戏数据的保存,和场景之间游戏数据的传递使用 PlayerPrefs
     //2,场景的分类
         //2.1开始场景
         //2.2角色选择界面
         //2.3游戏玩家打怪的界面,就是游戏实际的运行场景 村庄有野兽。。。

     //开始新游戏
     public void OnNewGame() {
         PlayerPrefs.SetInt();
         // 加载我们的选择角色的场景 2
         Application.LoadLevel();
     }
     //加载已经保存的游戏
     public void OnLoadGame() {
         PlayerPrefs.SetInt(); //DataFromSave表示数据来自保存
         //加载我们的play场景3
     }

 }

ButtonContainer

 using UnityEngine;
 using System.Collections;

 public class MovieCamera : MonoBehaviour {

     ;

     ;

     // Use this for initialization
     void Start () {

     }

     // Update is called once per frame
     void Update () {
         if (transform.position.z < endZ) {//还没有达到目标位置,需要移动
             transform.Translate( Vector3.forward*speed*Time.deltaTime);
         }
     }
 }

MovieCamera

 using UnityEngine;
 using System.Collections;

 public class PressAnyKey : MonoBehaviour {

     private bool isAnyKeyDown = false;//表示是否有任何按键按下
     private GameObject buttonContainer;

     void Start() {
         buttonContainer = this.transform.parent.Find("buttonContainer").gameObject;
     }

     // Update is called once per frame
     void Update () {
         if (isAnyKeyDown == false) {
             if (Input.anyKey) {
                 // show button container
                 //hide self
                 ShowButton();
             }
         }
     }

     void ShowButton() {
         buttonContainer.SetActive(true);
         this.gameObject.SetActive(false);
         isAnyKeyDown = true;
     }
 }

PressAnyKey

ui

 using UnityEngine;
 using System.Collections;

 public class EquipmentItem : MonoBehaviour {

     private UISprite sprite;
     public int id;
     private bool isHover = false;

     void Awake() {
         sprite = this.GetComponent<UISprite>();
     }

     void Update() {
         if (isHover) {//当鼠标在这个装备栏之上的时候,检测鼠标右键的点击
             )) {//鼠标右键的点击,表示卸下该装备
                 EquipmentUI._instance.TakeOff(id,this.gameObject);
             }
         }
     }

     public void SetId(int id) {
         this.id = id;
         ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
         SetInfo(info);
     }
     public void SetInfo(ObjectInfo info) {
         this.id = info.id;

         sprite.spriteName = info.icon_name;
     }

     public void OnHover(bool isOver) {
         isHover = isOver;
     }

 }

EquipmentItem

 using UnityEngine;
 using System.Collections;

 public class EquipmentUI : MonoBehaviour {

     public static EquipmentUI _instance;
     private TweenPosition tween;
     private bool isShow = false;

     private GameObject headgear;
     private GameObject armor;
     private GameObject rightHand;
     private GameObject leftHand;
     private GameObject shoe;
     private GameObject accessory;

     private PlayerStatus ps;

     public GameObject equipmentItem;

     ;
     ;
     ;

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

         headgear = transform.Find("Headgear").gameObject;
         armor = transform.Find("Armor").gameObject;
         rightHand = transform.Find("RightHand").gameObject;
         leftHand = transform.Find("LeftHand").gameObject;
         shoe = transform.Find("Shoe").gameObject;
         accessory = transform.Find("Accessory").gameObject;

         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
     }

     public void TransformState() {
         if (isShow == false) {
             tween.PlayForward();
             isShow = true;
         } else {
             tween.PlayReverse();
             isShow = false;
         }
     }

     //处理装备穿戴功能
     public bool Dress(int id) {
         ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id);
         if (info.type != ObjectType.Equip) {
             return false;//穿戴不成功
         }
         if (ps.heroType == HeroType.Magician) {
             if (info.applicationType == ApplicationType.Swordman) {
                 return false;
             }
         }
         if (ps.heroType == HeroType.Swordman) {
             if (info.applicationType == ApplicationType.Magician) {
                 return false;
             }
         }

         GameObject parent = null;
         switch (info.dressType) {
             case DressType.Headgear:
                 parent = headgear;
                 break;
             case DressType.Armor:
                 parent = armor;
                 break;
             case DressType.RightHand:
                 parent = rightHand;
                 break;
             case DressType.LeftHand:
                 parent = leftHand;
                 break;
             case DressType.Shoe:
                 parent = shoe;
                 break;
             case DressType.Accessory:
                 parent = accessory;
                 break;
         }
         EquipmentItem item = parent.GetComponentInChildren<EquipmentItem>();
         if (item != null) {//说明已经穿戴了同样类型的装备
             Inventory._instance.GetId(item.id);//把已经穿戴的装备卸下,放回物品栏
             item.SetInfo(info);
         } else {//没有穿戴同样类型的装备
             GameObject itemGo =  NGUITools.AddChild(parent, equipmentItem);
             itemGo.transform.localPosition = Vector3.zero;
             itemGo.GetComponent<EquipmentItem>().SetInfo(info);
         }
         UpdateProperty();
         return true;
     }
     public void TakeOff(int id,GameObject go) {
         Inventory._instance.GetId(id);
         GameObject.Destroy(go);
         UpdateProperty();
     }

     void UpdateProperty() {
         ;
         ;
         ;

         EquipmentItem headgearItem = headgear.GetComponentInChildren<EquipmentItem>();
         PlusProperty(headgearItem);
         EquipmentItem armorItem = armor.GetComponentInChildren<EquipmentItem>();
         PlusProperty(armorItem);
         EquipmentItem leftHandItem = leftHand.GetComponentInChildren<EquipmentItem>();
         PlusProperty(leftHandItem);
         EquipmentItem rightHandItem = rightHand.GetComponentInChildren<EquipmentItem>();
         PlusProperty(rightHandItem);
         EquipmentItem shoeItem = shoe.GetComponentInChildren<EquipmentItem>();
         PlusProperty(shoeItem);
         EquipmentItem accessoryItem = accessory.GetComponentInChildren<EquipmentItem>();
         PlusProperty(accessoryItem);
     }

     void PlusProperty(EquipmentItem item) {
         if (item != null) {
             ObjectInfo equipInfo = ObjectsInfo._instance.GetObjectInfoById(item.id);
             this.attack += equipInfo.attack;
             this.def += equipInfo.def;
             this.speed += equipInfo.speed;
         }
     }

 }

EquipmentUI

 using UnityEngine;

 using System.Collections;

 public class ExpBar : MonoBehaviour {

     public static ExpBar _instance;
     private UISlider progressBar;
     void Awake() {
         _instance = this;
         progressBar = this.GetComponent<UISlider>();
     }

     public void SetValue(float value) {
         progressBar.value = value;
     }
 }

ExpBar

 using UnityEngine;
 using System.Collections;

 public class FunctionBar : MonoBehaviour {

     public void OnStatusButtonClick() {
         Status._instance.TransformState();
     }
     public void OnBagButtonClick() {
         Inventory._instance.TransformState();
     }
     public void OnEquipButtonClick() {
         EquipmentUI._instance.TransformState();
     }
     public void OnSkillButtonClick() {
         SkillUI._instance.TransformState();
     }
     public void OnSettingButtonClick() {
     }

 }

FunctionBar

 using UnityEngine;
 using System.Collections;

 public class HeadStatusUI : MonoBehaviour {

     public static HeadStatusUI _instance;

     private UILabel name;

     private UISlider hpBar;
     private UISlider mpBar;

     private UILabel hpLabel;
     private UILabel mpLabel;
     private PlayerStatus ps;

     void Awake() {
         _instance = this;
         name = transform.Find("Name").GetComponent<UILabel>();
         hpBar = transform.Find("HP").GetComponent<UISlider>();
         mpBar = transform.Find("MP").GetComponent<UISlider>();

         hpLabel = transform.Find("HP/Thumb/Label").GetComponent<UILabel>();
         mpLabel = transform.Find("MP/Thumb/Label").GetComponent<UILabel>();
     }

     void Start() {
         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
         UpdateShow();
     }

     public void UpdateShow() {
         name.text = "Lv." + ps.level + " " + ps.name;
         hpBar.value = ps.hp_remain / ps.hp;
         mpBar.value = ps.mp_remain / ps.mp;

         hpLabel.text = ps.hp_remain + "/" + ps.hp;
         mpLabel.text = ps.mp_remain + "/" + ps.mp;
     }

 }

HeadStatusUI

 using UnityEngine;
 using System.Collections;

 public class HUDTextParent : MonoBehaviour {

     public static HUDTextParent _instance;

     void Awake() {
         _instance = this;
     }
 }

HUDTextParent

 using UnityEngine;
 using System.Collections;

 public class Minimap : MonoBehaviour {

     private Camera minimapCamera;

     void Start() {
         minimapCamera = GameObject.FindGameObjectWithTag(Tags.minimap).GetComponent<Camera>();
     }

     public void OnZoomInClick() {
         //放大
         minimapCamera.orthographicSize--;
     }
     public void OnZoomOutClick() {
         //缩小
         minimapCamera.orthographicSize++;
     }
 }

Minimap

 using UnityEngine;
 using System.Collections;

 public class ShopDrug : MonoBehaviour {

     public static ShopDrug _instance;
     private TweenPosition tween;
     private bool isShow = false;

     private GameObject numberDialog;
     private UIInput numberInput;
     ;

     void Awake() {
         _instance = this;
         tween = this.GetComponent<TweenPosition>();
         numberDialog = this.transform.Find("NumberDialog").gameObject;
         numberInput = transform.Find("NumberDialog/NumberInput").GetComponent<UIInput>();
         numberDialog.SetActive(false);
     }

     public void TransformState() {
         if (isShow == false) {
             tween.PlayForward(); isShow = true;
         } else {
             tween.PlayReverse(); isShow = false;
         }
     }

     public void OnCloseButtonClick() {
         TransformState();
     }

     public void OnBuyId1001() {
         Buy();
     }
     public void OnBuyId1002() {
         Buy();
     }
     public void OnBuyId1003() {
         Buy();
     }
     void Buy(int id) {
         ShowNumberDialog();
         buy_id = id;
     }

     public void OnOKButtonClick() {
         int count = int.Parse(numberInput.value );
         ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(buy_id);
         int price = info.price_buy;
         int price_total = price * count;
         bool success = Inventory._instance.GetCoin(price_total);
         if (success) {//取款成功,可以购买
             ) {
                 Inventory._instance.GetId(buy_id, count);
             }
         }
         numberDialog.SetActive(false);
     }

     void ShowNumberDialog() {
         numberDialog.SetActive(true);
         numberInput.value = ";
     }

 }

ShopDrug

 using UnityEngine;
 using System.Collections;

 public enum ShortCutType{
     Skill,
     Drug,
     None
 }

 public class ShortCutGrid : MonoBehaviour {

     public KeyCode keyCode;

     private ShortCutType type = ShortCutType.None;
     private UISprite icon;
     private int id;
     private SkillInfo skillInfo;
     private ObjectInfo objectInfo;
     private PlayerStatus ps;
     private PlayerAttack pa;

     void Awake() {
         icon = transform.Find("icon").GetComponent<UISprite>();
         icon.gameObject.SetActive(false);
     }

     void Start() {
         ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>();
         pa = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerAttack>();
     }

     void Update() {
         if (Input.GetKeyDown(keyCode)) {
             if (type == ShortCutType.Drug) {
                 OnDrugUse();
             } else if (type == ShortCutType.Skill) {
                 //释放技能
                 //1,得到该技能需要的mp
                 bool success = ps.TakeMP(skillInfo.mp);
                 if (success == false) {

                 } else {
                     //2,获得mp之后,要去释放这个技能
                     pa.UseSkill(skillInfo);
                 }
             }
         }
     }

     public void SetSkill(int id) {
         this.id = id;
         this.skillInfo = SkillsInfo._instance.GetSkillInfoById(id);
         icon.gameObject.SetActive(true);
         icon.spriteName = skillInfo.icon_name;
         type = ShortCutType.Skill;
     }

     public void SetInventory(int id) {
         this.id = id;
         objectInfo = ObjectsInfo._instance.GetObjectInfoById(id);
         if (objectInfo.type == ObjectType.Drug) {
             icon.gameObject.SetActive(true);
             icon.spriteName = objectInfo.icon_name;
             type = ShortCutType.Drug;
         }
     }
     public void OnDrugUse() {
         );
         if (success) {
             ps.GetDrug(objectInfo.hp, objectInfo.mp);
         } else {
             type = ShortCutType.None;
             icon.gameObject.SetActive(false);
             id = ;
             skillInfo = null;
             objectInfo = null;
         }
     }
 }

ShortCutGrid

weaponshop

 using UnityEngine;
 using System.Collections;

 public class ShopWeaponItem : MonoBehaviour {

     private int id;
     private ObjectInfo info;

     private UISprite icon_sprite;
     private UILabel name_label;
     private UILabel effect_label;
     private UILabel pricesell_label;

     void Awake() {
         icon_sprite = transform.Find("icon").GetComponent<UISprite>();
         name_label = transform.Find("name").GetComponent<UILabel>();
         effect_label = transform.Find("effect").GetComponent<UILabel>();
         pricesell_label = transform.Find("price_sell").GetComponent<UILabel>();
     }

     //通过调用这个方法,更新装备的显示
     public void SetId(int id) {
         this.id = id;
         info = ObjectsInfo._instance.GetObjectInfoById(id);

         icon_sprite.spriteName = info.icon_name;
         name_label.text = info.name;
         ) {
             effect_label.text = "+伤害 " + info.attack;
         } ) {
             effect_label.text = "+防御 " + info.def;
         } ){
             effect_label.text = "+速度 " + info.speed;
         }
         pricesell_label.text = info.price_sell.ToString();
     }

     public void OnBuyClick() {
         ShopWeaponUI._instance.OnBuyClick(id);
     }

 }

ShopWeaponItem

 using UnityEngine;
 using System.Collections;

 public class ShopWeaponUI : MonoBehaviour {

     public static ShopWeaponUI _instance;
     public int[] weaponidArray;
     public UIGrid grid;
     public GameObject weaponItem;
     private TweenPosition tween;
     private bool isShow = false;
     private GameObject numberDialog;
     private UIInput numberInput;
     ;

     void Awake() {
         _instance = this;
         tween = this.GetComponent<TweenPosition>();
         numberDialog = transform.Find("Panel/NumberDialog").gameObject;
         numberInput = transform.Find("Panel/NumberDialog/NumberInput").GetComponent<UIInput>();
         numberDialog.SetActive(false);
     }

     void Start() {
         InitShopWeapon();
     }

     public void TransformState() {
         if (isShow) {
             tween.PlayReverse(); isShow = false;
         } else {
             tween.PlayForward(); isShow = true;
         }
     }

     public void OnCloseButtonClick() {
         TransformState();
     }

     void InitShopWeapon() {//初始化武器商店的信息
         foreach (int id in weaponidArray) {
             GameObject itemGo = NGUITools.AddChild(grid.gameObject, weaponItem);
             grid.AddChild(itemGo.transform);
             itemGo.GetComponent<ShopWeaponItem>().SetId(id);
         }
     }

     //ok按钮点击的时候
     public void OnOkBtnClick() {
         int count = int.Parse( numberInput.value );
         ) {
             int price = ObjectsInfo._instance.GetObjectInfoById(buyid).price_buy;
             int total_price = price * count;
             bool success = Inventory._instance.GetCoin(total_price);
             if (success) {
                 Inventory._instance.GetId(buyid, count);
             }
         }

         buyid = ;
         numberInput.value = ";
         numberDialog.SetActive(false);
     }
     public void OnBuyClick(int id) {
         buyid = id;
         numberDialog.SetActive(true);
         numberInput.value = ";
     }

     public void OnClick() {
         numberDialog.SetActive(false);
     }

 }

ShopWeaponUI

自己写的没保存,拿老师项目留个记号

项目:https://pan.baidu.com/s/1bpCHbtX

上一篇:Linux系统教程:设置GRUB菜单密码


下一篇:python3.4学习笔记(二十五) Python 调用mysql redis实例代码