Unity2D 自定义Scriptable Tiles的理解与使用(二)——通过了解Tile类来对TileBase类的方法(主要是GetTileData方法)进行详细理解

本文章使用的Unity版本为 Unity2018.4.36f1,脚本语言为C#

引言: 

在上篇文章中,粗略介绍了TileBase类以及它的方法,TileBase作为所有自定义tile的父类,本身的方法是没有任何内容的(只提供一个空实现),如果我们自己写的tile继承它的话,还需要自己写一些代码来实现最基本的Tile功能(就连正常地显示在Palette和Scene上都需要自己写),所以为了不重复写一些最基本的功能,我们自己写的tile最好是继承Tile类(这个类也是官方提供的),因为Tile类已经帮我们实现了最基本的瓦片功能(当然如果你的需求实在特殊,例如有一些基本的功能不需要,也不一定要继承自Tile类)。所以这篇文章重点介绍一下Tile类,研究一下它的源代码,可以帮助我们更深入地了解各种方法的具体用途。

正文:

Tile类:

Unity英文文档介绍:

The Tile class is a simple class that allows a sprite to be rendered on the Tilemap
. Tile inherits from TileBase

Unity中文文档介绍:

Tile 类是一个允许在瓦片地图上渲染精灵的简单类。Tile 继承自 TileBase

Unity Scripting API介绍:

Class for a default tile in the Tilemap.This inherits from TileBase and represents a default tile to be placed in a Tilemap. It implements TileBase.GetTileData for simple rendering of a Sprite in the tile map.

翻译:这个类是Tilemap中使用的默认瓦片。它继承自TileBase而且充当放置在Tilemap中的默认瓦片,它重写了TileBase的GetTileData方法,以实现tilemap中的简单图片渲染。

这里仍然是Unity Scriptable API介绍的更为详细。我们可以得知,Tile是TileBase的子类,而且重写了TileBase的一些方法(其实就只有一个),让那些方法不再只是一个空方法。

详细介绍:

一、成员变量:

Tile类有6个成员变量,请看下面代码:

public Sprite sprite;
public Color color = Color.white;
public Matrix4x4 transform = Matrix4x4.identity;
public GameObject gameobject = null;
public TileFlags flags = TileFlags.LockColor;
public ColliderType colliderType = ColliderType.Sprite;

1.sprite:这个属性的类型是Spirte类型,它的作用就是储存该瓦片的图片。

2.color:这个属性的类型是Color类型,它的作用就是储存该瓦片的颜色。

3.transform:这个属性的类型是Matrix4x4,它的作用我没搞明白,应该是用来对瓦片的坐标进行矩阵变换的,这个没什么太大用处(我没发现对瓦片有什么用)。

4.gameobject:这个属性的类型是GameObject,它的作用就是储存该瓦片附加的一个游戏对象(之后会详细说明)。

5.flags:这个属性的类型是TileFlags,它的作用是控制Tile的一些行为,例如能不能在编辑器中被改变颜色等等。下面是TileFlags类型的介绍:

TileFlags

Description

Flags controlling behavior for the TileBase.   Flags控制了TileBase的行为。

Properties

None No TileFlags are set.     没有设置任何 TileFlags。
LockColor TileBase locks any color set by brushes or the user.      瓦片不让笔刷或用户改变颜色。
LockTransform TileBase locks any transform matrix set by brushes or the user.      瓦片不让笔刷或用户改变transform属性。
InstantiateGameObjectRuntimeOnly TileBase does not instantiate its associated GameObject in editor mode and instantiates it only during Play mode.      瓦片在编辑器模式下不会实例化与它相关的游戏对象而只在游戏模式下实例化它。
KeepGameObjectRuntimeOnly Keeps the TileBase's associated GameObject in Play mode when replaced with another TileBase or erased      在游戏模式下,当瓦片被替换或删除时,仍然保留与之相关联的游戏对象。
LockAll All lock flags.      以上所有都需要。

这个类型为枚举类型,设置属性值的时候应按照枚举类型的设置方法来设置。

6.colliderType:这个属性的类型是ColliderType,他的作用是指定Tile的碰撞器类型。下面是ColliderType的详细介绍:

ColliderType

Description

Enum for determining what collider shape is generated for this Tile by the TilemapCollider2D.

该类型为枚举类型,确定了基于TilemapCollider2D的瓦片碰撞器形状的生成样式。

Properties

None No collider shape is generated for the Tile by the TilemapCollider2D.      不会形成任何形状的碰撞器。
Sprite The Sprite outline is used as the collider shape for the Tile by the TilemapCollider2D.      碰撞器的形状根据瓦片的Sprite轮廓来生成。
Grid The grid layout boundary outline is used as the collider shape for the Tile by the TilemapCollider2D.      碰撞器的形状根据格子的边框来生成。


以上是所有属性的介绍。

以下是方法的介绍。

二、方法:

以下的所有方法要么是完全继承了TileBase类的方法,要么是重写了TileBase的方法,没有新的方法。方法的调用时机完全一致。

1.RefreshTile方法:

public void RefreshTile(Vector3Int location, ITilemap tilemap)

这个方法完全就只是继承了TileBase,调用时机完全一致。

2.GetTileData方法:

调用时机和父类的一致。这个方法是对TileBase的GetTileData方法的重写,我们来详细看一下这个方法的内容。

public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
{
    tileData.sprite = this.sprite;
    tileData.color = this.color;
    tileData.transform = this.transform;
    tileData.gameobject = this.gameobject;
    tileData.flags = this.flags;

tileData.colliderType = this.colliderType;
}

从代码里我们得知,它将Tile类的各个成员都赋值给了参数tileData中的各个成员,这个TileData其实是一个结构体,以下是TileData的介绍:

TileData

Implemented in:UnityEngine.TilemapModule

Description

A Struct for the required data for rendering a Tile.      这是一个结构体,储存构建瓦片需要的信息。

Properties

color Color of the Tile.       瓦片的颜色
flags TileFlags of the Tile.      瓦片的TileFlags
gameObject GameObject of the Tile.      瓦片附加的游戏对象
sprite Sprite to be rendered at the Tile.      瓦片的图片
transform Transform matrix of the Tile.      瓦片的位置矩阵

我们可以发现这个TileData结构体是专门储存构建瓦片需要的信息的。

GetTileData方法将Tile类的成员变量赋值给了参数tileData中的各个成员变量,就等于构建了一个装满构建信息的结构体,因为tileData参数有ref关键字修饰,所以tileData可以看作一个返回值传递给整个系统,然后系统就得到了如何构建某个位置的瓦片的信息,就可以正常地工作了。

3.GetTileAnimationData方法:

public bool GetTileAnimationData(Vector3Int location, ITilemap tilemap, ref TileAnimationData tileAnimationData)

这个方法完全就只是继承了TileBase的GetTileAnimationData方法,仍然是个空方法,调用时机完全一致。

我们来看一下参数中的TileAnimationData类型是什么:

 public struct TileAnimationData
    {
        public Sprite[] animatedSprites { get; set; }
        public float animationSpeed { get; set; }
        public float animationStartTime { get; set; }
    }

我们可以看到这个结构体有三个属性。

第一个是Sprite[]数组类型的 animatedSprites,用来存储组成动画的图像。

第二个是Float类型的animationSpeed,用来确认动画的播放速度。

第三个是Float类型的animationStartTime,用来确定动画播出的延迟时间。

4.StartUp方法:

public bool StartUp(Vector3Int location, ITilemap tilemap, GameObject go)

这个方法完全就只是继承了TileBase的StartUp方法,仍然是个空方法,调用时机完全一致。

没什么好说的。

好,那我们已经了解了Tile类的结构,Tile类主要是添加了一些公开的成员变量用于储存瓦片的构建信息,然后在GetTileData()方法中将这些成员变量值传入引用参数tileData中。

下一篇文章我就会开始着手构建一个基于Tile类的自定义tile,把没有详细讲的方法详细地讲一下。

下篇文章链接:(22条消息) Unity2D 自定义Scriptable Tiles的理解与使用(三)——开始着手构建一个基于Tile类的自定义tile(上)_see的再生的博客-CSDN博客

​​​​​​​

猜你喜欢

转载自blog.csdn.net/scombropidae/article/details/125303578
今日推荐