S老师 Top-Down RPG Starter Kit 学习
character creation
1 using UnityEngine; 2 using System.Collections; 3 4 public class CharacterCreation : MonoBehaviour { 5 6 public GameObject[] characterPrefabs; 7 public UIInput nameInput;//用来得到输入的文本 8 private GameObject[] characterGameObjects; 9 private int selectedIndex = 0; 10 private int length;//所有可供选择的角色的个数 11 12 // Use this for initialization 13 void Start () { 14 length = characterPrefabs.Length; 15 characterGameObjects = new GameObject[length]; 16 for (int i = 0; i < length; i++) { 17 characterGameObjects[i] = GameObject.Instantiate(characterPrefabs[i], transform.position, transform.rotation) as GameObject; 18 } 19 UpdateCharacterShow(); 20 } 21 22 void UpdateCharacterShow() {//更新所有角色的显示 23 characterGameObjects[selectedIndex].SetActive(true); 24 for (int i = 0; i < length; i++) { 25 if (i != selectedIndex) { 26 characterGameObjects[i].SetActive(false);//把为选择的角色设置为隐藏 27 } 28 } 29 } 30 31 public void OnNextButtonClick() {//当我们点击了下一个按钮 32 selectedIndex++; 33 selectedIndex %= length; 34 UpdateCharacterShow(); 35 } 36 public void OnPrevButtonClick() {//当我们点击了上一个按钮 37 selectedIndex--; 38 if (selectedIndex == -1) { 39 selectedIndex = length - 1; 40 } 41 UpdateCharacterShow(); 42 } 43 public void OnOkButtonClick() { 44 PlayerPrefs.SetInt("SelectedCharacterIndex", selectedIndex);//存储选择的角色 45 PlayerPrefs.SetString("name", nameInput.value);//存储输入的名字 46 //加载下一个场景 47 Application.LoadLevel(2); 48 } 49 50 }
custom
1 using UnityEngine; 2 using System.Collections; 3 4 public class CursorManager : MonoBehaviour { 5 6 public static CursorManager _instance; 7 8 public Texture2D cursor_normal; 9 public Texture2D cursor_npc_talk; 10 public Texture2D cursor_attack; 11 public Texture2D cursor_lockTarget; 12 public Texture2D cursor_pick; 13 14 private Vector2 hotspot = Vector2.zero; 15 private CursorMode mode = CursorMode.Auto; 16 17 void Start() { 18 _instance = this; 19 } 20 21 public void SetNormal() { 22 Cursor.SetCursor(cursor_normal, hotspot, mode); 23 } 24 public void SetNpcTalk() { 25 Cursor.SetCursor(cursor_npc_talk, hotspot, mode); 26 } 27 public void SetAttack() { 28 Cursor.SetCursor(cursor_attack, hotspot, mode); 29 } 30 public void SetLockTarget() { 31 Cursor.SetCursor(cursor_lockTarget, hotspot, mode); 32 } 33 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class GameLoad : MonoBehaviour { 5 6 public GameObject magicianPrefab; 7 public GameObject swordmanPrefab; 8 9 void Awake() { 10 int selectdIndex = PlayerPrefs.GetInt("SelectedCharacterIndex"); 11 string name = PlayerPrefs.GetString("name"); 12 13 GameObject go = null; 14 if (selectdIndex == 0) { 15 go = GameObject.Instantiate(magicianPrefab) as GameObject; 16 } else if (selectdIndex == 1) { 17 go = GameObject.Instantiate(swordmanPrefab) as GameObject; 18 } 19 go.GetComponent<PlayerStatus>().name = name; 20 } 21 }
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 5 public class ObjectsInfo : MonoBehaviour { 6 7 public static ObjectsInfo _instance; 8 9 private Dictionary<int, ObjectInfo> objectInfoDict = new Dictionary<int, ObjectInfo>(); 10 11 public TextAsset objectsInfoListText; 12 13 void Awake() { 14 _instance = this; 15 ReadInfo(); 16 17 } 18 19 20 public ObjectInfo GetObjectInfoById(int id) { 21 ObjectInfo info=null; 22 23 objectInfoDict.TryGetValue(id, out info); 24 25 return info; 26 } 27 28 void ReadInfo() { 29 string text = objectsInfoListText.text; 30 string[] strArray = text.Split('\n'); 31 32 foreach (string str in strArray) { 33 string[] proArray = str.Split(','); 34 ObjectInfo info = new ObjectInfo(); 35 36 int id = int.Parse(proArray[0]); 37 string name = proArray[1]; 38 string icon_name = proArray[2]; 39 string str_type = proArray[3]; 40 ObjectType type = ObjectType.Drug; 41 switch (str_type) { 42 case "Drug": 43 type = ObjectType.Drug; 44 break; 45 case "Equip": 46 type = ObjectType.Equip; 47 break; 48 case "Mat": 49 type = ObjectType.Mat; 50 break; 51 } 52 info.id = id; info.name = name; info.icon_name = icon_name; 53 info.type = type; 54 if (type == ObjectType.Drug) { 55 int hp = int.Parse(proArray[4]); 56 int mp = int.Parse(proArray[5]); 57 int price_sell = int.Parse(proArray[6]); 58 int price_buy = int.Parse(proArray[7]); 59 info.hp = hp; info.mp = mp; 60 info.price_buy = price_buy; info.price_sell = price_sell; 61 } else if (type == ObjectType.Equip) { 62 info.attack = int.Parse(proArray[4]); 63 info.def = int.Parse(proArray[5]); 64 info.speed = int.Parse(proArray[6]); 65 info.price_sell = int.Parse(proArray[9]); 66 info.price_buy = int.Parse(proArray[10]); 67 string str_dresstype = proArray[7]; 68 switch (str_dresstype) { 69 case "Headgear": 70 info.dressType = DressType.Headgear; 71 break; 72 case "Armor": 73 info.dressType = DressType.Armor; 74 break; 75 case "LeftHand": 76 info.dressType = DressType.LeftHand; 77 break; 78 case "RightHand": 79 info.dressType = DressType.RightHand; 80 break; 81 case "Shoe": 82 info.dressType = DressType.Shoe; 83 break; 84 case "Accessory": 85 info.dressType = DressType.Accessory; 86 break; 87 } 88 string str_apptype = proArray[8]; 89 switch (str_apptype) { 90 case "Swordman": 91 info.applicationType = ApplicationType.Swordman; 92 break; 93 case "Magician": 94 info.applicationType = ApplicationType.Magician; 95 break; 96 case "Common": 97 info.applicationType = ApplicationType.Common; 98 break; 99 } 100 101 } 102 103 objectInfoDict.Add(id, info);//添加到字典中,id为key,可以很方便的根据id查找到这个物品信息 104 } 105 } 106 107 } 108 109 110 111 //id 112 //名称 113 //icon名称 114 //类型(药品drug) 115 //加血量值 116 //加魔法值 117 //出售价 118 //购买 119 public enum ObjectType { 120 Drug, 121 Equip, 122 Mat 123 } 124 public enum DressType { 125 Headgear, 126 Armor, 127 RightHand, 128 LeftHand, 129 Shoe, 130 Accessory 131 } 132 public enum ApplicationType{ 133 Swordman,//剑士 134 Magician,//魔法师 135 Common//通用 136 } 137 138 public class ObjectInfo { 139 public int id; 140 public string name; 141 public string icon_name;//这个名称是存储在图集中的名称 142 public ObjectType type; 143 public int hp; 144 public int mp; 145 public int price_sell; 146 public int price_buy; 147 148 public int attack; 149 public int def; 150 public int speed; 151 public DressType dressType; 152 public ApplicationType applicationType; 153 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class Status : MonoBehaviour { 5 6 public static Status _instance; 7 private TweenPosition tween; 8 private bool isShow = false; 9 10 private UILabel attackLabel; 11 private UILabel defLabel; 12 private UILabel speedLabel; 13 private UILabel pointRemainLabel; 14 private UILabel summaryLabel; 15 16 private GameObject attackButtonGo; 17 private GameObject defButtonGo; 18 private GameObject speedButtonGo; 19 20 private PlayerStatus ps; 21 22 void Awake() { 23 _instance = this; 24 tween = this.GetComponent<TweenPosition>(); 25 26 attackLabel = transform.Find("attack").GetComponent<UILabel>(); 27 defLabel = transform.Find("def").GetComponent<UILabel>(); 28 speedLabel = transform.Find("speed").GetComponent<UILabel>(); 29 pointRemainLabel = transform.Find("point_remain").GetComponent<UILabel>(); 30 summaryLabel = transform.Find("summary").GetComponent<UILabel>(); 31 attackButtonGo = transform.Find("attack_plusbutton").gameObject; 32 defButtonGo = transform.Find("def_plusbutton").gameObject; 33 speedButtonGo = transform.Find("speed_plusbutton").gameObject; 34 35 } 36 void Start() { 37 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 38 } 39 40 41 public void TransformState() { 42 if (isShow == false) { 43 UpdateShow(); 44 tween.PlayForward(); isShow = true; 45 } else { 46 tween.PlayReverse(); isShow = false; 47 } 48 } 49 50 void UpdateShow() {// 更新显示 根据ps playerstatus的属性值,去更新显示 51 attackLabel.text = ps.attack + " + " + ps.attack_plus; 52 defLabel.text = ps.def + " + " + ps.def_plus; 53 speedLabel.text = ps.speed + " + " + ps.speed_plus; 54 55 pointRemainLabel.text = ps.point_remain.ToString(); 56 57 summaryLabel.text = "伤害:" + (ps.attack + ps.attack_plus) 58 + " " + "防御:" + (ps.def + ps.def_plus) 59 + " " + "速度:" + (ps.speed + ps.speed_plus); 60 61 if (ps.point_remain > 0) { 62 attackButtonGo.SetActive(true); 63 defButtonGo.SetActive(true); 64 speedButtonGo.SetActive(true); 65 } else { 66 attackButtonGo.SetActive(false); 67 defButtonGo.SetActive(false); 68 speedButtonGo.SetActive(false); 69 } 70 71 } 72 73 public void OnAttackPlusClick() { 74 bool success = ps.GetPoint(); 75 if (success) { 76 ps.attack_plus++; 77 UpdateShow(); 78 } 79 } 80 public void OnDefPlusClick() { 81 bool success = ps.GetPoint(); 82 if (success) { 83 ps.def_plus++; 84 UpdateShow(); 85 } 86 } 87 public void OnSpeedPlusClick() { 88 bool success = ps.GetPoint(); 89 if (success) { 90 ps.speed_plus++; 91 UpdateShow(); 92 } 93 } 94 95 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class Tags : MonoBehaviour { 5 6 public const string ground = "Ground";//const 标明这个是一个共有的不可变的变量 7 public const string player = "Player"; 8 public const string inventory_item = "InventoryItem"; 9 public const string inventory_item_grid = "InventoryItemGrid"; 10 public const string shortcut = "ShortCut"; 11 public const string minimap = "Minimap"; 12 public const string enemy = "Enemy"; 13 }
enemy
1 using UnityEngine; 2 using System.Collections; 3 4 public enum WolfState{ 5 Idle, 6 Walk, 7 Attack, 8 Death 9 } 10 11 public class WolfBaby : MonoBehaviour { 12 13 public WolfState state = WolfState.Idle; 14 public int hp = 100; 15 public int exp = 20; 16 public int attack = 10; 17 public float miss_rate = 0.2f; 18 public string aniname_death; 19 20 public string aniname_idle; 21 public string aniname_walk; 22 public string aniname_now; 23 public float time = 1; 24 private float timer = 0; 25 26 private CharacterController cc; 27 public float speed = 1; 28 29 private bool is_attacked = false; 30 private Color normal; 31 public AudioClip miss_sound; 32 33 private GameObject hudtextFollow; 34 private GameObject hudtextGo; 35 public GameObject hudtextPrefab; 36 37 private HUDText hudtext; 38 private UIFollowTarget followTarget; 39 public GameObject body; 40 41 public string aniname_normalattack; 42 public float time_normalattack; 43 44 public string aniname_crazyattack; 45 public float time_crazyattack; 46 public float crazyattack_rate; 47 48 public string aniname_attack_now; 49 public int attack_rate =1;//攻击速率 每秒 50 private float attack_timer = 0; 51 52 public Transform target; 53 54 public float minDistance=2; 55 public float maxDistance=5; 56 57 public WolfSpawn spawn; 58 private PlayerStatus ps; 59 60 void Awake() { 61 aniname_now = aniname_idle; 62 63 cc = this.GetComponent<CharacterController>(); 64 normal = body.GetComponent<Renderer>().material.color; 65 hudtextFollow = transform.Find("HUDText").gameObject; 66 } 67 void Start() { 68 //hudtextGo = GameObject.Instantiate(hudtextPrefab, Vector3.zero, Quaternion.identity) as GameObject; 69 //hudtextGo.transform.parent = HUDTextParent._instance.gameObject.transform; 70 hudtextGo= NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab); 71 72 hudtext = hudtextGo.GetComponent<HUDText>(); 73 followTarget = hudtextGo.GetComponent<UIFollowTarget>(); 74 followTarget.target = hudtextFollow.transform; 75 followTarget.gameCamera = Camera.main; 76 77 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 78 //followTarget.uiCamera = UICamera.current.GetComponent<Camera>(); 79 } 80 81 82 void Update() { 83 84 if (state == WolfState.Death) {//死亡 85 GetComponent<Animation>().CrossFade(aniname_death); 86 } else if (state == WolfState.Attack) {//自动攻击状态 87 AutoAttack(); 88 } else {//巡逻 89 GetComponent<Animation>().CrossFade(aniname_now);//播放当前动画 90 if (aniname_now == aniname_walk) { 91 cc.SimpleMove(transform.forward * speed); 92 } 93 94 timer += Time.deltaTime; 95 if (timer >= time) {//计时结束 切换状态 96 timer = 0; 97 RandomState(); 98 } 99 100 } 101 102 } 103 104 void RandomState() { 105 int value = Random.Range(0, 2); 106 if (value == 0) { 107 aniname_now = aniname_idle; 108 } else { 109 if (aniname_now != aniname_walk) { 110 transform.Rotate(transform.up * Random.Range(0, 360));//当状态切换的时候,重新生成方向 111 } 112 aniname_now = aniname_walk; 113 } 114 } 115 116 public void TakeDamage(int attack) {//受到伤害 117 if (state == WolfState.Death) return; 118 target = GameObject.FindGameObjectWithTag(Tags.player).transform; 119 state = WolfState.Attack; 120 float value = Random.Range(0f, 1f); 121 if (value < miss_rate) {// Miss效果 122 AudioSource.PlayClipAtPoint(miss_sound, transform.position); 123 hudtext.Add("Miss", Color.gray, 1); 124 } else {//打中的效果 125 hudtext.Add("-"+attack, Color.red, 1); 126 this.hp -= attack; 127 StartCoroutine( ShowBodyRed() ); 128 if (hp <= 0) { 129 state = WolfState.Death; 130 Destroy(this.gameObject, 2); 131 } 132 } 133 } 134 135 136 IEnumerator ShowBodyRed() { 137 body.GetComponent<Renderer>().material.color = Color.red; 138 yield return new WaitForSeconds(1f); 139 body.GetComponent<Renderer>().material.color = normal; 140 } 141 142 void AutoAttack() { 143 if (target != null) { 144 PlayerState playerState = target.GetComponent<PlayerAttack>().state; 145 if (playerState == PlayerState.Death) { 146 target = null; 147 state = WolfState.Idle; return; 148 } 149 float distance = Vector3.Distance(target.position, transform.position); 150 if (distance > maxDistance) {//停止自动攻击 151 target = null; 152 state = WolfState.Idle; 153 } else if (distance <= minDistance) {//自动攻击 154 attack_timer+=Time.deltaTime; 155 GetComponent<Animation>().CrossFade(aniname_attack_now); 156 if (aniname_attack_now == aniname_normalattack) { 157 if (attack_timer > time_normalattack) { 158 //产生伤害 159 target.GetComponent<PlayerAttack>().TakeDamage(attack); 160 aniname_attack_now = aniname_idle; 161 } 162 } else if (aniname_attack_now == aniname_crazyattack) { 163 if (attack_timer > time_crazyattack) { 164 //产生伤害 165 target.GetComponent<PlayerAttack>().TakeDamage(attack); 166 aniname_attack_now = aniname_idle; 167 } 168 } 169 if (attack_timer > (1f / attack_rate)) { 170 RandomAttack(); 171 attack_timer = 0; 172 } 173 } else {//朝着角色移动 174 transform.LookAt(target); 175 cc.SimpleMove(transform.forward * speed); 176 GetComponent<Animation>().CrossFade(aniname_walk); 177 } 178 } else { 179 state = WolfState.Idle; 180 } 181 } 182 183 void RandomAttack() { 184 float value = Random.Range(0f, 1f); 185 if (value < crazyattack_rate) {//进行疯狂攻击 186 aniname_attack_now = aniname_crazyattack; 187 } else {//进行普通攻击 188 aniname_attack_now = aniname_normalattack; 189 } 190 } 191 192 void OnDestroy() { 193 spawn.MinusNumber(); 194 ps.GetExp(exp); 195 BarNPC._instance.OnKillWolf(); 196 GameObject.Destroy(hudtextGo); 197 } 198 199 void OnMouseEnter() { 200 if(PlayerAttack._instance.isLockingTarget==false) 201 CursorManager._instance.SetAttack(); 202 } 203 void OnMouseExit() { 204 if (PlayerAttack._instance.isLockingTarget == false) 205 CursorManager._instance.SetNormal(); 206 } 207 208 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class WolfSpawn : MonoBehaviour { 5 6 public int maxnum = 5; 7 private int currentnum = 0; 8 public float time = 3; 9 private float timer = 0; 10 public GameObject prefab; 11 void Update() { 12 if (currentnum < maxnum) { 13 timer += Time.deltaTime; 14 if (timer > time) { 15 Vector3 pos = transform.position; 16 pos.x += Random.Range(-5, 5); 17 pos.z += Random.Range(-5, 5); 18 GameObject go = GameObject.Instantiate(prefab, pos, Quaternion.identity) as GameObject; 19 go.GetComponent<WolfBaby>().spawn = this; 20 timer = 0; 21 currentnum++; 22 } 23 } 24 } 25 26 public void MinusNumber() { 27 this.currentnum--; 28 } 29 30 }
inventory
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 5 public class Inventory : MonoBehaviour { 6 7 public static Inventory _instance; 8 9 private TweenPosition tween; 10 private int coinCount = 1000;//金币数量 11 12 public List<InventoryItemGrid> itemGridList = new List<InventoryItemGrid>(); 13 public UILabel coinNumberLabel; 14 public GameObject inventoryItem; 15 16 void Awake() { 17 _instance = this; 18 tween = this.GetComponent<TweenPosition>(); 19 } 20 21 void Update() { 22 if (Input.GetKeyDown(KeyCode.X)) { 23 GetId(Random.Range(2001, 2023)); 24 } 25 } 26 27 //拾取到id的物品,并添加到物品栏里面 28 //处理拾取物品的功能 29 public void GetId(int id,int count =1) { 30 //第一步是查找在所有的物品中是否存在该物品 31 //第二 如果存在,让num +1 32 33 InventoryItemGrid grid = null; 34 foreach (InventoryItemGrid temp in itemGridList) { 35 if (temp.id == id) { 36 grid = temp; break; 37 } 38 } 39 if (grid != null) {//存在的情况 40 grid.PlusNumber(count); 41 } else {//不存在的情况 42 foreach (InventoryItemGrid temp in itemGridList) { 43 if (temp.id == 0) { 44 grid = temp; break; 45 } 46 } 47 if (grid != null) {//第三 不过不存在,查找空的方格,然后把新创建的Inventoryitem放到这个空的方格里面 48 GameObject itemGo = NGUITools.AddChild(grid.gameObject, inventoryItem); 49 itemGo.transform.localPosition = Vector3.zero; 50 itemGo.GetComponent<UISprite>().depth = 4; 51 grid.SetId(id,count); 52 } 53 } 54 } 55 56 public bool MinusId(int id, int count = 1) { 57 InventoryItemGrid grid = null; 58 foreach (InventoryItemGrid temp in itemGridList) { 59 if (temp.id == id) { 60 grid = temp; break; 61 } 62 } 63 if (grid == null) { 64 return false; 65 } else { 66 bool isSuccess = grid.MinusNumber(count); 67 return isSuccess; 68 } 69 } 70 71 private bool isShow = false; 72 73 void Show() { 74 isShow = true; 75 tween.PlayForward(); 76 } 77 78 void Hide() { 79 isShow = false; 80 tween.PlayReverse(); 81 } 82 83 84 public void TransformState() {// 转变状态 85 if (isShow == false) { 86 Show(); 87 } else { 88 Hide(); 89 } 90 } 91 92 public void AddCoin(int count) { 93 coinCount += count; 94 coinNumberLabel.text = coinCount.ToString();//更新金币的显示 95 } 96 97 //这个是取款方法,返回true表示取款成功,返回false取款失败 98 public bool GetCoin(int count) { 99 if (coinCount >= count) { 100 coinCount -= count; 101 coinNumberLabel.text = coinCount.ToString();//更新金币的显示 102 return true; 103 } 104 return false; 105 } 106 107 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class InventoryDes : MonoBehaviour { 5 6 public static InventoryDes _instance; 7 private UILabel label; 8 private float timer = 0; 9 10 void Awake() { 11 _instance = this; 12 label = this.GetComponentInChildren<UILabel>(); 13 this.gameObject.SetActive(false); 14 } 15 16 // Update is called once per frame 17 void Update () { 18 if (this.gameObject.activeInHierarchy == true) { 19 timer -= Time.deltaTime; 20 if (timer <= 0) { 21 this.gameObject.SetActive(false); 22 } 23 } 24 } 25 26 public void Show(int id) { 27 this.gameObject.SetActive(true); timer = 0.1f; 28 transform.position = UICamera.currentCamera.ScreenToWorldPoint(Input.mousePosition); 29 ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id); 30 string des = ""; 31 switch (info.type) { 32 case ObjectType.Drug: 33 des = GetDrugDes(info); 34 break; 35 case ObjectType.Equip: 36 des = GetEquipDes(info); 37 break; 38 } 39 label.text = des; 40 } 41 42 43 string GetDrugDes(ObjectInfo info) { 44 string str = ""; 45 str += "名称:" + info.name + "\n"; 46 str += "+HP : " + info.hp + "\n"; 47 str += "+MP:" + info.mp + "\n"; 48 str += "出售价:" + info.price_sell + "\n"; 49 str += "购买价:" + info.price_buy; 50 51 return str; 52 } 53 54 string GetEquipDes(ObjectInfo info) { 55 string str = ""; 56 str += "名称:" + info.name + "\n"; 57 switch (info.dressType) { 58 case DressType.Headgear: 59 str += "穿戴类型:头盔\n"; 60 break; 61 case DressType.Armor: 62 str += "穿戴类型:盔甲\n"; 63 break; 64 case DressType.LeftHand: 65 str += "穿戴类型:左手\n"; 66 break; 67 case DressType.RightHand: 68 str += "穿戴类型:右手\n"; 69 break; 70 case DressType.Shoe: 71 str += "穿戴类型:鞋\n"; 72 break; 73 case DressType.Accessory: 74 str += "穿戴类型:饰品\n"; 75 break; 76 } 77 switch (info.applicationType) { 78 case ApplicationType.Swordman: 79 str += "适用类型:剑士\n"; 80 break; 81 case ApplicationType.Magician: 82 str += "适用类型:魔法师\n"; 83 break; 84 case ApplicationType.Common: 85 str += "适用类型:通用\n"; 86 break; 87 } 88 89 str += "伤害值:" + info.attack + "\n"; 90 str += "防御值:" + info.def + "\n"; 91 str += "速度值:" + info.speed + "\n"; 92 93 str += "出售价:" + info.price_sell + "\n"; 94 str += "购买价:" + info.price_buy; 95 96 return str; 97 } 98 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class InventoryItem : UIDragDropItem { 5 6 private UISprite sprite; 7 private int id; 8 void Awake() { 9 sprite = this.GetComponent<UISprite>(); 10 } 11 12 void Update() { 13 if (isHover) { 14 //显示提示信息 15 InventoryDes._instance.Show(id); 16 17 if (Input.GetMouseButtonDown(1)) { 18 //出来穿戴功能 19 bool success = EquipmentUI._instance.Dress(id); 20 if (success) { 21 transform.parent.GetComponent<InventoryItemGrid>().MinusNumber(); 22 } 23 } 24 } 25 } 26 27 protected override void OnDragDropRelease(GameObject surface) { 28 base.OnDragDropRelease(surface); 29 if (surface != null) { 30 if (surface.tag == Tags.inventory_item_grid) {//当拖放到了一个空的格子里面 31 if (surface == this.transform.parent.gameObject) {//拖放到了自己的格子里面 32 33 } else { 34 InventoryItemGrid oldParent = this.transform.parent.GetComponent<InventoryItemGrid>(); 35 36 this.transform.parent = surface.transform; ResetPosition(); 37 InventoryItemGrid newParent = surface.GetComponent<InventoryItemGrid>(); 38 newParent.SetId(oldParent.id, oldParent.num); 39 40 oldParent.ClearInfo(); 41 } 42 43 } else if (surface.tag == Tags.inventory_item) {//当拖放到了一个有物品的格子里面 44 InventoryItemGrid grid1 = this.transform.parent.GetComponent<InventoryItemGrid>(); 45 InventoryItemGrid grid2 = surface.transform.parent.GetComponent<InventoryItemGrid>(); 46 int id = grid1.id; int num = grid1.num; 47 grid1.SetId(grid2.id, grid2.num); 48 grid2.SetId(id, num); 49 } else if (surface.tag == Tags.shortcut) {//拖到的快捷方式里面 50 surface.GetComponent<ShortCutGrid>().SetInventory(id); 51 } 52 53 } 54 55 ResetPosition(); 56 } 57 58 void ResetPosition() { 59 transform.localPosition = Vector3.zero; 60 } 61 62 public void SetId(int id) { 63 ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id); 64 sprite.spriteName = info.icon_name; 65 } 66 public void SetIconName(int id,string icon_name) { 67 sprite.spriteName = icon_name; 68 this.id = id; 69 } 70 private bool isHover = false; 71 public void OnHoverOver() { 72 isHover = true; 73 } 74 public void OnHoverOut() { 75 isHover = false; 76 } 77 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class InventoryItemGrid : MonoBehaviour { 5 6 public int id=0; 7 private ObjectInfo info = null; 8 public int num = 0; 9 10 public UILabel numLabel; 11 12 // Use this for initialization 13 void Start () { 14 numLabel = this.GetComponentInChildren<UILabel>(); 15 } 16 17 public void SetId(int id, int num = 1) { 18 this.id = id; 19 info = ObjectsInfo._instance.GetObjectInfoById(id); 20 InventoryItem item = this.GetComponentInChildren<InventoryItem>(); 21 item.SetIconName(id,info.icon_name); 22 numLabel.enabled = true; 23 this.num = num; 24 numLabel.text = num.ToString(); 25 } 26 27 public void PlusNumber(int num = 1) { 28 this.num += num; 29 numLabel.text = this.num.ToString(); 30 } 31 //用来减去数量的,可以用来装备的穿戴,返回值,表示是否减去成功 32 public bool MinusNumber(int num = 1) { 33 if (this.num >= num) { 34 this.num -= num; 35 numLabel.text = this.num.ToString(); 36 if (this.num == 0) { 37 //要清空这个物品格子 38 ClearInfo();//清空存储的信息 39 GameObject.Destroy(this.GetComponentInChildren<InventoryItem>().gameObject);//销毁物品格子 40 } 41 return true; 42 } 43 return false; 44 } 45 46 //清空 格子存的物品信息 47 public void ClearInfo() { 48 id = 0; 49 info = null; 50 num = 0; 51 numLabel.enabled = false; 52 } 53 54 55 }
npc
1 using UnityEngine; 2 using System.Collections; 3 4 public class BarNPC : NPC { 5 6 public static BarNPC _instance; 7 public TweenPosition questTween; 8 public UILabel desLabel; 9 public GameObject acceptBtnGo; 10 public GameObject okBtnGo; 11 public GameObject cancelBtnGo; 12 13 public bool isInTask = false;//表示是否在任务中 14 public int killCount = 0;//表示任务进度,已经杀死了几只小野狼 15 16 private PlayerStatus status; 17 18 void Awake() { 19 _instance = this; 20 } 21 void Start() { 22 status = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 23 } 24 25 void OnMouseOver() {//当鼠标位于这个collider之上的时候,会在每一帧调用这个方法 26 if (Input.GetMouseButtonDown(0)) {//当点击了老爷爷 27 GetComponent<AudioSource>().Play(); 28 if (isInTask) { 29 ShowTaskProgress(); 30 } else { 31 ShowTaskDes(); 32 } 33 ShowQuest(); 34 } 35 } 36 37 void ShowQuest() { 38 questTween.gameObject.SetActive(true); 39 questTween.PlayForward(); 40 } 41 void HideQuest() { 42 questTween.PlayReverse(); 43 } 44 public void OnKillWolf() { 45 if (isInTask) { 46 killCount++; 47 } 48 } 49 50 void ShowTaskDes(){ 51 desLabel.text = "任务:\n杀死了10只狼\n\n奖励:\n1000金币"; 52 okBtnGo.SetActive(false); 53 acceptBtnGo.SetActive(true); 54 cancelBtnGo.SetActive(true); 55 } 56 void ShowTaskProgress(){ 57 desLabel.text = "任务:\n你已经杀死了" + killCount + "\\10只狼\n\n奖励:\n1000金币"; 58 okBtnGo.SetActive(true); 59 acceptBtnGo.SetActive(false); 60 cancelBtnGo.SetActive(false); 61 } 62 63 //任务系统 任务对话框上的按钮点击时间的处理 64 public void OnCloseButtonClick() { 65 HideQuest(); 66 } 67 68 public void OnAcceptButtonClick() { 69 ShowTaskProgress(); 70 isInTask = true;//表示在任务中 71 } 72 public void OnOkButtonClick() { 73 if(killCount>=10){//完成任务 74 Inventory._instance.AddCoin(1000); 75 killCount = 0; 76 ShowTaskDes(); 77 }else{ 78 //没有完成任务 79 HideQuest(); 80 } 81 } 82 public void OnCancelButtonClick() { 83 HideQuest(); 84 } 85 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class NPC : MonoBehaviour { 5 6 7 void OnMouseEnter() { 8 CursorManager._instance.SetNpcTalk(); 9 } 10 void OnMouseExit() { 11 CursorManager._instance.SetNormal(); 12 } 13 14 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class ShopDrugNPC : NPC { 5 6 7 public void OnMouseOver() {//当鼠标在这个游戏物体之上的时候,会一直调用这个方法 8 if (Input.GetMouseButtonDown(0)) {//弹出来药品购买列表 9 GetComponent<AudioSource>().Play(); 10 ShopDrug._instance.TransformState(); 11 } 12 } 13 14 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class ShopWeaponNPC : NPC { 5 public void OnMouseOver() {//当鼠标在这个游戏物体之上的时候,会一直调用这个方法 6 if (Input.GetMouseButtonDown(0)) {//弹出来武器商店 7 GetComponent<AudioSource>().Play(); 8 ShopWeaponUI._instance.TransformState(); 9 } 10 } 11 }
player
1 using UnityEngine; 2 using System.Collections; 3 4 public class FollowPlayer : MonoBehaviour { 5 6 private Transform player; 7 private Vector3 offsetPosition;//位置偏移 8 private bool isRotating = false; 9 10 11 public float distance = 0; 12 public float scrollSpeed = 10; 13 public float rotateSpeed = 2; 14 15 // Use this for initialization 16 void Start () { 17 player = GameObject.FindGameObjectWithTag(Tags.player).transform; 18 transform.LookAt(player.position); 19 offsetPosition = transform.position - player.position; 20 } 21 22 // Update is called once per frame 23 void Update () { 24 transform.position = offsetPosition + player.position; 25 //处理视野的旋转 26 RotateView(); 27 //处理视野的拉近和拉远效果 28 ScrollView(); 29 } 30 31 void ScrollView() { 32 //print(Input.GetAxis("Mouse ScrollWheel"));//向后 返回负值 (拉近视野) 向前滑动 返回正值(拉远视野) 33 distance = offsetPosition.magnitude; 34 distance += Input.GetAxis("Mouse ScrollWheel")*scrollSpeed; 35 distance = Mathf.Clamp(distance, 2, 18); 36 offsetPosition = offsetPosition.normalized * distance; 37 } 38 39 void RotateView() { 40 //Input.GetAxis("Mouse X");//得到鼠标在水平方向的滑动 41 //Input.GetAxis("Mouse Y");//得到鼠标在垂直方向的滑动 42 if (Input.GetMouseButtonDown(1)) { 43 isRotating = true; 44 } 45 if (Input.GetMouseButtonUp(1)) { 46 isRotating = false; 47 } 48 49 if (isRotating) { 50 transform.RotateAround(player.position,player.up, rotateSpeed * Input.GetAxis("Mouse X")); 51 52 Vector3 originalPos = transform.position; 53 Quaternion originalRotation = transform.rotation; 54 55 transform.RotateAround(player.position,transform.right, -rotateSpeed * Input.GetAxis("Mouse Y"));//影响的属性有两个 一个是position 一个是rotation 56 float x = transform.eulerAngles.x; 57 if (x < 10 || x > 80) {//当超出范围之后,我们将属性归位原来的,就是让旋转无效 58 transform.position = originalPos; 59 transform.rotation = originalRotation; 60 } 61 62 } 63 64 offsetPosition = transform.position - player.position; 65 } 66 }
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 5 public class MagicSphere : MonoBehaviour { 6 7 public float attack = 0; 8 9 private List<WolfBaby> wolfList = new List<WolfBaby>(); 10 11 12 public void OnTriggerEnter(Collider col) { 13 if (col.tag == Tags.enemy) { 14 WolfBaby baby = col.GetComponent<WolfBaby>(); 15 int index = wolfList.IndexOf(baby); 16 if (index == -1) { 17 baby.TakeDamage((int) attack); 18 wolfList.Add(baby); 19 } 20 } 21 } 22 23 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class PlayerAnimation : MonoBehaviour { 5 6 private PlayerMove move; 7 private PlayerAttack attack; 8 9 // Use this for initialization 10 void Start () { 11 move = this.GetComponent<PlayerMove>(); 12 attack = this.GetComponent<PlayerAttack>(); 13 } 14 15 // Update is called once per frame 16 void LateUpdate () { 17 if (attack.state == PlayerState.ControlWalk) { 18 if (move.state == ControlWalkState.Moving) { 19 PlayAnim("Run"); 20 } else if (move.state == ControlWalkState.Idle) { 21 PlayAnim("Idle"); 22 } 23 } else if (attack.state == PlayerState.NormalAttack) { 24 if (attack.attack_state == AttackState.Moving) { 25 PlayAnim("Run"); 26 } 27 } 28 } 29 30 void PlayAnim(string animName) { 31 GetComponent<Animation>().CrossFade(animName); 32 } 33 }
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 5 public enum PlayerState { 6 ControlWalk, 7 NormalAttack, 8 SkillAttack, 9 Death 10 } 11 public enum AttackState {//攻击时候的状态 12 Moving, 13 Idle, 14 Attack 15 } 16 17 public class PlayerAttack : MonoBehaviour { 18 19 public static PlayerAttack _instance; 20 21 public PlayerState state = PlayerState.ControlWalk; 22 public AttackState attack_state = AttackState.Idle; 23 24 public string aniname_normalattack;//普通攻击的动画 25 public string aniname_idle; 26 public string aniname_now; 27 public float time_normalattack;//普通攻击的时间 28 public float rate_normalattack = 1; 29 private float timer = 0; 30 public float min_distance = 5;//默认攻击的最小距离 31 private Transform target_normalattack; 32 33 private PlayerMove move; 34 public GameObject effect; 35 private bool showEffect = false; 36 private PlayerStatus ps; 37 public float miss_rate = 0.25f; 38 public GameObject hudtextPrefab; 39 private GameObject hudtextFollow; 40 private GameObject hudtextGo; 41 private HUDText hudtext; 42 public AudioClip miss_sound; 43 public GameObject body; 44 private Color normal; 45 public string aniname_death; 46 47 public GameObject[] efxArray; 48 private Dictionary<string, GameObject> efxDict = new Dictionary<string, GameObject>(); 49 50 public bool isLockingTarget = false;//是否正在选择目标 51 private SkillInfo info = null; 52 53 void Awake() { 54 _instance = this; 55 move = this.GetComponent<PlayerMove>(); 56 ps = this.GetComponent<PlayerStatus>(); 57 normal = body.GetComponent<Renderer>().material.color; 58 59 hudtextFollow = transform.Find("HUDText").gameObject; 60 61 foreach (GameObject go in efxArray) { 62 efxDict.Add(go.name, go); 63 } 64 } 65 66 void Start() { 67 68 hudtextGo = NGUITools.AddChild(HUDTextParent._instance.gameObject, hudtextPrefab); 69 70 hudtext = hudtextGo.GetComponent<HUDText>(); 71 UIFollowTarget followTarget = hudtextGo.GetComponent<UIFollowTarget>(); 72 followTarget.target = hudtextFollow.transform; 73 followTarget.gameCamera = Camera.main; 74 75 } 76 77 void Update() { 78 79 if ( isLockingTarget==false&& Input.GetMouseButtonDown(0) && state != PlayerState.Death ) { 80 //做射线检测 81 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 82 RaycastHit hitInfo; 83 bool isCollider = Physics.Raycast(ray, out hitInfo); 84 if (isCollider && hitInfo.collider.tag == Tags.enemy) { 85 //当我们点击了一个敌人的时候 86 target_normalattack = hitInfo.collider.transform; 87 state = PlayerState.NormalAttack;//进入普通攻击的模式 88 timer = 0; showEffect = false; 89 } else { 90 state = PlayerState.ControlWalk; 91 target_normalattack = null; 92 } 93 } 94 95 if (state == PlayerState.NormalAttack) { 96 if (target_normalattack == null) { 97 state = PlayerState.ControlWalk; 98 } else { 99 float distance = Vector3.Distance(transform.position, target_normalattack.position); 100 if (distance <= min_distance) {//进行攻击 101 transform.LookAt(target_normalattack.position); 102 attack_state = AttackState.Attack; 103 104 timer += Time.deltaTime; 105 GetComponent<Animation>().CrossFade(aniname_now); 106 if (timer >= time_normalattack) { 107 aniname_now = aniname_idle; 108 if (showEffect == false) { 109 showEffect = true; 110 //播放特效 111 GameObject.Instantiate(effect, target_normalattack.position, Quaternion.identity); 112 target_normalattack.GetComponent<WolfBaby>().TakeDamage(GetAttack()); 113 } 114 115 } 116 if (timer >= (1f / rate_normalattack)) { 117 timer = 0; showEffect = false; 118 aniname_now = aniname_normalattack; 119 } 120 121 } else {//走向敌人 122 attack_state = AttackState.Moving; 123 move.SimpleMove(target_normalattack.position); 124 } 125 } 126 } else if (state == PlayerState.Death) {//如果死亡就播放死亡动画 127 GetComponent<Animation>().CrossFade(aniname_death); 128 } 129 130 if (isLockingTarget && Input.GetMouseButtonDown(0)) { 131 OnLockTarget(); 132 } 133 } 134 135 136 public int GetAttack() { 137 return (int)(EquipmentUI._instance.attack + ps.attack + ps.attack_plus); 138 } 139 140 141 public void TakeDamage(int attack) { 142 if (state == PlayerState.Death) return; 143 float def = EquipmentUI._instance.def + ps.def + ps.def_plus; 144 float temp = attack * ((200 - def) / 200); 145 if (temp < 1) temp = 1; 146 147 float value = Random.Range(0f, 1f); 148 if (value < miss_rate) {//MISS 149 AudioSource.PlayClipAtPoint(miss_sound, transform.position); 150 hudtext.Add("MISS", Color.gray, 1); 151 } else { 152 hudtext.Add("-" + temp, Color.red, 1); 153 ps.hp_remain -= (int)temp; 154 StartCoroutine(ShowBodyRed()); 155 if (ps.hp_remain <= 0) { 156 state = PlayerState.Death; 157 } 158 } 159 HeadStatusUI._instance.UpdateShow(); 160 } 161 162 IEnumerator ShowBodyRed() { 163 body.GetComponent<Renderer>().material.color = Color.red; 164 yield return new WaitForSeconds(1f); 165 body.GetComponent<Renderer>().material.color = normal; 166 } 167 168 void OnDestroy() { 169 GameObject.Destroy(hudtextGo); 170 171 } 172 173 public void UseSkill(SkillInfo info) { 174 if (ps.heroType == HeroType.Magician) { 175 if (info.applicableRole == ApplicableRole.Swordman) { 176 return; 177 } 178 } 179 if (ps.heroType == HeroType.Swordman) { 180 if (info.applicableRole == ApplicableRole.Magician) { 181 return; 182 } 183 } 184 switch (info.applyType) { 185 case ApplyType.Passive: 186 StartCoroutine( OnPassiveSkillUse(info)); 187 break; 188 case ApplyType.Buff: 189 StartCoroutine(OnBuffSkillUse(info)); 190 break; 191 case ApplyType.SingleTarget: 192 OnSingleTargetSkillUse(info) ; 193 break; 194 case ApplyType.MultiTarget: 195 OnMultiTargetSkillUse(info); 196 break; 197 } 198 199 } 200 //处理增益技能 201 IEnumerator OnPassiveSkillUse(SkillInfo info ) { 202 state = PlayerState.SkillAttack; 203 GetComponent<Animation>().CrossFade(info.aniname); 204 yield return new WaitForSeconds(info.anitime); 205 state = PlayerState.ControlWalk; 206 int hp = 0, mp = 0; 207 if (info.applyProperty == ApplyProperty.HP) { 208 hp = info.applyValue; 209 } else if (info.applyProperty == ApplyProperty.MP) { 210 mp = info.applyValue; 211 } 212 213 ps.GetDrug(hp,mp); 214 //实例化特效 215 GameObject prefab = null; 216 efxDict.TryGetValue(info.efx_name, out prefab); 217 GameObject.Instantiate(prefab, transform.position, Quaternion.identity); 218 } 219 //处理增强技能 220 IEnumerator OnBuffSkillUse(SkillInfo info) { 221 state = PlayerState.SkillAttack; 222 GetComponent<Animation>().CrossFade(info.aniname); 223 yield return new WaitForSeconds(info.anitime); 224 state = PlayerState.ControlWalk; 225 226 //实例化特效 227 GameObject prefab = null; 228 efxDict.TryGetValue(info.efx_name, out prefab); 229 GameObject.Instantiate(prefab, transform.position, Quaternion.identity); 230 231 switch (info.applyProperty) { 232 case ApplyProperty.Attack: 233 ps.attack *= (info.applyValue / 100f); 234 break; 235 case ApplyProperty.AttackSpeed: 236 rate_normalattack *= (info.applyValue / 100f); 237 break; 238 case ApplyProperty.Def: 239 ps.def *= (info.applyValue / 100f); 240 break; 241 case ApplyProperty.Speed: 242 move.speed *= (info.applyValue / 100f); 243 break; 244 } 245 yield return new WaitForSeconds(info.applyTime); 246 switch (info.applyProperty) { 247 case ApplyProperty.Attack: 248 ps.attack /= (info.applyValue / 100f); 249 break; 250 case ApplyProperty.AttackSpeed: 251 rate_normalattack /= (info.applyValue / 100f); 252 break; 253 case ApplyProperty.Def: 254 ps.def /= (info.applyValue / 100f); 255 break; 256 case ApplyProperty.Speed: 257 move.speed /= (info.applyValue / 100f); 258 break; 259 } 260 } 261 //准备选择目标 262 void OnSingleTargetSkillUse(SkillInfo info) { 263 state = PlayerState.SkillAttack; 264 CursorManager._instance.SetLockTarget(); 265 isLockingTarget = true; 266 this.info = info; 267 } 268 //选择目标完成,开始技能的释放 269 void OnLockTarget() { 270 isLockingTarget = false; 271 switch (info.applyType) { 272 case ApplyType.SingleTarget: 273 StartCoroutine( OnLockSingleTarget()); 274 break; 275 case ApplyType.MultiTarget: 276 StartCoroutine(OnLockMultiTarget()); 277 break; 278 } 279 } 280 281 IEnumerator OnLockSingleTarget() { 282 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 283 RaycastHit hitInfo; 284 bool isCollider = Physics.Raycast(ray, out hitInfo); 285 if (isCollider && hitInfo.collider.tag == Tags.enemy) {//选择了一个敌人 286 GetComponent<Animation>().CrossFade(info.aniname); 287 yield return new WaitForSeconds(info.anitime); 288 state = PlayerState.ControlWalk; 289 //实例化特效 290 GameObject prefab = null; 291 efxDict.TryGetValue(info.efx_name, out prefab); 292 GameObject.Instantiate(prefab, hitInfo.collider.transform.position, Quaternion.identity); 293 294 hitInfo.collider.GetComponent<WolfBaby>().TakeDamage((int)(GetAttack() * (info.applyValue / 100f))); 295 } else { 296 state = PlayerState.NormalAttack; 297 } 298 CursorManager._instance.SetNormal(); 299 } 300 301 void OnMultiTargetSkillUse(SkillInfo info) { 302 state = PlayerState.SkillAttack; 303 CursorManager._instance.SetLockTarget(); 304 isLockingTarget = true; 305 this.info = info; 306 } 307 IEnumerator OnLockMultiTarget() { 308 CursorManager._instance.SetNormal(); 309 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 310 RaycastHit hitInfo; 311 bool isCollider = Physics.Raycast(ray, out hitInfo,11); 312 if (isCollider) { 313 GetComponent<Animation>().CrossFade(info.aniname); 314 yield return new WaitForSeconds(info.anitime); 315 state = PlayerState.ControlWalk; 316 317 //实例化特效 318 GameObject prefab = null; 319 efxDict.TryGetValue(info.efx_name, out prefab); 320 GameObject go = GameObject.Instantiate(prefab, hitInfo.point + Vector3.up * 0.5f, Quaternion.identity) as GameObject; 321 go.GetComponent<MagicSphere>().attack = GetAttack() * (info.applyValue / 100f); 322 } else { 323 state = PlayerState.ControlWalk; 324 } 325 326 } 327 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class PlayerDir : MonoBehaviour { 5 6 public GameObject effect_click_prefab; 7 public Vector3 targetPosition = Vector3.zero; 8 private bool isMoving = false;//表示鼠标是否按下 9 private PlayerMove playerMove; 10 private PlayerAttack attack; 11 12 void Start() { 13 targetPosition = transform.position; 14 playerMove = this.GetComponent<PlayerMove>(); 15 attack = this.GetComponent<PlayerAttack>(); 16 } 17 18 // Update is called once per frame 19 void Update () { 20 if (attack.state == PlayerState.Death) return; 21 if ( attack.isLockingTarget==false&& Input.GetMouseButtonDown(0) && UICamera.hoveredObject == null) { 22 Ray ray =Camera.main.ScreenPointToRay(Input.mousePosition); 23 RaycastHit hitInfo; 24 bool isCollider = Physics.Raycast(ray, out hitInfo); 25 if (isCollider && hitInfo.collider.tag == Tags.ground) { 26 isMoving = true; 27 ShowClickEffect(hitInfo.point); 28 LookAtTarget(hitInfo.point); 29 } 30 } 31 32 if (Input.GetMouseButtonUp(0)) { 33 isMoving = false; 34 } 35 36 if (isMoving) { 37 //得到要移动的目标位置 38 //让主角朝向目标位置 39 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 40 RaycastHit hitInfo; 41 bool isCollider = Physics.Raycast(ray, out hitInfo); 42 if (isCollider && hitInfo.collider.tag == Tags.ground) { 43 LookAtTarget(hitInfo.point); 44 } 45 } else { 46 if (playerMove.isMoving) { 47 LookAtTarget(targetPosition); 48 } 49 } 50 } 51 52 //实例化出来点击的效果 53 void ShowClickEffect( Vector3 hitPoint ) { 54 hitPoint = new Vector3( hitPoint.x,hitPoint.y + 0.1f ,hitPoint.z ); 55 GameObject.Instantiate(effect_click_prefab, hitPoint, Quaternion.identity); 56 } 57 //让主角朝向目标位置 58 void LookAtTarget(Vector3 hitPoint) { 59 targetPosition = hitPoint; 60 targetPosition = new Vector3(targetPosition.x, transform.position.y, targetPosition.z); 61 this.transform.LookAt(targetPosition); 62 } 63 }
1 using UnityEngine; 2 using System.Collections; 3 4 public enum ControlWalkState { 5 Moving, 6 Idle 7 } 8 9 public class PlayerMove : MonoBehaviour { 10 11 public float speed = 4; 12 public ControlWalkState state = ControlWalkState.Idle; 13 private PlayerDir dir; 14 private PlayerAttack attack; 15 private CharacterController controller; 16 public bool isMoving = false; 17 18 void Start() { 19 dir = this.GetComponent<PlayerDir>(); 20 controller = this.GetComponent<CharacterController>(); 21 attack = this.GetComponent<PlayerAttack>(); 22 } 23 // Update is called once per frame 24 void Update () { 25 if (attack.state == PlayerState.ControlWalk) { 26 float distance = Vector3.Distance(dir.targetPosition, transform.position); 27 if (distance > 0.3f) { 28 isMoving = true; 29 state = ControlWalkState.Moving; 30 controller.SimpleMove(transform.forward * speed); 31 } else { 32 isMoving = false; 33 state = ControlWalkState.Idle; 34 } 35 } 36 } 37 38 public void SimpleMove(Vector3 targetPos) { 39 transform.LookAt(targetPos); 40 controller.SimpleMove(transform.forward * speed); 41 } 42 }
1 using UnityEngine; 2 using System.Collections; 3 4 public enum HeroType { 5 Swordman, 6 Magician 7 } 8 9 public class PlayerStatus : MonoBehaviour { 10 11 public HeroType heroType; 12 13 public int level = 1; // 100+level*30 14 15 public string name = "默认名称"; 16 public int hp = 100;//hp最大值 17 public int mp = 100;//mp最大值 18 public float hp_remain = 100; 19 public float mp_remain = 100; 20 public float exp = 0;//当前已经获得的经验 21 22 public float attack = 20; 23 public int attack_plus = 0; 24 public float def = 20; 25 public int def_plus = 0; 26 public float speed = 20; 27 public int speed_plus = 0; 28 29 public int point_remain = 0;//剩余的点数 30 31 void Start() { 32 GetExp(0); 33 } 34 35 36 public void GetDrug(int hp,int mp) {//获得治疗 37 hp_remain += hp; 38 mp_remain += mp; 39 if (hp_remain > this.hp) { 40 hp_remain = this.hp; 41 } 42 if (mp_remain > this.mp) { 43 mp_remain = this.mp; 44 } 45 HeadStatusUI._instance.UpdateShow(); 46 } 47 48 public bool GetPoint(int point=1) {//获得点数 49 if (point_remain >= point) { 50 point_remain -= point; 51 return true; 52 } 53 return false; 54 } 55 56 public void GetExp(int exp) { 57 this.exp += exp; 58 int total_exp = 100 + level * 30; 59 while (this.exp >= total_exp) { 60 //TODO 升级 61 this.level++; 62 this.exp -= total_exp; 63 total_exp = 100 + level * 30; 64 } 65 66 ExpBar._instance.SetValue(this.exp/total_exp ); 67 } 68 69 public bool TakeMP(int count) { 70 if (mp_remain >= count) { 71 mp_remain -= count; 72 HeadStatusUI._instance.UpdateShow(); 73 return true; 74 } else { 75 return false; 76 } 77 } 78 }
skill
1 using UnityEngine; 2 using System.Collections; 3 4 public class SkillItem : MonoBehaviour { 5 6 public int id; 7 private SkillInfo info; 8 9 private UISprite iconname_sprite; 10 private UILabel name_label; 11 private UILabel applytype_label; 12 private UILabel des_label; 13 private UILabel mp_label; 14 15 private GameObject icon_mask; 16 17 18 void InitProperty() { 19 iconname_sprite = transform.Find("icon_name").GetComponent<UISprite>(); 20 name_label = transform.Find("property/name_bg/name").GetComponent<UILabel>(); 21 applytype_label = transform.Find("property/applytype_bg/applytype").GetComponent<UILabel>(); 22 des_label = transform.Find("property/des_bg/des").GetComponent<UILabel>(); 23 mp_label = transform.Find("property/mp_bg/mp").GetComponent<UILabel>(); 24 icon_mask = transform.Find("icon_mask").gameObject; 25 icon_mask.SetActive(false); 26 } 27 28 public void UpdateShow(int level) { 29 if (info.level <= level) {//技能可用 30 icon_mask.SetActive(false); 31 iconname_sprite.GetComponent<SkillItemIcon>().enabled = true; 32 } else { 33 icon_mask.SetActive(true); 34 iconname_sprite.GetComponent<SkillItemIcon>().enabled = false; 35 } 36 } 37 38 //通过调用这个方法,来更新显示 39 public void SetId(int id) { 40 InitProperty(); 41 this.id = id; 42 info = SkillsInfo._instance.GetSkillInfoById(id); 43 iconname_sprite.spriteName = info.icon_name; 44 name_label.text = info.name; 45 switch (info.applyType) { 46 case ApplyType.Passive: 47 applytype_label.text = "增益"; 48 break; 49 case ApplyType.Buff: 50 applytype_label.text = "增强"; 51 break; 52 case ApplyType.SingleTarget: 53 applytype_label.text = "单个目标"; 54 break; 55 case ApplyType.MultiTarget: 56 applytype_label.text = "群体技能"; 57 break; 58 } 59 des_label.text = info.des; 60 mp_label.text = info.mp + "MP"; 61 } 62 63 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class SkillItemIcon : UIDragDropItem { 5 6 private int skillId; 7 8 protected override void OnDragDropStart() {//在克隆的icon上调用的 9 base.OnDragDropStart(); 10 11 skillId = transform.parent.GetComponent<SkillItem>().id; 12 transform.parent = transform.root; 13 this.GetComponent<UISprite>().depth = 40; 14 } 15 16 protected override void OnDragDropRelease(GameObject surface) { 17 base.OnDragDropRelease(surface); 18 if (surface != null && surface.tag == Tags.shortcut) {//当一个技能拖到了快捷方式上的时候 19 surface.GetComponent<ShortCutGrid>().SetSkill(skillId); 20 } 21 } 22 }
1 using UnityEngine; 2 using System.Collections; 3 using System.Collections.Generic; 4 5 public class SkillsInfo : MonoBehaviour { 6 7 public static SkillsInfo _instance; 8 public TextAsset skillsInfoText; 9 10 private Dictionary<int, SkillInfo> skillInfoDict = new Dictionary<int, SkillInfo>(); 11 12 void Awake() { 13 _instance = this; 14 InitSkillInfoDict();//初始化技能信息字典 15 } 16 17 //我们可以通过在这个方法,根据id查找到一个技能信息 18 public SkillInfo GetSkillInfoById(int id) { 19 SkillInfo info = null; 20 skillInfoDict.TryGetValue(id, out info); 21 return info; 22 } 23 24 //初始化技能信息字典 25 void InitSkillInfoDict() { 26 string text = skillsInfoText.text; 27 string[] skillinfoArray = text.Split('\n'); 28 foreach (string skillinfoStr in skillinfoArray) { 29 string[] pa = skillinfoStr.Split(','); 30 SkillInfo info = new SkillInfo(); 31 info.id = int.Parse(pa[0]); 32 info.name = pa[1]; 33 info.icon_name = pa[2]; 34 info.des = pa[3]; 35 string str_applytype = pa[4]; 36 switch (str_applytype) { 37 case "Passive": 38 info.applyType = ApplyType.Passive; 39 break; 40 case "Buff": 41 info.applyType = ApplyType.Buff; 42 break; 43 case "SingleTarget": 44 info.applyType = ApplyType.SingleTarget; 45 break; 46 case "MultiTarget": 47 info.applyType = ApplyType.MultiTarget; 48 break; 49 } 50 string str_applypro = pa[5]; 51 switch (str_applypro) { 52 case "Attack": 53 info.applyProperty = ApplyProperty.Attack; 54 break; 55 case "Def": 56 info.applyProperty = ApplyProperty.Def; 57 break; 58 case "Speed": 59 info.applyProperty = ApplyProperty.Speed; 60 break; 61 case "AttackSpeed": 62 info.applyProperty = ApplyProperty.AttackSpeed; 63 break; 64 case "HP": 65 info.applyProperty = ApplyProperty.HP; 66 break; 67 case "MP": 68 info.applyProperty = ApplyProperty.MP; 69 break; 70 } 71 info.applyValue = int.Parse(pa[6]); 72 info.applyTime = int.Parse(pa[7]); 73 info.mp = int.Parse(pa[8]); 74 info.coldTime = int.Parse(pa[9]); 75 switch (pa[10]) { 76 case "Swordman": 77 info.applicableRole = ApplicableRole.Swordman; 78 break; 79 case "Magician": 80 info.applicableRole = ApplicableRole.Magician; 81 break; 82 } 83 info.level = int.Parse(pa[11]); 84 switch (pa[12]) { 85 case "Self": 86 info.releaseType = ReleaseType.Self; 87 break; 88 case "Enemy": 89 info.releaseType = ReleaseType.Enemy; 90 break; 91 case "Position": 92 info.releaseType = ReleaseType.Position; 93 break; 94 } 95 info.distance = float.Parse(pa[13]); 96 info.efx_name = pa[14]; 97 info.aniname = pa[15]; 98 info.anitime = float.Parse(pa[16]); 99 skillInfoDict.Add(info.id, info); 100 } 101 } 102 103 } 104 //适用角色 105 public enum ApplicableRole { 106 Swordman, 107 Magician 108 } 109 //作用类型 110 public enum ApplyType { 111 Passive, 112 Buff, 113 SingleTarget, 114 MultiTarget 115 } 116 //作用属性 117 public enum ApplyProperty { 118 Attack, 119 Def, 120 Speed, 121 AttackSpeed, 122 HP, 123 MP 124 } 125 //释放类型 126 public enum ReleaseType { 127 Self, 128 Enemy, 129 Position 130 } 131 //技能信息 132 public class SkillInfo { 133 public int id; 134 public string name; 135 public string icon_name; 136 public string des; 137 public ApplyType applyType; 138 public ApplyProperty applyProperty; 139 public int applyValue; 140 public int applyTime; 141 public int mp; 142 public int coldTime; 143 public ApplicableRole applicableRole; 144 public int level; 145 public ReleaseType releaseType; 146 public float distance; 147 public string efx_name; 148 public string aniname; 149 public float anitime = 0; 150 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class SkillUI : MonoBehaviour { 5 6 public static SkillUI _instance; 7 private TweenPosition tween; 8 private bool isShow = false; 9 private PlayerStatus ps; 10 11 public UIGrid grid; 12 public GameObject skillItemPrefab; 13 public int[] magicianSkillIdList; 14 public int[] swordmanSkillIdList; 15 16 17 void Awake() { 18 _instance = this; 19 tween = this.GetComponent<TweenPosition>(); 20 } 21 void Start() { 22 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 23 int[] idList = null; 24 switch (ps.heroType) { 25 case HeroType.Magician: 26 idList = magicianSkillIdList; 27 break; 28 case HeroType.Swordman: 29 idList = swordmanSkillIdList; 30 break; 31 } 32 foreach (int id in idList) { 33 GameObject itemGo = NGUITools.AddChild(grid.gameObject, skillItemPrefab); 34 grid.AddChild(itemGo.transform); 35 itemGo.GetComponent<SkillItem>().SetId(id); 36 } 37 38 } 39 40 41 public void TransformState() { 42 if (isShow) { 43 tween.PlayReverse(); isShow = false; 44 } else { 45 tween.PlayForward(); isShow = true; 46 UpdateShow(); 47 } 48 } 49 50 void UpdateShow() { 51 SkillItem[] items = this.GetComponentsInChildren<SkillItem>(); 52 foreach (SkillItem item in items) { 53 item.UpdateShow(ps.level); 54 } 55 } 56 }
start
1 using UnityEngine; 2 using System.Collections; 3 4 public class ButtonContainer : MonoBehaviour { 5 6 //1,游戏数据的保存,和场景之间游戏数据的传递使用 PlayerPrefs 7 //2,场景的分类 8 //2.1开始场景 9 //2.2角色选择界面 10 //2.3游戏玩家打怪的界面,就是游戏实际的运行场景 村庄有野兽。。。 11 12 13 //开始新游戏 14 public void OnNewGame() { 15 PlayerPrefs.SetInt("DataFromSave", 0); 16 // 加载我们的选择角色的场景 2 17 Application.LoadLevel(1); 18 } 19 //加载已经保存的游戏 20 public void OnLoadGame() { 21 PlayerPrefs.SetInt("DataFromSave", 1); //DataFromSave表示数据来自保存 22 //加载我们的play场景3 23 } 24 25 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class MovieCamera : MonoBehaviour { 5 6 public float speed = 10; 7 8 private float endZ = -20; 9 10 // Use this for initialization 11 void Start () { 12 13 } 14 15 // Update is called once per frame 16 void Update () { 17 if (transform.position.z < endZ) {//还没有达到目标位置,需要移动 18 transform.Translate( Vector3.forward*speed*Time.deltaTime); 19 } 20 } 21 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class PressAnyKey : MonoBehaviour { 5 6 private bool isAnyKeyDown = false;//表示是否有任何按键按下 7 private GameObject buttonContainer; 8 9 void Start() { 10 buttonContainer = this.transform.parent.Find("buttonContainer").gameObject; 11 } 12 13 // Update is called once per frame 14 void Update () { 15 if (isAnyKeyDown == false) { 16 if (Input.anyKey) { 17 // show button container 18 //hide self 19 ShowButton(); 20 } 21 } 22 } 23 24 void ShowButton() { 25 buttonContainer.SetActive(true); 26 this.gameObject.SetActive(false); 27 isAnyKeyDown = true; 28 } 29 }
ui
1 using UnityEngine; 2 using System.Collections; 3 4 public class EquipmentItem : MonoBehaviour { 5 6 private UISprite sprite; 7 public int id; 8 private bool isHover = false; 9 10 void Awake() { 11 sprite = this.GetComponent<UISprite>(); 12 } 13 14 void Update() { 15 if (isHover) {//当鼠标在这个装备栏之上的时候,检测鼠标右键的点击 16 if (Input.GetMouseButtonDown(1)) {//鼠标右键的点击,表示卸下该装备 17 EquipmentUI._instance.TakeOff(id,this.gameObject); 18 } 19 } 20 } 21 22 public void SetId(int id) { 23 this.id = id; 24 ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id); 25 SetInfo(info); 26 } 27 public void SetInfo(ObjectInfo info) { 28 this.id = info.id; 29 30 sprite.spriteName = info.icon_name; 31 } 32 33 public void OnHover(bool isOver) { 34 isHover = isOver; 35 } 36 37 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class EquipmentUI : MonoBehaviour { 5 6 public static EquipmentUI _instance; 7 private TweenPosition tween; 8 private bool isShow = false; 9 10 private GameObject headgear; 11 private GameObject armor; 12 private GameObject rightHand; 13 private GameObject leftHand; 14 private GameObject shoe; 15 private GameObject accessory; 16 17 private PlayerStatus ps; 18 19 public GameObject equipmentItem; 20 21 public int attack = 0; 22 public int def = 0; 23 public int speed = 0; 24 25 void Awake() { 26 _instance = this; 27 tween = this.GetComponent<TweenPosition>(); 28 29 headgear = transform.Find("Headgear").gameObject; 30 armor = transform.Find("Armor").gameObject; 31 rightHand = transform.Find("RightHand").gameObject; 32 leftHand = transform.Find("LeftHand").gameObject; 33 shoe = transform.Find("Shoe").gameObject; 34 accessory = transform.Find("Accessory").gameObject; 35 36 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 37 } 38 39 40 public void TransformState() { 41 if (isShow == false) { 42 tween.PlayForward(); 43 isShow = true; 44 } else { 45 tween.PlayReverse(); 46 isShow = false; 47 } 48 } 49 50 //处理装备穿戴功能 51 public bool Dress(int id) { 52 ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(id); 53 if (info.type != ObjectType.Equip) { 54 return false;//穿戴不成功 55 } 56 if (ps.heroType == HeroType.Magician) { 57 if (info.applicationType == ApplicationType.Swordman) { 58 return false; 59 } 60 } 61 if (ps.heroType == HeroType.Swordman) { 62 if (info.applicationType == ApplicationType.Magician) { 63 return false; 64 } 65 } 66 67 GameObject parent = null; 68 switch (info.dressType) { 69 case DressType.Headgear: 70 parent = headgear; 71 break; 72 case DressType.Armor: 73 parent = armor; 74 break; 75 case DressType.RightHand: 76 parent = rightHand; 77 break; 78 case DressType.LeftHand: 79 parent = leftHand; 80 break; 81 case DressType.Shoe: 82 parent = shoe; 83 break; 84 case DressType.Accessory: 85 parent = accessory; 86 break; 87 } 88 EquipmentItem item = parent.GetComponentInChildren<EquipmentItem>(); 89 if (item != null) {//说明已经穿戴了同样类型的装备 90 Inventory._instance.GetId(item.id);//把已经穿戴的装备卸下,放回物品栏 91 item.SetInfo(info); 92 } else {//没有穿戴同样类型的装备 93 GameObject itemGo = NGUITools.AddChild(parent, equipmentItem); 94 itemGo.transform.localPosition = Vector3.zero; 95 itemGo.GetComponent<EquipmentItem>().SetInfo(info); 96 } 97 UpdateProperty(); 98 return true; 99 } 100 public void TakeOff(int id,GameObject go) { 101 Inventory._instance.GetId(id); 102 GameObject.Destroy(go); 103 UpdateProperty(); 104 } 105 106 107 void UpdateProperty() { 108 this.attack = 0; 109 this.def = 0; 110 this.speed = 0; 111 112 EquipmentItem headgearItem = headgear.GetComponentInChildren<EquipmentItem>(); 113 PlusProperty(headgearItem); 114 EquipmentItem armorItem = armor.GetComponentInChildren<EquipmentItem>(); 115 PlusProperty(armorItem); 116 EquipmentItem leftHandItem = leftHand.GetComponentInChildren<EquipmentItem>(); 117 PlusProperty(leftHandItem); 118 EquipmentItem rightHandItem = rightHand.GetComponentInChildren<EquipmentItem>(); 119 PlusProperty(rightHandItem); 120 EquipmentItem shoeItem = shoe.GetComponentInChildren<EquipmentItem>(); 121 PlusProperty(shoeItem); 122 EquipmentItem accessoryItem = accessory.GetComponentInChildren<EquipmentItem>(); 123 PlusProperty(accessoryItem); 124 } 125 126 void PlusProperty(EquipmentItem item) { 127 if (item != null) { 128 ObjectInfo equipInfo = ObjectsInfo._instance.GetObjectInfoById(item.id); 129 this.attack += equipInfo.attack; 130 this.def += equipInfo.def; 131 this.speed += equipInfo.speed; 132 } 133 } 134 135 136 }
1 using UnityEngine; 2 3 using System.Collections; 4 5 public class ExpBar : MonoBehaviour { 6 7 public static ExpBar _instance; 8 private UISlider progressBar; 9 void Awake() { 10 _instance = this; 11 progressBar = this.GetComponent<UISlider>(); 12 } 13 14 public void SetValue(float value) { 15 progressBar.value = value; 16 } 17 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class FunctionBar : MonoBehaviour { 5 6 public void OnStatusButtonClick() { 7 Status._instance.TransformState(); 8 } 9 public void OnBagButtonClick() { 10 Inventory._instance.TransformState(); 11 } 12 public void OnEquipButtonClick() { 13 EquipmentUI._instance.TransformState(); 14 } 15 public void OnSkillButtonClick() { 16 SkillUI._instance.TransformState(); 17 } 18 public void OnSettingButtonClick() { 19 } 20 21 22 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class HeadStatusUI : MonoBehaviour { 5 6 public static HeadStatusUI _instance; 7 8 private UILabel name; 9 10 private UISlider hpBar; 11 private UISlider mpBar; 12 13 private UILabel hpLabel; 14 private UILabel mpLabel; 15 private PlayerStatus ps; 16 17 void Awake() { 18 _instance = this; 19 name = transform.Find("Name").GetComponent<UILabel>(); 20 hpBar = transform.Find("HP").GetComponent<UISlider>(); 21 mpBar = transform.Find("MP").GetComponent<UISlider>(); 22 23 hpLabel = transform.Find("HP/Thumb/Label").GetComponent<UILabel>(); 24 mpLabel = transform.Find("MP/Thumb/Label").GetComponent<UILabel>(); 25 } 26 27 void Start() { 28 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 29 UpdateShow(); 30 } 31 32 public void UpdateShow() { 33 name.text = "Lv." + ps.level + " " + ps.name; 34 hpBar.value = ps.hp_remain / ps.hp; 35 mpBar.value = ps.mp_remain / ps.mp; 36 37 hpLabel.text = ps.hp_remain + "/" + ps.hp; 38 mpLabel.text = ps.mp_remain + "/" + ps.mp; 39 } 40 41 42 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class HUDTextParent : MonoBehaviour { 5 6 public static HUDTextParent _instance; 7 8 void Awake() { 9 _instance = this; 10 } 11 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class Minimap : MonoBehaviour { 5 6 private Camera minimapCamera; 7 8 void Start() { 9 minimapCamera = GameObject.FindGameObjectWithTag(Tags.minimap).GetComponent<Camera>(); 10 } 11 12 public void OnZoomInClick() { 13 //放大 14 minimapCamera.orthographicSize--; 15 } 16 public void OnZoomOutClick() { 17 //缩小 18 minimapCamera.orthographicSize++; 19 } 20 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class ShopDrug : MonoBehaviour { 5 6 7 public static ShopDrug _instance; 8 private TweenPosition tween; 9 private bool isShow = false; 10 11 private GameObject numberDialog; 12 private UIInput numberInput; 13 private int buy_id = 0; 14 15 void Awake() { 16 _instance = this; 17 tween = this.GetComponent<TweenPosition>(); 18 numberDialog = this.transform.Find("NumberDialog").gameObject; 19 numberInput = transform.Find("NumberDialog/NumberInput").GetComponent<UIInput>(); 20 numberDialog.SetActive(false); 21 } 22 23 public void TransformState() { 24 if (isShow == false) { 25 tween.PlayForward(); isShow = true; 26 } else { 27 tween.PlayReverse(); isShow = false; 28 } 29 } 30 31 public void OnCloseButtonClick() { 32 TransformState(); 33 } 34 35 36 public void OnBuyId1001() { 37 Buy(1001); 38 } 39 public void OnBuyId1002() { 40 Buy(1002); 41 } 42 public void OnBuyId1003() { 43 Buy(1003); 44 } 45 void Buy(int id) { 46 ShowNumberDialog(); 47 buy_id = id; 48 } 49 50 public void OnOKButtonClick() { 51 int count = int.Parse(numberInput.value ); 52 ObjectInfo info = ObjectsInfo._instance.GetObjectInfoById(buy_id); 53 int price = info.price_buy; 54 int price_total = price * count; 55 bool success = Inventory._instance.GetCoin(price_total); 56 if (success) {//取款成功,可以购买 57 if (count > 0) { 58 Inventory._instance.GetId(buy_id, count); 59 } 60 } 61 numberDialog.SetActive(false); 62 } 63 64 void ShowNumberDialog() { 65 numberDialog.SetActive(true); 66 numberInput.value = "0"; 67 } 68 69 }
1 using UnityEngine; 2 using System.Collections; 3 4 public enum ShortCutType{ 5 Skill, 6 Drug, 7 None 8 } 9 10 public class ShortCutGrid : MonoBehaviour { 11 12 public KeyCode keyCode; 13 14 private ShortCutType type = ShortCutType.None; 15 private UISprite icon; 16 private int id; 17 private SkillInfo skillInfo; 18 private ObjectInfo objectInfo; 19 private PlayerStatus ps; 20 private PlayerAttack pa; 21 22 void Awake() { 23 icon = transform.Find("icon").GetComponent<UISprite>(); 24 icon.gameObject.SetActive(false); 25 } 26 27 void Start() { 28 ps = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerStatus>(); 29 pa = GameObject.FindGameObjectWithTag(Tags.player).GetComponent<PlayerAttack>(); 30 } 31 32 void Update() { 33 if (Input.GetKeyDown(keyCode)) { 34 if (type == ShortCutType.Drug) { 35 OnDrugUse(); 36 } else if (type == ShortCutType.Skill) { 37 //释放技能 38 //1,得到该技能需要的mp 39 bool success = ps.TakeMP(skillInfo.mp); 40 if (success == false) { 41 42 } else { 43 //2,获得mp之后,要去释放这个技能 44 pa.UseSkill(skillInfo); 45 } 46 } 47 } 48 } 49 50 public void SetSkill(int id) { 51 this.id = id; 52 this.skillInfo = SkillsInfo._instance.GetSkillInfoById(id); 53 icon.gameObject.SetActive(true); 54 icon.spriteName = skillInfo.icon_name; 55 type = ShortCutType.Skill; 56 } 57 58 public void SetInventory(int id) { 59 this.id = id; 60 objectInfo = ObjectsInfo._instance.GetObjectInfoById(id); 61 if (objectInfo.type == ObjectType.Drug) { 62 icon.gameObject.SetActive(true); 63 icon.spriteName = objectInfo.icon_name; 64 type = ShortCutType.Drug; 65 } 66 } 67 public void OnDrugUse() { 68 bool success = Inventory._instance.MinusId(id, 1); 69 if (success) { 70 ps.GetDrug(objectInfo.hp, objectInfo.mp); 71 } else { 72 type = ShortCutType.None; 73 icon.gameObject.SetActive(false); 74 id = 0; 75 skillInfo = null; 76 objectInfo = null; 77 } 78 } 79 }
weaponshop
1 using UnityEngine; 2 using System.Collections; 3 4 public class ShopWeaponItem : MonoBehaviour { 5 6 private int id; 7 private ObjectInfo info; 8 9 private UISprite icon_sprite; 10 private UILabel name_label; 11 private UILabel effect_label; 12 private UILabel pricesell_label; 13 14 void Awake() { 15 icon_sprite = transform.Find("icon").GetComponent<UISprite>(); 16 name_label = transform.Find("name").GetComponent<UILabel>(); 17 effect_label = transform.Find("effect").GetComponent<UILabel>(); 18 pricesell_label = transform.Find("price_sell").GetComponent<UILabel>(); 19 } 20 21 //通过调用这个方法,更新装备的显示 22 public void SetId(int id) { 23 this.id = id; 24 info = ObjectsInfo._instance.GetObjectInfoById(id); 25 26 icon_sprite.spriteName = info.icon_name; 27 name_label.text = info.name; 28 if (info.attack > 0) { 29 effect_label.text = "+伤害 " + info.attack; 30 } else if (info.def > 0) { 31 effect_label.text = "+防御 " + info.def; 32 } else if(info.speed>0){ 33 effect_label.text = "+速度 " + info.speed; 34 } 35 pricesell_label.text = info.price_sell.ToString(); 36 } 37 38 public void OnBuyClick() { 39 ShopWeaponUI._instance.OnBuyClick(id); 40 } 41 42 43 }
1 using UnityEngine; 2 using System.Collections; 3 4 public class ShopWeaponUI : MonoBehaviour { 5 6 public static ShopWeaponUI _instance; 7 public int[] weaponidArray; 8 public UIGrid grid; 9 public GameObject weaponItem; 10 private TweenPosition tween; 11 private bool isShow = false; 12 private GameObject numberDialog; 13 private UIInput numberInput; 14 private int buyid = 0; 15 16 void Awake() { 17 _instance = this; 18 tween = this.GetComponent<TweenPosition>(); 19 numberDialog = transform.Find("Panel/NumberDialog").gameObject; 20 numberInput = transform.Find("Panel/NumberDialog/NumberInput").GetComponent<UIInput>(); 21 numberDialog.SetActive(false); 22 } 23 24 25 void Start() { 26 InitShopWeapon(); 27 } 28 29 public void TransformState() { 30 if (isShow) { 31 tween.PlayReverse(); isShow = false; 32 } else { 33 tween.PlayForward(); isShow = true; 34 } 35 } 36 37 public void OnCloseButtonClick() { 38 TransformState(); 39 } 40 41 void InitShopWeapon() {//初始化武器商店的信息 42 foreach (int id in weaponidArray) { 43 GameObject itemGo = NGUITools.AddChild(grid.gameObject, weaponItem); 44 grid.AddChild(itemGo.transform); 45 itemGo.GetComponent<ShopWeaponItem>().SetId(id); 46 } 47 } 48 49 //ok按钮点击的时候 50 public void OnOkBtnClick() { 51 int count = int.Parse( numberInput.value ); 52 if (count > 0) { 53 int price = ObjectsInfo._instance.GetObjectInfoById(buyid).price_buy; 54 int total_price = price * count; 55 bool success = Inventory._instance.GetCoin(total_price); 56 if (success) { 57 Inventory._instance.GetId(buyid, count); 58 } 59 } 60 61 buyid = 0; 62 numberInput.value = "0"; 63 numberDialog.SetActive(false); 64 } 65 public void OnBuyClick(int id) { 66 buyid = id; 67 numberDialog.SetActive(true); 68 numberInput.value = "0"; 69 } 70 71 public void OnClick() { 72 numberDialog.SetActive(false); 73 } 74 75 76 }
自己写的没保存,拿老师项目留个记号
项目:https://pan.baidu.com/s/1bpCHbtX