unity学习笔记--day01

这篇具有很好参考价值的文章主要介绍了unity学习笔记--day01。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

今天学习制作了一个简单的抽卡功能,学习结束后目录结构如下:
unity抽卡系统,unity,unity,学习,游戏引擎
.mate文件是unity生成的配置文件,不用管

制作第一张卡片

  1. 创建一个空物体,改名为Card。
  1. 在Card下挂载以下UI组件:
    unity抽卡系统,unity,unity,学习,游戏引擎
  2. 编写数据脚本并挂载,unity采用c#作为脚本语言。

3-1. 首先定义一个卡牌类,定义卡牌上通用的属性

public class Card {
    // 卡牌id
    public int id;
    // 卡牌名称
    public string cardName;
    // 卡牌描述
    public string direction;

    // 构造函数
    public Card(int _id, string _cardName, string _direction){
        this.id = _id;
        this.cardName = _cardName;
        this.direction = _direction;
    }
}

3-2. 定义一个怪物卡类,继承自卡牌类,添加独有的攻击力,最大生命和当前生命。同时定义一个魔法卡类,后续有魔法卡的专有属性时方便添加。

public class MonsterCard: Card {
    // 攻击力
    public int attack;
    // 当前生命值
    public int healthPoint;
    // 最大生命值
    public int healthPointMax;
    // 使用base调用父类的构造函数
    public MonsterCard(int _id, string _cardName, string _direction,int _attack, int _healthPointMax):base(_id, _cardName, _direction)
    {
        this.attack = _attack;
        this.healthPointMax = _healthPointMax;
        this.healthPoint = _healthPointMax;
    }

}

public class SpellCard: Card {
    // public string effect;

    public SpellCard(int _id, string _cardName, string _direction):base(_id, _cardName, _direction)
    {
        // this.effect = _effect;
    }
}
  1. 将数据类与视图想关联

定义一个脚本用于呈现数据,命名为CardDisPlay。
在类中定义相关的UI属性,并用Card中的数据对UI属性进行赋值。

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

public class CardDisplay : MonoBehaviour
{
    // UI相关的属性
    public Text nameText;
    public Text attackText;
    public Text healthText;
    // public Text effectText;
    public Text directionText;

    public Image backgroundImage;

    public Card card;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void ShowCard(){
        nameText.text = card.cardName;
        directionText.text = card.direction;
        if (card is MonsterCard)
        {
            var monster = card as MonsterCard;
            attackText.text = monster.attack.ToString();
            healthText.text = monster.healthPoint.ToString();

            // effectText.gameObject.SetActive(false);
        }
        else if (card is SpellCard)
        {
            var spell = card as SpellCard;
            // effectText.text = spell.effect;

            attackText.gameObject.SetActive(false);
            healthText.gameObject.SetActive(false);
        }
    }
}

  1. 将定义好的脚本组件挂载道卡片上,并将组件的属性与对应的UI关联。
    unity抽卡系统,unity,unity,学习,游戏引擎
  1. 将做好的卡片保存为预制体。

抽卡场景

场景

unity通过划分场景来对游戏的页面进行划分,场景文件的后缀名为.sence

卡牌数据读取

定义一个空物体命名为CardStore,并定义一个名为CardStore的脚本文件,用来读取卡牌数据。

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

public class CardStore : MonoBehaviour
{
    // 定义一个文本资源
    public TextAsset cardDate;
    // 定义链表,跟数组对比的好处是它不限制数量,添加和删除十分方便
    public List<Card> cardList = new List<Card>();
    // Start is called before the first frame update
    void Start()
    {
        LoadCardDate();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void LoadCardDate(){
        string[] dateRow = cardDate.text.Split('\n');
        foreach (var row in dateRow)
        {
            string[] rowArray = row.Split(',');
            if(rowArray[0] == "#"){
                continue;
            }
            else if(rowArray[0] == "monster"){
                // 新建怪兽卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                string direction = rowArray[3];
                int atk = int.Parse(rowArray[4]);
                int health  = int.Parse(rowArray[5]);

                MonsterCard monsterCard = new MonsterCard(id,name,direction,atk,health);
                cardList.Add(monsterCard);

                // Debug.Log("读取到怪兽卡:" + monsterCard.cardName);
            }
            else if(rowArray[0] == "spell"){
                // 新建魔法卡
                int id = int.Parse(rowArray[1]);
                string name = rowArray[2];
                string direction = rowArray[3];
                string effect = rowArray[4];
                SpellCard spellcard = new SpellCard(id,name,direction);
                cardList.Add(spellcard);

                // Debug.Log("读取到魔法卡:" + spellcard.cardName);
            }
        }
    }
	// 返回数据中的随机一张卡,后面抽卡时后用到
    public Card RandomCard(){
        Card card = cardList[Random.Range(0,cardList.Count)];
        return card;
    }
}

将脚本挂载到CardStore这个物体上,并将文本数据关联起来,就可以进行读取。
unity抽卡系统,unity,unity,学习,游戏引擎

卡池

定义一个空物体并添加网格布局组件,用于展示抽到的卡。
unity抽卡系统,unity,unity,学习,游戏引擎

抽卡脚本

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

public class OpenPackage : MonoBehaviour
{
    public GameObject cardPrefeb;

    CardStore CardStore;

    public GameObject cardpool;
    // Start is called before the first frame update
    void Start()
    {
        CardStore = GetComponent<CardStore>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void OnClickOpen(){
        for (int j = 0; j < cardpool.transform.childCount; j++) {
			Destroy (cardpool.transform.GetChild(j).gameObject);
		}
        for(int i = 0; i < 5; i++){
            

            // 使用预制体生成一个游戏对象
            GameObject newCard = GameObject.Instantiate(cardPrefeb, cardpool.transform);
            // 在这个预制体上挂载一个卡片属性,用来指代这张卡本身
            // GetComponent这个函数用来获取当前物体上的其他组件
            newCard.GetComponent<CardDisplay>().card = CardStore.RandomCard();
            newCard.GetComponent<CardDisplay>().ShowCard();
            // Debug.Log($"执行了");
        }
    }
}

按钮组件

按钮组件也是UI组件的一种,可以选择编写的组件中的方法作为点击事件。
unity抽卡系统,unity,unity,学习,游戏引擎
将相关的物体关联好,这样在点击抽卡按钮时,就会随机在页面上展示5张卡片。文章来源地址https://www.toymoban.com/news/detail-521339.html

到了这里,关于unity学习笔记--day01的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Unity3D学习记录01:URP渲染管线以及3D游戏场景设置

    以下内容所使用的版本均为Unity2022.3 先在 Window-Package Manager-Unity Registry 里面搜索添加Universal RP   Unity中,创建渲染管线的方式为Asset文件夹下右键 Create-Readering-URP Asset(with Universal Asset) 会创建以下两个Pipeline:  接着在图中的设置里添加这两个渲染管线(Project Setting在Edit窗口下

    2024年02月08日
    浏览(39)
  • Games104现代游戏引擎笔记 面向数据编程与任务系统

    核达到了上限,无法越做越快,只能通过更多的核来解决问题 Process 进程 有独立的存储单元,系统去管理,需要通过特殊机制去交换信息 Thread 线程 在进程之内,共享了内存。线程之间会分享很多内存,这些内存就是数据交换的通道。 管理Tasking的方法 Preemptive Multitasking 抢占

    2024年02月04日
    浏览(49)
  • 游戏引擎架构01__引擎架构图

    根据游戏引擎架构预设的引擎架构来构建运行时引擎架构 ​

    2024年04月09日
    浏览(29)
  • 【学习笔记】自学Unity Day02

    前言:主要想记录下自己自学的过程、期间遇见的问题、不同版本需要调整的地方,方便以后复习能及时找到对应的部分;同时也希望给想要入门游戏开发、学习unity的各位一些经验,减少一些弯路 学习资料目前主要依靠 unity 官网,我的想法是先根据官方的游戏套件能够做出

    2024年04月17日
    浏览(18)
  • Unity3D学习笔记——物理引擎

    1简介 刚体可以为游戏对象赋予物理特性,是游戏对象在物理系统的控制下接受推力和扭力,从而实现现实世界的物理学现象。 2属性 1简介 碰撞器是物理组件的一类,他与刚体一起促使碰撞发生 碰撞体是简单形状,如方块、球形或者胶囊形,在 Unity 3D 中每当一个 GameObjects

    2023年04月12日
    浏览(39)
  • unity学习笔记----游戏练习06

    一、豌豆射手的子弹控制 创建脚本单独控制子弹的运动 用transform来控制移动     void Update()     {         transform.Translate(Vector3.right * speed * Time.deltaTime);     } 创建一个控制子弹速度的方法,方便速度的控制 private void SetSpeed(float speet)     {         this.speed = speet;     } 回到

    2024年01月25日
    浏览(30)
  • unity学习笔记----游戏练习05

    一、阳光的收集和搜集动画开发 1.收集阳光的思路:当鼠标点击到阳光的时候,就可以进行收集了。可以通过为添加一个碰撞器来检测Circle Collider 2D 编写脚本: 在SunManager中写一个增加阳光的方法     //增加阳光     public void AddSubSun(int Point)     {         sunPoint += Point;  

    2024年02月20日
    浏览(25)
  • stm32f407VET6 系统学习 day01 GPIO 配置

    GPIO,即通用I/O(输入/输出)端口,是STM32可控制的引脚。STM32芯片的GPIO引脚与外部设备连接起来,可实现与外部通讯、控制外部硬件或者采集外部硬件数据的功能。 STM32F407有7组IO。分别为GPIOA~GPIOG,每组IO有16个IO口,共有112个IO口  通常称为 PAx、PBx、PCx、PDx、PEx、PFx、PGx,其中

    2023年04月09日
    浏览(38)
  • Unity学习笔记[一] RollBall小游戏

    目录 一、适配vs 二、初识Unity 2.1 unity核心模块 2.2 Unity基本操作和场景操作 2.3 世界坐标系和局部坐标系 2.4 工具栏 QWER 三、基础知识 3.1 基本组件 3.2 刚体组件 3.2.1 获取刚体组件 3.2.2 给刚体施加力 3.3 三维向量Vector3 3.4 通过按键控制左右运动 3.5 控制相机位置和跟随 3.6 物体

    2023年04月09日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包