Custom materials for tk2d fonts

More
8 years 1 month ago #1894 by vividhelix
I'm trying to change the font per language while using tk2d text meshes.

Unfortunately I'm using a custom shader with additional textures. Just changing the font isn't enough unfortunately.

Are there any workarounds? I see I can define textures as terms, so ideally I'd need to change two of the textures on the material upon also changing the font.


Can you point me in the right direction?

Please Log in or Create an account to join the conversation.

More
8 years 1 month ago #1896 by Frank
Hi,

Unfortunately I'm using a custom shader with additional textures. Just changing the font isn't enough unfortunately.


I2Loc allows changing the font by using the Secondary Terms. When the font is changed, the material and textures associated with that font are also changed to the one that come with the new font.
What you normally would do for you case, is to create a new font and assign a NEW material and textures to it. That way, material and textures will be automatically changed.

Font1 (using Mat1, that references Texture1, Texture2 and several float parameters)
Font2 (using Mat2, that references Texture3, Texture4 and several float parameters)

Secondary Term assigns Font1 or Font2 depending on the language.

However, if your Font2, reuses the same material, then its a problem to handle that.
e.g. Font2 (using Mat1)

Are there any workarounds? I see I can define textures as terms, so ideally I'd need to change two of the textures on the material upon also changing the font.


Said that, if you really need to make more modifications to how the font/material/textures are handled, you could do it with a callback .

Just pack all the data you need into the SecondaryTerm translation and then parse that data back and apply any custom modification:

e.g. Instead of having the secondary term for English be "MyEnglishFont", make it "MyEnglishFont,MyCustomMaterial,MyTexture1,34"

Then, add this callback script to your localize component:
class CallbackExample : Monobehaviour        // this could be a separated class or a method in one of your own scripts
{
	public void OnModifyLocalization()
	{
		   // if no MainTranslation then skip (most likely this localize component only changes the font)
		   if (string.IsNullOrEmpty(Localize.MainTranslation))
				   return;

		   string[] data = Localize.MainTranslation.Split(",");
		   
		   string FontName = data[0];
		   string MaterialName = data[1];
		   string TextureName = data[2];
		   float ShaderParameter = float.Parse(data[3]);

		   tk2dFont newFont = Localize.FindTranslatedObject<tk2dFont>(FontName);        // loads the font from the Resources folder 
		   Material newMat = Localize.FindTranslatedObject<Material>(MaterialName);     // or if referenced in the Assets section
		   Texture newTex = Localize.FindTranslatedObject<Texture>(TextureName);
		   
		   newMat.SetFloat("MyParameter", ShaderParameter);
		   newMat.SetTexture("MyTexture", newTex);
		   
		   var textMesh = Localize.GetComponent<tk2dTextMesh>();
		   textMesh.font = newFont.data;
		   textMesh.font.material = newMat;
	}
}

That way you have full control over what modifications you do based on the language.

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

Please Log in or Create an account to join the conversation.

Time to create page: 0.123 seconds
Template by JoomlaShine