How to localize secondary sprite swap images?
- pedrevans
- Topic Author
- Visitor
9 years 2 months ago - 9 years 2 months ago #1004
by pedrevans
How to localize secondary sprite swap images? was created by pedrevans
I've been using separate localized images for many of my buttons. I use the sprite swap mechanism to swap between images when a button is tapped. The primary image is easily localized. But I haven't been able to figure out how to localize the secondary "pressed" image. The attached image shows which component is affected.
How does one do this with I2L?
How does one do this with I2L?
Last edit: 9 years 2 months ago by pedrevans.
Please Log in or Create an account to join the conversation.
9 years 2 months ago - 9 years 2 months ago #1005
by Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
Replied by Frank on topic How to localize secondary sprite swap images?
Hi,
Out of the box, the plugin doesn't support localizing Button states. This is because a button has several images, while the Localize component only handles localization for 1 target.
Nonetheless, the support for multiple targets is coming to I2 Localization v3 (although it will be still a couple releases before switching to an stable v3)
However, to solve your issue with the current plugin features, you could use localization callbacks to modify the sprites.
Just create a script using this code and attach it to your button, next to the Localize component:
Then, in the localize component, at the bottom, there is a field that allows you adding a callback. Call the OnModifyLocalization function. That way, whenever the plugin localizes, it will also call that function.
If you check the script, it just takes the term that its been used and asks the localization source for variants of it ("PRESSED", "DISABLED", etc).
So, if you create a term "BUTTON_PLAY" to localize that button, you will also need to create "BUTTON_PLAY_PRESSED", "BUTTON_PLAY_DISABLED", etc.
Hope that helps, and don't hesitate in letting me know if you need more details about how to use this.
Thanks,
Frank
Out of the box, the plugin doesn't support localizing Button states. This is because a button has several images, while the Localize component only handles localization for 1 target.
Nonetheless, the support for multiple targets is coming to I2 Localization v3 (although it will be still a couple releases before switching to an stable v3)
However, to solve your issue with the current plugin features, you could use localization callbacks to modify the sprites.
Just create a script using this code and attach it to your button, next to the Localize component:
using UnityEngine;
using UnityEngine.UI;
using I2.Loc;
public class CallbackNotification : MonoBehaviour
{
public void OnModifyLocalization( Localize loc )
{
Button button = GetComponent<Button>();
Sprite sp = (Sprite)ScriptLocalization.Get (Localize.CallBackTerm);
if (sp!=null) GetComponent<Image>().sprite = sp;
sp = (Sprite)ScriptLocalization.Get (Localize.CallBackTerm + "HIGHLIGHTED");
if (sp!=null) button.spriteState.highlightedSprite = sp;
sp = (Sprite)ScriptLocalization.Get (Localize.CallBackTerm + "PRESSED");
if (sp!=null) button.spriteState.pressedSprite = sp;
sp = (Sprite)ScriptLocalization.Get (Localize.CallBackTerm + "DISABLED");
if (sp!=null) button.spriteState.disabledSprite = sp
}
}
Then, in the localize component, at the bottom, there is a field that allows you adding a callback. Call the OnModifyLocalization function. That way, whenever the plugin localizes, it will also call that function.
If you check the script, it just takes the term that its been used and asks the localization source for variants of it ("PRESSED", "DISABLED", etc).
So, if you create a term "BUTTON_PLAY" to localize that button, you will also need to create "BUTTON_PLAY_PRESSED", "BUTTON_PLAY_DISABLED", etc.
Hope that helps, and don't hesitate in letting me know if you need more details about how to use this.
Thanks,
Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
To get the betas as soon as they are ready,
check this out
Last edit: 9 years 2 months ago by Frank.
Please Log in or Create an account to join the conversation.
- pedrevans
- Topic Author
- Visitor
9 years 2 months ago #1006
by pedrevans
Replied by pedrevans on topic How to localize secondary sprite swap images?
Thanks, that's an excellent tip. It means a fair amount of work so maybe it would be better to use the color tint mechanism instead and just stick to the one image per element, especially as my app is already starting to suffer from asset bloat. I will think about that.
Please Log in or Create an account to join the conversation.
- pedrevans
- Topic Author
- Visitor
9 years 1 month ago #1103
by pedrevans
Replied by pedrevans on topic How to localize secondary sprite swap images?
After rigorously enforcing a naming convention, I was able to implement this. It was a little more complex than Frank's suggestion, but the approach was his. This is the code (UnityScript) for pressed button only, I didn't need the other kinds. It works perfectly.
Thanks Frank!
class LocalizationCallback extends MonoBehaviour {
function OnModifyLocalization(loc: Localize){
Debug.Log('LocalizationCallback.CallBackTerm: '+Localize.CallBackTerm);
var translation: String = ScriptLocalization.Get(Localize.CallBackTerm + " p", false) as String;
if (translation == null)
return;
var pressedSpritePath: String = Regex.Replace(translation, '\\[.*', '');
var pressedSprite: Sprite = Resources.Load.<Sprite>(pressedSpritePath);
var spriteState: SpriteState = new SpriteState();
spriteState.pressedSprite = pressedSprite;
var button: Button = GetComponent.<Button>();
button.spriteState = spriteState;
}
}
Please Log in or Create an account to join the conversation.
Time to create page: 0.157 seconds