您的位置:首页 > 其它

RPG黑暗之光(6)状态栏/技能系统/武器系统/小地图/头像栏

2015-08-29 13:32 381 查看

RPG黑暗之光(6)状态栏/技能系统/武器系统/小地图/头像栏









不多说。只贴代码。

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();
        }
    }
}


Status.cs

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();
        }
    }

}


Equip.cs

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();
        }
    }
}


Head.cs

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;
	}
}


单例模式(singleton pattern)大家都不陌生,探讨一下单例模式在unity中的实现,比起一般的单例,unity中有些他的特点。

1、最普通的单例:(样式一)

public class Singleton
{
    static Singleton instance;
 
    public static Singleton Instance {
        get {
            if (instance == null) {
                instance = new Singleton ();
            }
            return instance;
        }
    }
}


2、unity单例模式二:

但是unity的所有脚本都必须挂在一个gameobject上,否则无法执行,你这个单例中若只是一些数据,那倒没关系,但我相信绝大多数单例模式都不 会只包含数据,若只要实现包含数据的功能,用全局静态变量就行了,说到这里加一句,有些盆友喜欢用单例脚本当做全局脚本来用,那其实是违背单例模式的初衷 的...

好,我们来实现以下挂到gameobject的单例(模式二):

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;
        }
    }
}


3、unity单例模式三:

那如果我的游戏里有很多单例脚本,每个脚本都这么写岂不是很麻烦?岂不是很违背oo思想?^_^,那我们来设计一个泛型类:

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;
        }
    }
}


这样一来,场景中需要单例化的脚本只要简单的继承这个类就可以了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: