Search Unity

Question UI Source Image of Image Component not updating during runtime

Discussion in 'Image Effects' started by sobble_p, May 21, 2023.

  1. sobble_p

    sobble_p

    Joined:
    Dec 10, 2022
    Posts:
    3
    UPDATE: I solved the problem, you have to leave Prefab mode if you want it to actually update in runtime... lol

    Hello Unity Forum.
    I have several crosshair sprites for my first-person game, which I want to be able to switch between using the inspector during runtime (AKA while the game is played). I'm using a canvas object for the UI along with a crosshair object inside of it with an Image component.

    I am running into a seemingly trivial issue where the sprite will not update on the screen during runtime but will update while the game is not running. I am also using a custom shader but switching to other built-in UI shaders doesn't resolve the issue. How can I fix this problem?



    Here's a video of the problem in action. Starting at 0:01 is a demonstration of me choosing the type of crosshair with an enum value while not running the game, which updates on the screen correctly both on the Scene display and the Game display. At 0:11, you can see that the Scene display updates but not the Game display when selecting the options at runtime. Notice how the "Source Image" field in the Image Component continues to change correctly but does not update the Game display.

    Here are the two scripts that control the crosshair. Crosshair.cs is the main script and CrosshairEditor.cs is the custom inspector for Crosshair.cs.

    Crosshair.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. namespace HyBo_Crosshair
    8. {
    9.     public class Crosshair : MonoBehaviour
    10.     {
    11.         public enum CrosshairStyle
    12.         {
    13.             XCrosshair,
    14.             PlusCrosshair,
    15.             OCrosshair,
    16.             OMonocularCrosshair,
    17.             DotCrosshair,
    18.             SpiralCrosshair
    19.         }
    20.  
    21.         public bool enableCrosshair = true;
    22.         public CrosshairStyle crosshairStyle;
    23.         public Image imageComponent;
    24.  
    25.         // SPRITE REFERENCES
    26.  
    27.         public Sprite XCrosshairSprite;
    28.         public Sprite PlusCrosshairSprite;
    29.         public Sprite OCrosshairSprite;
    30.         public Sprite OMonocularCrosshairSprite;
    31.         public Sprite DotCrosshairSprite;
    32.         public Sprite SpiralCrosshairSprite;
    33.  
    34.         public void updateCrosshair()
    35.         {
    36.             if(enableCrosshair)
    37.             {
    38.                 switch(crosshairStyle)
    39.                 {
    40.                     case CrosshairStyle.XCrosshair:
    41.  
    42.                         Debug.Log("x crossheir");
    43.  
    44.                         imageComponent.sprite = XCrosshairSprite;
    45.  
    46.                         break;
    47.  
    48.                     case CrosshairStyle.PlusCrosshair:
    49.  
    50.                         Debug.Log("plus crossheir");
    51.  
    52.                         imageComponent.sprite = PlusCrosshairSprite;
    53.  
    54.                         break;
    55.  
    56.                     case CrosshairStyle.OCrosshair:
    57.  
    58.                         Debug.Log("o crossheir");
    59.  
    60.                         imageComponent.sprite = OCrosshairSprite;
    61.  
    62.                         break;
    63.  
    64.                     case CrosshairStyle.OMonocularCrosshair:
    65.  
    66.                         Debug.Log("o mono crossheir");
    67.  
    68.                         imageComponent.sprite = OMonocularCrosshairSprite;
    69.  
    70.                         break;
    71.  
    72.                     case CrosshairStyle.DotCrosshair:
    73.  
    74.                         Debug.Log("dot crossheir");
    75.  
    76.                         imageComponent.sprite = DotCrosshairSprite;
    77.  
    78.                         break;
    79.  
    80.                     case CrosshairStyle.SpiralCrosshair:
    81.  
    82.                         Debug.Log("sp1ral crossheir");
    83.  
    84.                         imageComponent.sprite = SpiralCrosshairSprite;
    85.  
    86.                         break;
    87.  
    88.                     default:
    89.  
    90.                         Debug.LogError($"Invalid CrosshairStyle enum type: ({crosshairStyle})");
    91.  
    92.                         imageComponent.sprite = null;
    93.  
    94.                         break;
    95.                 }
    96.             }
    97.             else
    98.             {
    99.                 Debug.Log("no crosshartehjruthtr");
    100.  
    101.                 imageComponent.sprite = null;
    102.             }
    103.         }
    104.     }
    105. }
    106.  
    CrosshairEditor.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using HyBo_Crosshair;
    4.  
    5. [CustomEditor(typeof(Crosshair))]
    6. public class CrosshairEditor : Editor
    7. {
    8.    public override void OnInspectorGUI()
    9.     {
    10.         // base.OnInspectorGUI();
    11.         Crosshair crosshair = (Crosshair) target;
    12.  
    13.         if(DrawDefaultInspector())
    14.         {
    15.             crosshair.updateCrosshair();
    16.         }
    17.     }
    18. }
    If I need to provide any additional information, please let me know. Thank you in advance!
     
    Last edited: May 22, 2023
  2. ms7982036

    ms7982036

    Joined:
    May 27, 2023
    Posts:
    1
    If you're using a UI framework or library and the source image of an Image component is not updating during runtime, there could be a few potential reasons and solutions:

    Caching: The image may be cached by the browser or the framework, causing it to not update when the source changes. To resolve this, you can try adding a query parameter or a unique identifier to the image URL each time it changes. For example:
    Code (CSharp):
    1. image_url = "path/to/image.jpg?timestamp=" + str(time.time())
    2.  
    This approach ensures that the browser recognizes the image as a new resource and fetches the updated version.

    Re-rendering: If you're using a reactive framework, such as React or Vue.js, make sure that you're updating the state or triggering a re-render of the component when the image source changes. Ensure that the component is receiving the updated image source prop or state.

    Verify the source update: Double-check that the source image is actually being updated correctly during runtime. Log the image source or use debugging tools to confirm that the new image URL is being set properly.


    Asynchronous loading: If the image source is being loaded asynchronously, make sure to handle the loading process appropriately. For example, if you're using an onLoad event callback, ensure that it fires after the new image is loaded and triggers any necessary updates.