当前位置:网站首页>Understanding and use of unity2d custom scriptable tiles (II) -- understand the methods of the tilebase class (mainly gettiledata method) in detail by understanding the tile class
Understanding and use of unity2d custom scriptable tiles (II) -- understand the methods of the tilebase class (mainly gettiledata method) in detail by understanding the tile class
2022-07-21 04:04:00 【Regeneration of see】
This article uses Unity Version is Unity2018.4.36f1, The scripting language is C#
introduction :
In the last article , A brief introduction to TileBase Class and its methods ,TileBase As all customizations tile Parent class of , Its own method has no content ( Only one empty implementation is provided ), If we write it ourselves tile If you inherit it , You also need to write some code to realize the most basic Tile function ( Even the normal display in Palette and Scene You need to write it yourself ), So in order not to repeat some of the most basic functions , We wrote it ourselves tile It is better to inherit Tile class ( This class is also officially provided ), because Tile Class has helped us realize the most basic tile function ( Of course, if your needs are really special , For example, there are some basic functions that do not need , It doesn't have to inherit from Tile class ). So this article Let's focus on Tile class , Study its source code , It can help us better understand the specific uses of various methods .
Text :
Tile class :
Unity English document introduction :
The Tile
class is a simple class that allows a sprite to be rendered on the Tilemap
. Tile inherits from TileBase
.
Unity Introduction to Chinese documents :
Tile
Class is a simple class that allows rendering sprites on tile maps .Tile Inherited from TileBase
.
Unity Scripting API Introduce :
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.
translate : This class is Tilemap Default tiles used in . It is inherited from TileBase And act as placed on Tilemap Default tile in , It rewrites TileBase Of GetTileData Method , In order to realize the tilemap Simple image rendering in .
It's still Unity Scriptable API The introduction is more detailed . We can see ,Tile yes TileBase Subclasses of , And rewritten TileBase Some of the ways ( In fact, there is only one ), So that those methods are no longer just empty methods .
Detailed introduction :
One 、 Member variables :
Tile Like 6 Member variables , Look at the following code :
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: The type of this property is Spirte type , Its function is to store the picture of the tile .
2.color: The type of this property is Color type , Its function is to store the color of the tile .
3.transform: The type of this property is Matrix4x4, I don't understand its function , It should be used for matrix transformation of the coordinates of tiles , This is of little use ( I didn't find any use for tiles ).
4.gameobject: The type of this property is GameObject, Its function is to store a game object attached to the tile ( It will be explained in detail later ).
5.flags: The type of this property is TileFlags, Its function is to control Tile Some behaviors of , For example, whether the color can be changed in the editor, and so on . Here is TileFlags Type of introduction :
TileFlags
Description
Flags controlling behavior for the TileBase. Flags Controlled TileBase act .
Properties
None | No TileFlags are set. There's no setup for TileFlags. |
LockColor | TileBase locks any color set by brushes or the user. Tiles don't let brushes or users change colors . |
LockTransform | TileBase locks any transform matrix set by brushes or the user. Tiles don't let brushes or users change transform attribute . |
InstantiateGameObjectRuntimeOnly | TileBase does not instantiate its associated GameObject in editor mode and instantiates it only during Play mode. Tiles will not instantiate the game object related to them in editor mode, but only instantiate them in game mode . |
KeepGameObjectRuntimeOnly | Keeps the TileBase's associated GameObject in Play mode when replaced with another TileBase or erased In game mode , When tiles are replaced or deleted , Still retain the game object associated with it . |
LockAll | All lock flags. All of the above are needed . |
This type is enumeration type , When setting the attribute value, you should set it according to the setting method of enumeration type .
6.colliderType: The type of this property is ColliderType, His role is to specify Tile Collider type . Here is ColliderType Detailed introduction :
ColliderType
Description
Enum for determining what collider shape is generated for this Tile by the TilemapCollider2D.
This type is an enumeration type , Based on TilemapCollider2D Tile collider shape generation style .
Properties
None | No collider shape is generated for the Tile by the TilemapCollider2D. Collider that will not form any shape . |
Sprite | The Sprite outline is used as the collider shape for the Tile by the TilemapCollider2D. The shape of the impactor depends on the Sprite Contour to generate . |
Grid | The grid layout boundary outline is used as the collider shape for the Tile by the TilemapCollider2D. The shape of the collider is generated according to the frame of the lattice . |
The above is the introduction of all attributes .
The following is the introduction of the method .
Two 、 Method :
All of the following methods are either fully inherited TileBase Class method , Or it is rewritten TileBase Methods , No new method . Methods are called at exactly the same time .
1.RefreshTile Method :
public void RefreshTile(Vector3Int location, ITilemap tilemap)
This method is simply inherited TileBase, The call timing is exactly the same .
2.GetTileData Method :
The call timing is consistent with that of the parent class . This method is right TileBase Of GetTileData Method rewrite , Let's take a detailed look at the content of this method .
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;
}
We know from the code , It will Tile Each member of the class is assigned to a parameter tileData The members of , This TileData It's actually a structure , Here are TileData Introduction to :
TileData
Implemented in:UnityEngine.TilemapModule
Description
A Struct for the required data for rendering a Tile. This is a structure , Store the information needed to build tiles .
Properties
color | Color of the Tile. The color of the tiles |
flags | TileFlags of the Tile. Tiled TileFlags |
gameObject | GameObject of the Tile. Tile attached GameObject |
sprite | Sprite to be rendered at the Tile. Pictures of tiles |
transform | Transform matrix of the Tile. Position matrix of tiles |
We can find this TileData Structures are designed to store the information needed to build tiles .
GetTileData Methods will Tile The member variable of the class is assigned to the parameter tileData Member variables in , It is equivalent to building a structure full of construction information , because tileData Parameters have ref Keyword modification , therefore tileData It can be regarded as a return value and passed to the whole system , Then the system gets the information about how to build tiles at a certain location , You can work normally .
3.GetTileAnimationData Method :
public bool GetTileAnimationData(Vector3Int location, ITilemap tilemap, ref TileAnimationData tileAnimationData)
This method is simply inherited TileBase Of GetTileAnimationData Method , Still an empty method , The call timing is exactly the same .
Let's take a look at TileAnimationData What is the type :
public struct TileAnimationData
{
public Sprite[] animatedSprites { get; set; }
public float animationSpeed { get; set; }
public float animationStartTime { get; set; }
}
We can see that this structure has three properties .
The first is Sprite[] Array type animatedSprites, Used to store the images that make up the animation .
The second is Float Type of animationSpeed, Used to confirm the playback speed of animation .
The third is Float Type of animationStartTime, Used to determine the delay time of animation broadcast .
4.StartUp Method :
public bool StartUp(Vector3Int location, ITilemap tilemap, GameObject go)
This method is simply inherited TileBase Of StartUp Method , Still an empty method , The call timing is exactly the same .
There's nothing to say .
good , Then we already know Tile Class structure ,Tile Class mainly adds some public member variables to store the construction information of tiles , And then in GetTileData() Method to pass the values of these member variables into the reference parameters tileData in .
In the next article, I will start to build a system based on Tile Class customization tile, Explain in detail the methods that have not been discussed in detail .
Link to the next article :
边栏推荐
- 结合源码看《我所理解的cocos2dx-3.0》—— 概述
- 聪明人的游戏提高篇:第三章第二课:因子个数(dcount)
- 解读最新ECCV 2022工作:组合式扩散模型
- 学习记录十三
- 学习记录十
- Model graduation thesis on traffic safety management
- eslint报错
- UGUI——Text和TextEffect
- Uni app - solution to the failure of the return key of the mobile phone with the embedded web page physics in the app platform (after the embedded WebView web page H5 is packaged, the mobile phone's p
- canvas记录
猜你喜欢
把项目发布网上,不想开端口
vsCode配置Eslint+Prettier结合使用详细配置步骤,规范化开发
Chapter006 LCD display of FPGA learning
实验四 CTF实践
uni-app - 插件[App云打包]安装失败!(app打包时显示app云打包插件安装失败)解决方案
基于STM32CubeMX的片外SPIFLASH嵌入FATFS
SourceTree对代码版本管理的操作流程及故障处理(不定时更新)
Chapter005 serial port return of FPGA learning
解决错误:Error: TomEE required to support EAR/EJB deployment
Uni app - plugin [app cloud packaging] installation failed! (the app cloud packaging plug-in installation fails when the app is packaged) solution
随机推荐
把项目发布网上,不想开端口
启新聚势 云谱新篇|海泰方圆与四川联通达成生态战略合作
cocos2dx 引擎中的一些技巧
回到原点:重新开始学习
结合源码看《我所理解的cocos2dx-3.0》—— 概述
Ask me if there is a better way to do database migration, whole database migration, from MySQL (old) - & gt; MySQL (New)?
针对嵌入式设备外接设备的驱动开发心得
【深度学习】使用yolov5对数据进行预标注
hello csdn
MiNi简约版小程序送上,专栏用户独享
香蕉派 BPI-M5折腾记录(2)—— 编译u-boot
Please tell me about flink1.15 flinksql Kafka topic. If you don't create a submission job in advance, it will fail
Chapter001-FPGA学习之Vivado的LED闪烁
HDF5 library version mismatched error的解决方案
Clickhouse failed to start_ Unit clickhouse-server. service entered failed state
解决错误:Error: TomEE required to support EAR/EJB deployment
结合源码看《我所理解的cocos2dx-3.0》—— 粒子系统
三种办法:字符串逆序排列(而非逆序打印)
Stm32cubemx reads and writes USB flash disk through FatFs
Jupyter中markdown的操作技巧