Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Editor Script Issues.

Discussion in 'Scripting' started by Iceman420, Jun 24, 2023.

  1. Iceman420

    Iceman420

    Joined:
    Oct 28, 2021
    Posts:
    13
    Hi everyone !
    I'm trying to create a custom editor script to make it easier to swap sprites from a sprite atlas.
    The component itself works as it should, but whenever I make a change and save, I get this warning :
    "Could not extract GUID in text file".
    In project settings when I change the Asset Sterilization Mode, the error disappears, and I can build the scene successfully, but when I make a change the error comes back.
    Maybe someone could help, I would really appreciate it :)

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.U2D;
    4. using SuperComputer.Interfaces;
    5.  
    6. namespace SuperComputer.Components
    7. {
    8.     public class DisplaySpriteFromAtlas : MonoBehaviour, IComponent
    9.     {
    10.         #region Fields
    11.  
    12.         public SpriteAtlas SpriteAtlas;
    13.         public int SelectedSpriteIndex;
    14.         public RendererType RendererTypeChoice;
    15.         public SpriteRenderer SpriteRendererComponent;
    16.         public Image ImageComponent;
    17.  
    18.         #endregion
    19.  
    20.         #region Enum
    21.  
    22.         public enum RendererType
    23.         {
    24.             SpriteRenderer,
    25.             Image
    26.         }
    27.  
    28.         #endregion
    29.  
    30.         #region IComponent Implementation
    31.  
    32.         public void Initialize(GameObject owner)
    33.         {
    34.             UpdateSprite();
    35.         }
    36.  
    37.         #endregion
    38.  
    39.         #region Public Methods
    40.  
    41.         public void UpdateSprite()
    42.         {
    43.             if (SpriteAtlas == null || SelectedSpriteIndex < 0)
    44.                 return;
    45.  
    46.             var sprites = new Sprite[SpriteAtlas.spriteCount];
    47.             SpriteAtlas.GetSprites(sprites);
    48.  
    49.             if (SelectedSpriteIndex < sprites.Length)
    50.             {
    51.                 var selectedSprite = sprites[SelectedSpriteIndex];
    52.  
    53.                 switch (RendererTypeChoice)
    54.                 {
    55.                     case RendererType.SpriteRenderer:
    56.                         if (SpriteRendererComponent != null)
    57.                             SpriteRendererComponent.sprite = selectedSprite;
    58.                         break;
    59.                     case RendererType.Image:
    60.                         if (ImageComponent != null)
    61.                             ImageComponent.sprite = selectedSprite;
    62.                         break;
    63.                 }
    64.             }
    65.         }
    66.  
    67.         #endregion
    68.  
    69. #if UNITY_EDITOR
    70.         private void OnValidate()
    71.         {
    72.             if (!Application.isPlaying && UnityEditor.Selection.activeGameObject == gameObject)
    73.             {
    74.                 UnityEditor.EditorApplication.delayCall += UpdateSprite;
    75.             }
    76.         }
    77.  
    78.         private void OnDisable()
    79.         {
    80.             UnityEditor.EditorApplication.delayCall -= UpdateSprite;
    81.         }
    82. #endif
    83.     }
    84. }
    85.  
    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.U2D;
    4. using SuperComputer.Components;
    5.  
    6. [CustomEditor(typeof(DisplaySpriteFromAtlas))]
    7. public class DisplaySpriteFromAtlasEditor : Editor
    8. {
    9.     #region Fields
    10.  
    11.     private SerializedProperty spriteAtlasProperty;
    12.     private SerializedProperty selectedSpriteIndexProperty;
    13.     private SerializedProperty rendererTypeChoiceProperty;
    14.     private SerializedProperty spriteRendererComponentProperty;
    15.     private SerializedProperty imageComponentProperty;
    16.  
    17.     #endregion
    18.  
    19.     #region Editor Callbacks
    20.  
    21.     private void OnEnable()
    22.     {
    23.         // Find serialized properties
    24.         spriteAtlasProperty = serializedObject.FindProperty("SpriteAtlas");
    25.         selectedSpriteIndexProperty = serializedObject.FindProperty("SelectedSpriteIndex");
    26.         rendererTypeChoiceProperty = serializedObject.FindProperty("RendererTypeChoice");
    27.         spriteRendererComponentProperty = serializedObject.FindProperty("SpriteRendererComponent");
    28.         imageComponentProperty = serializedObject.FindProperty("ImageComponent");
    29.     }
    30.  
    31.     public override void OnInspectorGUI()
    32.     {
    33.         // Update serialized object
    34.         serializedObject.Update();
    35.  
    36.         // Display Sprite Atlas property field
    37.         EditorGUILayout.PropertyField(spriteAtlasProperty);
    38.  
    39.         // Check if Sprite Atlas is assigned
    40.         if (spriteAtlasProperty.objectReferenceValue == null)
    41.         {
    42.             EditorGUILayout.HelpBox("Please assign a Sprite Atlas.", MessageType.Info);
    43.             serializedObject.ApplyModifiedProperties();
    44.             return;
    45.         }
    46.  
    47.         // Retrieve sprites from the Sprite Atlas
    48.         var spriteAtlas = spriteAtlasProperty.objectReferenceValue as SpriteAtlas;
    49.         var sprites = new Sprite[spriteAtlas.spriteCount];
    50.         spriteAtlas.GetSprites(sprites);
    51.         var spriteNames = new string[sprites.Length];
    52.  
    53.         // Extract sprite names
    54.         for (int i = 0; i < sprites.Length; i++)
    55.         {
    56.             spriteNames[i] = sprites[i].name;
    57.         }
    58.  
    59.         // Display selected sprite index popup
    60.         DrawSelectedSpriteIndexField(spriteNames);
    61.  
    62.         // Display renderer type choice
    63.         EditorGUILayout.PropertyField(rendererTypeChoiceProperty, new GUIContent("Renderer Type"), true);
    64.  
    65.         // Check the selected renderer type
    66.         var rendererType = (DisplaySpriteFromAtlas.RendererType)rendererTypeChoiceProperty.enumValueIndex;
    67.  
    68.         if (rendererType == DisplaySpriteFromAtlas.RendererType.SpriteRenderer)
    69.         {
    70.             // Display SpriteRenderer component field
    71.             DrawSpriteRendererComponentField();
    72.         }
    73.         else
    74.         {
    75.             // Display Image component field
    76.             DrawImageComponentField();
    77.         }
    78.  
    79.         // Apply modified properties
    80.         serializedObject.ApplyModifiedProperties();
    81.     }
    82.  
    83.  
    84.     #endregion
    85.  
    86.     #region GUI Drawing Methods
    87.  
    88.     private void DrawSelectedSpriteIndexField(string[] spriteNames)
    89.     {
    90.         EditorGUILayout.BeginHorizontal();
    91.         EditorGUILayout.LabelField("Selected Sprite", GUILayout.Width(EditorGUIUtility.labelWidth - 4));
    92.         selectedSpriteIndexProperty.intValue = EditorGUILayout.Popup(selectedSpriteIndexProperty.intValue, spriteNames);
    93.         EditorGUILayout.EndHorizontal();
    94.     }
    95.  
    96.     private void DrawSpriteRendererComponentField()
    97.     {
    98.         EditorGUILayout.PropertyField(spriteRendererComponentProperty, new GUIContent("Sprite Renderer"), true);
    99.  
    100.         if (spriteRendererComponentProperty.objectReferenceValue == null)
    101.         {
    102.             EditorGUILayout.HelpBox("Please assign a SpriteRenderer component.", MessageType.Info);
    103.         }
    104.     }
    105.  
    106.     private void DrawImageComponentField()
    107.     {
    108.         EditorGUILayout.PropertyField(imageComponentProperty, new GUIContent("UI Image"), true);
    109.  
    110.         if (imageComponentProperty.objectReferenceValue == null)
    111.         {
    112.             EditorGUILayout.HelpBox("Please assign an Image component.", MessageType.Info);
    113.         }
    114.     }
    115.  
    116.     #endregion
    117. }
    118.  
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    This error likely means an invalid GUID was written to the asset file. This shouldn't happen and is a bug somewhere in Unity. Are on the latest patch version of the Unity release stream you're using?

    There are many internal causes that can lead to this issue and, over the years, different causes have popped up and been fixed. You should report a bug to Unity so they can find and fix the issue, there's little that can be done from the outside.

    Maybe you can find a workaround but that's usually time-consuming. Searching for the error will turn up all kinds of different causes, so its difficult to say if a solution applies to your case. You can try to narrow down when exactly the problem appears and then see if you can change things around to avoid it.
     
  3. Iceman420

    Iceman420

    Joined:
    Oct 28, 2021
    Posts:
    13
    I tried running it on 19.4.4 and on 22.2.15 and on 22.3.1

    With all of them I got the same issue.
     
  4. Iceman420

    Iceman420

    Joined:
    Oct 28, 2021
    Posts:
    13
    Maybe someone has any other idea ? I really want to share this tool, I think it could be really handy, yet I didn't want to share it with this bug.
     
  5. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,051
    2019.4 and 2022.2 are no longer updated and the latest version of 2022.3 is 2022.3.3. Always check with the latest patch release wether an issue has been fixed (best would be to also try with the latest Tech stream release – 2023.1.1).

    Did you report a bug to Unity? That's the best way to get your issue looked at by someone who can actually debug and fix the issue. Even if you find a workaround, it's good to report the issue so that it can be fixed for everyone who might run into it in the future.

    Post the internal bug number here, in case a Unity dev reads this post, and post the public issue tracker link once it's available, so that others can can check if/when the bug has been fixed.