Vinark117 / BetterRuleTiles-Support

Support repository for the Unity package "Better Rule Tiles"
1 stars 0 forks source link

Adding Tilesheet to a Sprite Atlas breaks Better Rule Tiles #21

Open Baviecca opened 4 months ago

Baviecca commented 4 months ago

Hi,

I have already created a rule tile based on a sprite sheet.

but i noticed that there were lines and gaps between the tiles and so i used a sprite atlas which fixed the gap issues perfectly.

but now i cant open the rule tile GUI at all and it throws me the following error:

Exception thrown while invoking [OnOpenAssetAttribute] method 'VinToolsEditor.BetterRuleTiles.BetterRuleTileAssetHandler:OpenCustomEditorWindow (int,int)' : InvalidCastException: Specified cast is not valid. VinToolsEditor.BetterRuleTiles.EditorGridBase.FixMissingTexture (UnityEngine.Texture2D tex) (at Assets/BetterRuleTiles/Editor/EditorSubClasses/EditorGridBase.cs:505) VinToolsEditor.BetterRuleTiles.EditorGridBase.CacheSprite (UnityEngine.Sprite sprite) (at Assets/BetterRuleTiles/Editor/EditorSubClasses/EditorGridBase.cs:475) VinToolsEditor.BetterRuleTiles.EditorGridBase.SetUpClass (VinToolsEditor.BetterRuleTiles.BetterRuleTileEditor window, System.Boolean _new) (at Assets/BetterRuleTiles/Editor/EditorSubClasses/EditorGridBase.cs:46) VinToolsEditor.BetterRuleTiles.EditorGridBase..ctor (VinToolsEditor.BetterRuleTiles.BetterRuleTileEditor window, System.Boolean _new) (at Assets/BetterRuleTiles/Editor/EditorSubClasses/EditorGridBase.cs:18) VinToolsEditor.BetterRuleTiles.BetterRuleTileEditor.SetupGrid (System.Boolean _new) (at Assets/BetterRuleTiles/Editor/CustomEditor/BetterRuleTileEditor.cs:218) VinToolsEditor.BetterRuleTiles.BetterRuleTileEditor.ShowWindow (VinTools.BetterRuleTiles.BetterRuleTileContainer container) (at Assets/BetterRuleTiles/Editor/CustomEditor/BetterRuleTileEditor.cs:40) VinToolsEditor.BetterRuleTiles.BetterRuleTileAssetHandler.OpenCustomEditorWindow (System.Int32 instanceID, System.Int32 line) (at Assets/BetterRuleTiles/Editor/AssetHandling/BetterRuleTileAssetHandler.cs:20) UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)

ManuelRauber commented 4 months ago

@Vinark117 I just discovered this bug as well.

First I created the BTR without an Atlas, and later I decided to add an Atlas for better performance, rendering etc. The moment you add the Atlas, opening the BTR-Editor breaks with the error message from the original post.

Drawing the tiles in the Unity Scene editor still works as expected.

Krechevskoy commented 3 months ago

Enabling Read/Write on the Sprite Atlas seems to fix the issue.

The existing error message is probably fine, just need to avoid the casting error:

public class EditorGridBase {
  //...
  public void FixMissingTexture(Texture2D tex) {
    var assetImporter = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(tex));
    if (assetImporter is TextureImporter texImporter) {
      texImporter.isReadable = true;
      EditorUtility.SetDirty(texImporter);
      texImporter.SaveAndReimport();
    }
  }
}