How does the RuleTile from Unitys 2D-extras work?
I have watched this tutorial where they are using the RuleTile from the "2D-extras" repository (link).
To learn how this stuff works, I started to implement this RuleTile on my own. Everything is fine until I close the Unity Editor and reopen it. All the changes I had made on the RuleTile were gone.
After spending some hours searching on the internet I got some of the features to work by using SerializedProperties in the custom editor instead of accessing the properties directly.
Does the RuleTile/RuleTileEditor work with the current version of unity (2017.4)?
Do I have to mess around with the SerializedProperty/SerializedObejct stuff to get this running?
Is there any other solution to place tiles automatically in a similar way?
Some Code Fragments from the 2D Extra RuleTile
[Serializable]
[CreateAssetMenu]
public class RuleTile : TileBase
{
[Serializable]
public class TilingRule
{
...
}
public List<TilingRule> m_TilingRules;
public Sprite m_DefaultSprite;
public Tile.ColliderType m_DefaultColliderType = Tile.ColliderType.Sprite;
...
}
[CustomEditor(typeof(RuleTile))]
[CanEditMultipleObjects]
internal class RuleTileEditor : Editor
{
private ReorderableList m_ReorderableList;
public RuleTile tile { get { return (target as RuleTile); } }
public void OnEnable()
{
if (tile.m_TilingRules == null)
tile.m_TilingRules = new List<RuleTile.TilingRule>();
m_ReorderableList = new ReorderableList(tile.m_TilingRules, typeof(RuleTile.TilingRule), true, true, true, true);
...
}
public override void OnInspectorGUI()
{
tile.m_DefaultSprite = EditorGUILayout.ObjectField("Default Sprite", tile.m_DefaultSprite, typeof(Sprite), false) as Sprite;
tile.m_DefaultColliderType = (Tile.ColliderType)EditorGUILayout.EnumPopup("Default Collider", tile.m_DefaultColliderType);
EditorGUILayout.Space();
if (m_ReorderableList != null && tile.m_TilingRules != null)
m_ReorderableList.DoLayoutList();
}
}
My modifications
[Serializable]
public class RuleTile : TileBase {
[SerializeField]
private List<TilingRule> tilingRules;
[SerializeField]
private Sprite defaultSprite;
[SerializeField]
private Tile.ColliderType defaultCollider;
}
[CustomEditor(typeof(RuleTile))]
public class RuleTileEditor : Editor {
protected RuleTile tile {get { return serializedObject.targetObject as RuleTile; }}
//Serialization
private SerializedProperty defaultColliderProperty;
public void OnEnable(){
//Properties
defaultColliderProperty = serializedObject.FindProperty("defaultCollider");
...
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
//deserialize values
Tile.ColliderType collider = (Tile.ColliderType)defaultColliderProperty.enumValueIndex;
//Layout GUI
collider = (Tile.ColliderType) EditorGUILayout.EnumPopup (colliderTypeLabel, collider);
//serialize values
defaultColliderProperty.enumValueIndex = (int)collider;
serializedObject.ApplyModifiedProperties ();
}
}
Your answer
Follow this Question
Related Questions
I can't see tiles after Instantiate() a tilemap 1 Answer
Tilemap tile animation - How to get the current sprite ( or frame ) name with the GetTile command? 0 Answers
I am using the new tilemap feature in unity 2017, how do I store information in a specific tile? 7 Answers
How can I add script to a single tile ? 3 Answers