Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by simgel · Apr 14, 2018 at 09:08 PM · serializationtilemaptilesserializedproperty

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 ();
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

137 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't see tiles after Instantiate() a tilemap 1 Answer

SerializableProperty for a gameObject inside an array of a custom class inside of a ScriptableObject not setting. 0 Answers

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges