Class LocalizeSpriteEvent
Component that can be used to Localize a Sprite asset. Provides an update event OnUpdateAsset that can be used to automatically update the Sprite whenever the SelectedLocale or AssetReference changes.
Inheritance
LocalizeSpriteEvent
Inherited Members
Namespace: UnityEngine.Localization.Components
Syntax
public class LocalizeSpriteEvent : LocalizedAssetEvent<Sprite, LocalizedSprite, UnityEventSprite>
Remarks
This component can also be added through the Localize menu item in the Image context menu. Adding it this way will also automatically configure the Update Asset events to update the Image.
Examples
The example show how it is possible to switch between different Localized Sprites.
#if PACKAGE_UGUI
#region example-code
using UnityEngine;
using UnityEngine.Localization;
using UnityEngine.Localization.Components;
using UnityEngine.UI;
public class LocalizedSpriteChanger : MonoBehaviour
{
public LocalizeSpriteEvent localizeSpriteEvent;
public LocalizedSprite[] sprites;
public Image image;
int currentSprite = 0;
private void Start()
{
ChangeSprite(sprites[currentSprite]);
}
private void OnGUI()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button("Previous"))
{
if (currentSprite == 0)
currentSprite = sprites.Length - 1;
else
currentSprite--;
ChangeSprite(sprites[currentSprite]);
}
// Show the current sprite that is visible
GUILayout.Label(image.sprite?.name);
if (GUILayout.Button("Next"))
{
if (currentSprite == sprites.Length - 1)
currentSprite = 0;
else
currentSprite++;
ChangeSprite(sprites[currentSprite]);
}
GUILayout.EndHorizontal();
}
void ChangeSprite(LocalizedSprite sprite)
{
// When we assign a new AssetReference the system will automatically load the new Sprite asset then call the AssetChanged event.
localizeSpriteEvent.AssetReference = sprite;
}
}
#endregion
#endif