Search Unity

How to edit gameobject's tile with script ?

Discussion in '2D' started by macharius666, Sep 23, 2019.

  1. macharius666

    macharius666

    Joined:
    Sep 23, 2019
    Posts:
    2
    Hi,
    I've a little problem.
    I want to edit the PolygonCollider2D in "instanced game object" by script.

    upload_2019-9-23_22-16-46.png

    upload_2019-9-23_22-19-23.png

    Code (CSharp):
    1. public override void GetTileData(Vector3Int location, ITilemap tilemap, ref TileData tileData)
    2. {
    3.     base.GetTileData(location, tilemap, ref tileData);
    4.     string composition = string.Empty;//Makes an empty string as compostion, we need this so that we change the sprite
    5.     for (int x = -1; x <= 1; x++)//Runs through all neighbours
    6.     {
    7.         for (int y = -1; y <= 1; y++)
    8.         {
    9.             if (x != 0 || y != 0) //Makes sure that we aren't checking our self
    10.             {
    11.                 if (HasThisTile(tilemap, new Vector3Int(location.x + x, location.y + y, location.z)))
    12.                 {
    13.                     composition += 'I';
    14.                 }
    15.                 else
    16.                 {
    17.                     composition += 'O';
    18.                 }
    19.             }
    20.         }
    21.     }
    22.  
    23.     tileData.sprite = holeSprites[0];
    24.  
    25.     List<Vector2> list = new List<Vector2>();
    26.     list.Add(new Vector2(-.5f, -.5f));
    27.     list.Add(new Vector2(-.5f, .5f));
    28.     list.Add(new Vector2(.5f, .5f));
    29.     list.Add(new Vector2(.5f, -.5f));
    30.  
    31.     if (composition[1] == 'O' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'O')
    32.     {
    33.         list.Clear();
    34.         list.Add(new Vector2(-.3f, -.3f));
    35.         list.Add(new Vector2(-.3f, .3f));
    36.         list.Add(new Vector2(.3f, .3f));
    37.         list.Add(new Vector2(.3f, -.3f));
    38.     }
    39.     else if (composition[1] == 'O' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'I')
    40.     {
    41.         list.Clear();
    42.         tileData.sprite = holeSprites[1];
    43.         list.Add(new Vector2(-.3f, -.3f));
    44.         list.Add(new Vector2(-.3f, .3f));
    45.         list.Add(new Vector2(.5f, .3f));
    46.         list.Add(new Vector2(.5f, -.3f));
    47.     }
    48.     else if (composition[1] == 'I' && composition[3] == 'O' && composition[4] == 'O' && composition[6] == 'O')
    49.     {
    50.         list.Clear();
    51.         tileData.sprite = holeSprites[2];
    52.         list.Add(new Vector2(-.5f, -.3f));
    53.         list.Add(new Vector2(-.5f, .3f));
    54.         list.Add(new Vector2(.3f, .3f));
    55.         list.Add(new Vector2(.3f, -.3f));
    56.     }
    57.  
    58.     tileData.gameObject.GetComponent<PolygonCollider2D>().SetPath(0, list);
    59. }
    The sprite's tile work, but all Tiles have the same gameObject clone.
    In this example, i'have 2 tiles, but they have the same PolygonCollider2D.

    upload_2019-9-23_22-52-33.png

    Why ? How can have different gameObject ?

    Thx for your help :)
     
  2. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    You are setting properties of the components on the referenced gameObject, you need to wait for the instance to be created first. Most of this logic should be done in StartUp(). GetTileData should only return data about the tile.

    See the following as an example:
    Code (CSharp):
    1. /*
    2. *   Delve : Scripts.Tiles.Advanced - Dan Flanigan, 2019
    3. */
    4. using General;
    5. using UnityEngine;
    6. using UnityEngine.Serialization;
    7. using UnityEngine.Tilemaps;
    8.  
    9. // ReSharper disable once CheckNamespace
    10. [CreateAssetMenu]
    11. public class AnimatedStaticPrefabTile : AnimatedTile
    12. {
    13.     //public Sprite TileSprite;
    14.     [FormerlySerializedAs("TileAssociatedPrefab")] public GameObject tileAssociatedPrefab;
    15.  
    16.     [FormerlySerializedAs("PrefabLocalZOffset")] public float prefabLocalZOffset;
    17.  
    18.     [FormerlySerializedAs("UseAbsoluteZOffset")] public bool useAbsoluteZOffset;
    19.     [FormerlySerializedAs("PrefabAbsoluteZOffset")] public float prefabAbsoluteZOffset = -1f;
    20.  
    21.     public override bool StartUp(Vector3Int position, ITilemap tilemap, GameObject go)
    22.     {
    23.  
    24.         //This prevents rogue prefab objects from appearing when the TileBase palette is present
    25. #if UNITY_EDITOR
    26.         if (go != null)
    27.         {
    28.             if (go.scene.name == null)
    29.             {
    30.                 DestroyImmediate(go);
    31.             }
    32.         }
    33. #endif
    34.  
    35.         if (go != null)
    36.         {
    37.             if (useAbsoluteZOffset)
    38.             {
    39.                 //Modify position of GO to match middle of TileBase sprite
    40.                 go.transform.position = new Vector3(position.x + Globals.PrefabXyOffset
    41.                     , position.y + Globals.PrefabXyOffset
    42.                     , prefabAbsoluteZOffset);
    43.  
    44.                 return true;
    45.             }
    46.  
    47.             //Modify position of GO to match middle of TileBase sprite
    48.             go.transform.position = new Vector3(position.x + Globals.PrefabXyOffset
    49.                 , position.y + Globals.PrefabXyOffset
    50.                 , position.z);
    51.  
    52.             //Set Z
    53.             var localPosition = go.transform.localPosition;
    54.             localPosition =
    55.                 new Vector3(localPosition.x, localPosition.y, prefabLocalZOffset);
    56.             go.transform.localPosition = localPosition;
    57.         }
    58.  
    59.         return true;
    60.     }
    61.  
    62.     public override void GetTileData(Vector3Int position, ITilemap tilemap, ref TileData tileData)
    63.     {
    64.         tileData.sprite = !Application.isPlaying ? m_AnimatedSprites[0] : null;
    65.  
    66.         if (tileAssociatedPrefab && tileData.gameObject == null)
    67.         {
    68.             tileData.gameObject = tileAssociatedPrefab;
    69.         }
    70.  
    71.         tileData.flags = TileFlags.InstantiateGameObjectRuntimeOnly;
    72.     }
    73. }
     
    Last edited: Sep 24, 2019
  3. macharius666

    macharius666

    Joined:
    Sep 23, 2019
    Posts:
    2
    I'm so stupid :(
    It's work now, thx for your help :)
     
  4. spryx

    spryx

    Joined:
    Jul 23, 2013
    Posts:
    557
    No prob at all. The tilebase overrides are a bit odd sometimes.