Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
2
Question by Guennor · Mar 29, 2019 at 09:37 PM · tilemaptiletiles

How do I add neighbor rules for RuleTiles by script?

Not much hope for this but here we go.

So far I managed to successfully create a rule tile by script, add rules to it by script, and set the sprite for each of those rules also by script.

Here's the test code i'm making. It has a default sprite and 3 rules, each with its own sprite.

 public static Sprite spriteDefault;
 public static Sprite sprite1;
 public static Sprite sprite2;
 public static Sprite sprite3;
 
 static void CreateRuleTile()
 {
     RuleTile ruleTile = ScriptableObject.CreateInstance("RuleTile") as RuleTile;
 
     AssetDatabase.CreateAsset(ruleTile, "Assets/" + UnityEngine.Random.Range(0, 999).ToString() + "MyRuleTile.asset");
 
     Debug.Log(AssetDatabase.GetAssetPath(ruleTile));
     ruleTile.m_DefaultSprite = spriteDefault;
 
     RuleTile.TilingRule rule01 = new RuleTile.TilingRule();
     rule01.m_Sprites[0] = sprite1;
 
     RuleTile.TilingRule rule02 = new RuleTile.TilingRule();
     rule02.m_Sprites[0] = sprite2;
 
     RuleTile.TilingRule rule03 = new RuleTile.TilingRule();
     rule03.m_Sprites[0] = sprite3;
 
 
     ruleTile.m_TilingRules.Add(rule01);
     ruleTile.m_TilingRules.Add(rule02);
     ruleTile.m_TilingRules.Add(rule03);
 
 }

This create a nice little rule tile for me: alt text

Now i'm trying to add the neighbor rules (the 8 little arrows we can set manually on the editor) by script but I have no idea how to do it.

Each rule has a property called m_Neighbors and when I hover over it it says RuleTile.TilingRule.Neighbor[] RuleTile.TilingRule.m_Neighbors

How can I create custom neighbor rules to add to each of my rules?

ruletile.png (38.8 kB)
Comment
Add comment · Show 5
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
avatar image metalted · Mar 16, 2020 at 10:08 PM 0
Share

Didnt notice this question is almost a year old. If you found something on the subject, maybe you could share it?

avatar image Guennor metalted · Mar 17, 2020 at 01:16 PM 0
Share

Check my answer to the other comment

avatar image Guennor metalted · Aug 11, 2021 at 07:46 PM 0
Share

Oh my goodness. Yet another year later, I just realized you meant that you wanted the solution to setting up neighbor rules. I'm very sorry, I'll share my findings here in case anyone in the future is curious about this.

First you declare or reference your rule tile.

 RuleTile rTile = ScriptableObject.CreateInstance("RuleTile") as RuleTile;

Then you create each rule by declaring them.

 RuleTile.TilingRule myRuleA = new RuleTile.TilingRule();
 myRuleA.m_Sprites[0] = spriteList[33];//What sprite is assigned to this rule?

 int[] myRuleArray = new int[8];
 myRuleArray[0] = 2;//TopLeft
 myRuleArray[1] = 1;//Top
 myRuleArray[2] = 2;//TopRight
 myRuleArray[3] = 1;//Left
 myRuleArray[4] = 1;//Right
 myRuleArray[5] = 2;//BottomLeft
 myRuleArray[6] = 1;//Bottom
 myRuleArray[7] = 1;//BottomRight

The array of rules can be more easily visualized if you put it like this:

 2,  1,  2
 1,      1,
 2,  1,  1,

Where each number represents a position in the rules. The number sets what type of rule it is:

1 = GREEN CHECKMARK

2 = RED X

0 = EMPTY

alt text

Then finally, you assign your rule to the rule tile you declared or created:

 rTile.m_TilingRules.Add(myRuleA);
avatar image paul-masri-stone · Mar 16, 2020 at 11:36 PM 0
Share

$$anonymous$$y thought exactly - use a script rather than spending ages adding individual rules. Did you find a solution?

avatar image Guennor paul-masri-stone · Mar 17, 2020 at 01:16 PM 2
Share

https://assetstore.unity.com/packages/tools/sprite-management/tilegen-tileset-generator-automatic-rule-tiles-149809

This! Done by me after I figured it out :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by metalted · Mar 16, 2020 at 10:06 PM

After some searching in the code i found that neighbors can be set using 2 functions in the TilingRule class. This little snippet will just set all the neighbors to one single type.

     Dictionary<Vector3Int, int> dict = rule01.GetNeighbors();
     List<Vector3Int> neighbors = rule01.m_NeighborPositions;
     for(int i = 0; i < neighbors.Count; i++)
     {
         dict.Add(neighbors[i], RuleTile.TilingRuleOutput.Neighbor.This);
     }
     rule01.ApplyNeighbors(dict);

And this is the class that can be found in RuleTile.cs:

         [Serializable]
         public class TilingRule : TilingRuleOutput
         {
             /// <summary>
             /// The matching Rule conditions for each of its neighboring Tiles.
             /// </summary>
             public List<int> m_Neighbors = new List<int>();
             /// <summary>
             /// * Preset this list to RuleTile backward compatible, but not support for HexagonalRuleTile backward compatible.
             /// </summary>
             public List<Vector3Int> m_NeighborPositions = new List<Vector3Int>()
             {
                 new Vector3Int(-1, 1, 0),
                 new Vector3Int(0, 1, 0),
                 new Vector3Int(1, 1, 0),
                 new Vector3Int(-1, 0, 0),
                 new Vector3Int(1, 0, 0),
                 new Vector3Int(-1, -1, 0),
                 new Vector3Int(0, -1, 0),
                 new Vector3Int(1, -1, 0),
             };
             /// <summary>
             /// The transform matching Rule for this Rule.
             /// </summary>
             public Transform m_RuleTransform;
 
             public Dictionary<Vector3Int, int> GetNeighbors()
             {
                 Dictionary<Vector3Int, int> dict = new Dictionary<Vector3Int, int>();
 
                 for (int i = 0; i < m_Neighbors.Count && i < m_NeighborPositions.Count; i++)
                     dict.Add(m_NeighborPositions[i], m_Neighbors[i]);
 
                 return dict;
             }
 
             public void ApplyNeighbors(Dictionary<Vector3Int, int> dict)
             {
                 m_NeighborPositions = dict.Keys.ToList();
                 m_Neighbors = dict.Values.ToList();
             }
 
             public BoundsInt GetBounds()
             {
                 BoundsInt bounds = new BoundsInt(Vector3Int.zero, Vector3Int.one);
                 foreach (var neighbor in GetNeighbors())
                 {
                     bounds.xMin = Mathf.Min(bounds.xMin, neighbor.Key.x);
                     bounds.yMin = Mathf.Min(bounds.yMin, neighbor.Key.y);
                     bounds.xMax = Mathf.Max(bounds.xMax, neighbor.Key.x + 1);
                     bounds.yMax = Mathf.Max(bounds.yMax, neighbor.Key.y + 1);
                 }
                 return bounds;
             }
         }




Comment
Add comment · Share
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

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

104 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

Related Questions

Tilemaps making feature 1 Answer

Can you add sprites to existing sprite sheet without breaking the tile pallet and in-game tiles 0 Answers

Spawning a specific Tile in a random position a certain distance from the player character 1 Answer

I can‘t assign a folder to my tile palette,I cannot assign a folder to my tile palette 0 Answers

Unity 2D TileMap Rule Tile Carpet 1 Answer


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