不多说。只贴代码。
Skill.cs
using UnityEngine; using System.Collections; public class Skill : MonoBehaviour { public static Skill _instance; public GameObject skill2Button; public GameObject suo2; public GameObject buySkill; public UILabel icon; private TweenPosition Tween; public bool isShow = false; public bool getSkill = false; private int AttackSkill1 = 20; private int AttackSkill2 = 30; // Use this for initialization void Awake () { Tween = this.GetComponent<TweenPosition>(); _instance = this; } // Update is called once per frame void Update () { icon.text = Inventory._instance.coinCount.ToString(); if (Inventory._instance.coinCount >= 1000&&getSkill==false) { buySkill.SetActive(true); } else { buySkill.SetActive(false); } } public void BuySkill() { Inventory._instance.coinCount = Inventory._instance.coinCount - 1000; buySkill.SetActive(false); suo2.SetActive(true); skill2Button.SetActive(true); getSkill = true; } public void Hide() { Tween.PlayReverse(); isShow = false; } public void ShowDilog() { Tween.PlayForward(); isShow = true; Status._instance.Hide(); Inventory._instance.Hide(); ShopDurg._instance.Hide(); BarNPC._instance.OnCloseButtonClick(); Equip._instance.Hide(); } public void transfromStatus() { if (isShow) { Hide(); } else { ShowDilog(); } } }
using UnityEngine; using System.Collections; public class Status : MonoBehaviour { public static Status _instance; private TweenPosition tween; public 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; 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() { } public void TransformState() { if (isShow == false) { UpdateShow(); tween.PlayForward(); isShow = true; } else { Hide(); } } public void Hide() { tween.PlayReverse(); isShow = false; } void UpdateShow() {// 更新显示 根据PlayerStatus的属性值,去更新显示 ps attackLabel.text = PlayerStatus._instance.attack.ToString() ; defLabel.text = PlayerStatus._instance.def.ToString(); speedLabel.text = PlayerStatus._instance.speed.ToString(); pointRemainLabel.text = PlayerStatus._instance.point_remain.ToString(); summaryLabel.text = "伤害:" + (PlayerStatus._instance.attack ) + " " + "防御:" + (PlayerStatus._instance.def) + " " + "速度:" + (PlayerStatus._instance.speed ); if (PlayerStatus._instance.point_remain > 0) { attackButtonGo.SetActive(true); defButtonGo.SetActive(true); speedButtonGo.SetActive(true); } else { attackButtonGo.SetActive(false); defButtonGo.SetActive(false); speedButtonGo.SetActive(false); } } public void OnAttackPlusClick() { bool success = PlayerStatus._instance.GetPoint(); if (success) { PlayerStatus._instance.attack++; UpdateShow(); } } public void OnDefPlusClick() { bool success = PlayerStatus._instance.GetPoint(); if (success) { PlayerStatus._instance.def++; UpdateShow(); } } public void OnSpeedPlusClick() { bool success = PlayerStatus._instance.GetPoint(); if (success) { PlayerStatus._instance.speed++; UpdateShow(); } } }
using UnityEngine; using System.Collections; public class Equip : MonoBehaviour { public static Equip _instance; public UILabel Icon; public UILabel Greed; private int GreedNumber = 1; public UILabel AttackNumber; private int AddAttack = 0; public UISprite weepon; private TweenPosition Tween; public bool isShow=false; public GameObject stonger; private int name = 0; void Awake() { if (name == 0) { weepon.spriteName = "rod-icon03"; } else if(name ==1) { weepon.spriteName = "sword2-icon"; } // Icon.text = Inventory._instance.coinCount.ToString(); Greed.text ="强化等级"+GreedNumber+" "+"再次强化需要500金币"; AttackNumber.text = " " + "加伤害值:"+AddAttack; Tween = this.GetComponent<TweenPosition>(); _instance = this;} void Start(){ name = PlayerStatus._instance.name; } void Update() { Icon.text = Inventory._instance.coinCount.ToString(); Greed.text = "强化等级" + GreedNumber + " " + "再次强化需要500金币"; AttackNumber.text = " " + "加伤害值:" + AddAttack; if (Inventory._instance.coinCount >= 500) { stonger.SetActive(true); } else { stonger.SetActive(false); } } public void Stonger() { AddAttack += 10; GreedNumber += 1; Inventory._instance.coinCount = Inventory._instance.coinCount - 500; PlayerStatus._instance.attack += AddAttack; } public void ShowDilog() { Tween.PlayForward(); isShow = true; Inventory._instance.Hide(); Status._instance.Hide(); ShopDurg._instance.Hide(); BarNPC._instance.OnCloseButtonClick(); } public void Hide() { Tween.PlayReverse(); isShow = false; } public void transfromStatus() { if (isShow) { Hide(); } else { ShowDilog(); } } }
using UnityEngine; using System.Collections; public class Head : MonoBehaviour { public UILabel nameLabe; public UISlider hp; public UISlider mp; // Use this for initialization void Start () { } // Update is called once per frame void Update () { nameLabe.text = "Lv."+PlayerStatus._instance.grade +" "+PlayerStatus._instance.nameString; hp.value = ((float)PlayerStatus._instance.hp) / 100f; mp.value = ((float)PlayerStatus._instance.mp) / 100f; } }
1、最普通的单例:(样式一)
public class Singleton { static Singleton instance; public static Singleton Instance { get { if (instance == null) { instance = new Singleton (); } return instance; } } }
using UnityEngine; public class UnitySingleton : MonoBehaviour { static UnitySingleton instance; public static UnitySingleton Instance { get { if ( instance == null ) { GameObject obj = new GameObject("UnitySingleton"); instance = obj.AddComponent(typeof(UnitySingleton)) as UnitySingleton; } return instance; } } }
using UnityEngine; public class UnitySingletonG : MonoBehaviour where T : Component { private static T _instance; public static T Instance { get { if ( _instance == null ) { GameObject obj = new GameObject("UnitySingletonG"); // 隐藏实例化的new game object,下同 //obj.hideFlags = HideFlags.HideAndDontSave; // 不删除该物体 DontDestroyOnLoad(obj); _instance = obj.AddComponent(typeof(T)) as T; } return _instance; } } }