Unity笔记(0925-0927)

手牌组

新建名为HandCardsImage对象,TempCard的空对象

注意他们都在Canvas

HandCards添加HorizontalLayoutGroup

摆好手牌的位置

鼠标进入放大卡牌

public void MouseEnter () {
    transform.localScale=new Vector3(1.2f,1.2f,1);
}
public void MouseExit () {
    transform.localScale=new Vector3(1,1,1);
}

使放开鼠标可以归回手牌组

public void MouseDrag () {
    transform.SetParent(GameObject.Find("TempCard").transform);
}
public void EndDrag () {
    transform.SetParent(GameObject.Find("HandCards").transform);
}

让手牌增加或减少时依然居中(不是万能公式)

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

using UnityEngine;
using UnityEngine.UI;

public class HandCards : MonoBehaviour {
    private RectTransform rectTransform;
    private HorizontalLayoutGroup layout;
    void Start () {
        rectTransform = transform.GetComponent<RectTransform> ();
        layout = transform.GetComponent<HorizontalLayoutGroup> ();
    }
    void Update () {
        rectTransform.anchoredPosition = new Vector2 (-(200 * (transform.childCount - 1) + layout.spacing * (transform.childCount - 2)) / 2 + 100, -230);
        if (transform.childCount > 5) {
            layout.spacing = -50 - 10 * (transform.childCount - 4);
        }
    }
}

效果

场景切换

首先要把场景加入到构建设置中

摆好布局

StartBtn添加脚本

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

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class StartBtn : MonoBehaviour {
    void Start () {
        this.GetComponent<Button> ().onClick.AddListener (OnClick);
    }
    void Update () {

    }
    void OnClick () {
        SceneManager.LoadScene ("FightScene");
    }
}

效果

洗手牌

原理是改变子物体层级顺序

HandCards.cs

if(Input.GetKeyDown(KeyCode.E)){
	for(int i=0;i<transform.childCount;i++){
		int rand=Random.Range(i,transform.childCount);
		transform.GetChild(rand).SetSiblingIndex(0);
	}
}

效果

血条

新建两个Image,一个红色表示hp,一个灰色表示空血,一个Text显示血量

修改红色血条图片的填充方式

然后给Enemy对象添加Enemy.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Enemy : MonoBehaviour {
    public int maxHp;
    public int hp;
    public int cutHp;
    private Transform hpBar;
    private Transform hpText;
    void Start () {
        hpBar = transform.Find ("HpRed");
        hpText = transform.Find ("HpText");
        hpText.GetComponent<Text> ().text = hp + "/" + maxHp;
    }
    void Update () {
        if (hp > 0) {
            if (Input.GetKeyDown (KeyCode.A)) {
                hp -= cutHp;
                hpBar.GetComponent<Image> ().fillAmount -= (float) cutHp / maxHp;
                hpText.GetComponent<Text> ().text = hp + "/" + maxHp;
            }
        }
    }
}

效果

弹出游戏胜利

制作一个游戏胜利的预制体

Enemy.cs中编写

public GameObject youWin;
void Update () {
    if (hp > 0) {
        if (Input.GetKeyDown (KeyCode.A)) {
            hp -= cutHp;
            hpBar.GetComponent<Image> ().fillAmount -= (float) cutHp / maxHp;
            hpText.GetComponent<Text> ().text = hp + "/" + maxHp;
            if (hp <= 0) {
                Instantiate (youWin);
            }
        }
    }
}

属性中挂载预制体

YouWin添加脚本,点击一次提示消失

void Update () {
    if(Input.GetMouseButtonDown(0)){
        Destroy(gameObject);
    }
}

效果

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页