Create a new C# script in Scripts->Editor called
TMXFileInspector.
Create 2 new C# script in Scripts called TMX and TMXMap.
TMXFileInspector is going to be the script that displays the
TMX file in the inspector. The TMX script is going to be responsible for
loading our file. The TMXMap script is going to hold all of the information
about the map that was created in Tiled.
TMXMap.cs
using System.Xml.Serialization;
namespace TileMapXML
{
///
/// TMXMap.cs
///
/// Tiled map file saved as a *.tmx or *.xml
///
/// As per the Tiled Documentation
http://doc.mapeditor.org/reference/tmx-map-format/
/// The TMX (Tile Map XML) map format used by Tiled is a
flexible way to describe a tile based map. It can describe maps with any tile
size, any amount of layers, any number of tile sets and it allows custom
properties to be set on most elements. Beside tile layers, it can also contain
groups of objects that can be placed freely.
///
/// Attributes
/// • version: The
TMX format version, generally 1.0.
/// • orientation:
Map orientation. Tiled supports "orthogonal", "isometric",
"staggered" (since 0.9) and "hexagonal" (since 0.11).
/// • renderorder:
The order in which tiles on tile layers are rendered. Valid values are
right-down(the default), right-up, left-down and left-up.In all cases, the map
is drawn row-by-row. (since 0.10, but only supported for orthogonal maps at the
moment)
/// • width: The map
width in tiles.
/// • height: The
map height in tiles.
/// • tilewidth: The
width of a tile.
/// • tileheight:
The height of a tile.
/// • hexsidelength:
Only for hexagonal maps. Determines the width or height (depending on the
staggered axis) of the tile's edge, in pixels.
/// • staggeraxis:
For staggered and hexagonal maps, determines which axis("x" or
"y") is staggered. (since 0.11)
/// • staggerindex:
For staggered and hexagonal maps, determines whether the "even" or
"odd" indexes along the staggered axis are shifted. (since 0.11)
/// • backgroundcolor:
The background color of the map. (since 0.9, optional, may include alpha value
since 0.15 in the form #AARRGGBB)
/// • nextobjectid:
Stores the next available ID for new objects.This number is stored to prevent
reuse of the same ID after objects have been removed. (since 0.11)
///
/// The tilewidth and tileheight properties determine the
general grid size of the map. The individual tiles may have different sizes.
Larger tiles will extend at the top and right (anchored to the bottom left).
///
/// Can contain: properties, tileset, layer, objectgroup,
imagelayer
///
[XmlRoot("map")]
public class TMXMap
{
#region Attributes
#endregion
}//public
class TMXMap
}//namespace TileMapXML
We are using xml serialization to load a map from a file so
we need the using System.Xml.Serialization;
[XmlRoot("map")] This is telling the xml
serializer that the class is going to be the root element in our xml file and
the name of the element is “map”. We will be filling in the rest of this class
as we go.
TMX.cs
using System.Xml.Serialization;
using System.IO;
namespace TileMapXML
{
public class TMX
{
public TMXMap map;
///
/// Loads A TMX file from xml using serialization
///
/// tmxFilePath">The path of the file to load
public void Load (string tmxFilePath)
{
// Load
the map from the xml file at tmxFilePath
XmlSerializer serializer = new XmlSerializer(typeof(TMXMap));
using(FileStream stream = new FileStream(tmxFilePath, FileMode.Open))
map =
serializer.Deserialize(stream) as TMXMap;
}//void
Load
}//public
class TMX
}//namespace TileMapXML
This class we have our TMXmap variable called map and a
method to load the map. The Load method takes in a file to load and uses the
xml serializer to load that file into our map variable.
TMXFileInspector.cs
using UnityEditor;
using UnityEngine;
namespace TileMapXML
{
///
/// An editor for TMX files.
///
/// Overrides the DefaultAsset, Cannot create a Type for tmx
files in Unity
///
/// Can change the way an Inspector looks based on the file
name extension
///
[CustomEditor(typeof(DefaultAsset))]
public class TMXFileInspector : Editor
{
///
/// A reference to the asset
///
DefaultAsset m_asset;
///
/// A reference to the path of the asset
///
string m_assetPath;
///
/// This is the file itself that will be loaded using xml
serialization.
///
TMX m_tmx;
///
/// This is the function that Unity uses to display in the
inspector.
///
public override void OnInspectorGUI()
{
// Get a
reference to the target as a DefaultAsset;
m_asset = target as DefaultAsset;
// Get
the path of the asset
m_assetPath = AssetDatabase.GetAssetPath(m_asset);
// Draw
the default Inspector
DrawDefaultInspector();
// If
the asset is not a tmx file no need to do anything so return
if(!m_assetPath.EndsWith(".tmx", System.StringComparison.OrdinalIgnoreCase) || m_asset == null)
return;
// if
the file has not been loaded then load it
if(m_tmx == null)
{
//
Create a new tmx file in memory
m_tmx = new TMX();
//
Load the file
m_tmx.Load(m_assetPath);
//
print a message to the log that has the asset path
Debug.Log(m_assetPath);
}
// Get a
reference to whether the GUI is enabled or not
bool enabled = GUI.enabled;
// Set
the GUI to be enabled
GUI.enabled = true;
TMXOnInspectorGUI();
// Set
the GUI enabled back to what it was
GUI.enabled = enabled;
}//override
void OnInspectorGUI()
void TMXOnInspectorGUI()
{
EditorGUILayout.LabelField("This
is where we are going to display the information about the map");
}
}//class
TMXFileInspector : Editor
}//namespace TileMapXML
We tell unity that this is a Custom Editor for a Default
asset. And that we wat to override the Inspector GUI. In the inspector GUI method we get the asset
that we are inspecting, the target. Then
we get the asset path. We then tell Unity to draw the Default Inspector. Next
we check to see if we have a tmx file selected. If not then we just return.
Then we check to see if the TMX m_tmx is null, this will only be null if we
select a deferent TMX file, we do this so that we only load the tmx file once
instead of every time the inspector loops. Then we tell that we want to run our
own Inspector GUI method call TMXOnInspectorGUI. Right now the
TMXOnInspectorGUI method just displays a label telling us that this is where we
are going to display the information about the map. We will be coming back to
this method later once we finish with the TMX file format.
No comments:
Post a Comment