New Term Types
8 years 4 months ago #1605
by Hamerub
New Term Types was created by Hamerub
Hi there,
I was wondering if you could implement a termtype which allows a URL + a field type. ( www.mysite.nl/images/image_01 , typeof(image))
This way we would be able to download images, video and other files from the internet at runtime.
Using this you can have all data externally and download it when you need it. This also greatly improves flexibility since you don't need to compile your Unity3d project again.
Thank you.
I was wondering if you could implement a termtype which allows a URL + a field type. ( www.mysite.nl/images/image_01 , typeof(image))
This way we would be able to download images, video and other files from the internet at runtime.
Using this you can have all data externally and download it when you need it. This also greatly improves flexibility since you don't need to compile your Unity3d project again.
Thank you.
Please Log in or Create an account to join the conversation.
8 years 4 months ago #1613
by Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
Replied by Frank on topic New Term Types
Hi,
Downloading images at runtime to use it in the game should be treated really carefully to avoid crashing the game. Specially in mobile where the memory is limited.
That's why I haven't added a term like the one you described.
Instead, its advised to use AssetBundles to store your new content (Images, etc) in the web. If you are already managing bundles, you can easily integrate your existing code with i2L for it get the Images from the bundles.
In version 2.6.7f1 I added support for bundles.
In the Examples\Common\Script folder there is a class named RegisterBundlesManager.cs
That component, when attached to an object in your scene, it registers itself in the ResourcesManager and if the sprite can't be found in the Resources folder, the managers calls the LoadFromBundle function, allowing you to get the sprite from your bundles.
If you are not managing AssetBundles, then in 2.6.8 I'm planning on releasing a simple manager that could do that for you.
However, if you still need to download images from the web when a localization happens, then you could use the following script.
Just add it to your GameObject containing the Image or RawImage and select the Term.
Hope that helps,
Frank
Downloading images at runtime to use it in the game should be treated really carefully to avoid crashing the game. Specially in mobile where the memory is limited.
That's why I haven't added a term like the one you described.
Instead, its advised to use AssetBundles to store your new content (Images, etc) in the web. If you are already managing bundles, you can easily integrate your existing code with i2L for it get the Images from the bundles.
In version 2.6.7f1 I added support for bundles.
In the Examples\Common\Script folder there is a class named RegisterBundlesManager.cs
That component, when attached to an object in your scene, it registers itself in the ResourcesManager and if the sprite can't be found in the Resources folder, the managers calls the LoadFromBundle function, allowing you to get the sprite from your bundles.
If you are not managing AssetBundles, then in 2.6.8 I'm planning on releasing a simple manager that could do that for you.
However, if you still need to download images from the web when a localization happens, then you could use the following script.
Just add it to your GameObject containing the Image or RawImage and select the Term.
using UnityEngine;
using System.Collections;
using I2.Loc;
public class DownloadLocalizedTexture : MonoBehaviour
{
[TermsPopup] public string Term;
public void OnEnable()
{
LocalizationManager.OnLocalizeEvent += OnLocalize;
}
public void OnDisable()
{
LocalizationManager.OnLocalizeEvent -= OnLocalize;
StopAllCoroutines ();
}
public void OnLocalize()
{
StartCoroutine ("DownloadTexture");
}
IEnumerator DownloadTexture()
{
// Get the localized URL and validate it
string TextureURL = ScriptLocalization.Get (Term);
if (string.IsNullOrEmpty (TextureURL))
yield break;
// Download the texture
var www = new WWW (TextureURL);
yield return www;
// Can't find the image or there is no internet
if (string.IsNullOrEmpty (www.error) || www.texture == null)
yield break;
// Set the value for Image
var imageCmp = GetComponent<UnityEngine.UI.Image> ();
if (imageCmp) {
imageCmp.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), Vector2.one*0.5f);
yield return null;
}
// Set the value for RawImage
var rawImage = GetComponent<UnityEngine.UI.RawImage> ();
if (rawImage)
rawImage.texture = www.texture;
}
}
Hope that helps,
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
Please Log in or Create an account to join the conversation.
8 years 4 months ago #1630
by Hamerub
Replied by Hamerub on topic New Term Types
Hey Frank,
Thank you.
I have refactored your script to create some custom localizers accepting JSON strings. (termtype: string)
Using JSON strings I can put alot of extra meta data into the field too.
With the localizers I'm now able to unserialize objects which contain strings to urls or local paths(paths on the device) and other data.
When I setup the path I can use Unity's WWW class to download images, audio and video from storage or the internet at runtime.
The WWW class also provides some streaming capabilities.
This way I can load my files from an external source. I also created a runtime importer for CSV files or JSON(convert json to csv) to always have the latest version of the language source file.
Thanks for your help with the localizers.
Hamers
Thank you.
I have refactored your script to create some custom localizers accepting JSON strings. (termtype: string)
Using JSON strings I can put alot of extra meta data into the field too.
With the localizers I'm now able to unserialize objects which contain strings to urls or local paths(paths on the device) and other data.
When I setup the path I can use Unity's WWW class to download images, audio and video from storage or the internet at runtime.
The WWW class also provides some streaming capabilities.
This way I can load my files from an external source. I also created a runtime importer for CSV files or JSON(convert json to csv) to always have the latest version of the language source file.
Thanks for your help with the localizers.
Hamers
Please Log in or Create an account to join the conversation.
8 years 4 months ago #1632
by Frank
Are you Give I2L 5 stars!
Are you Please lets us know how to improve it!
Replied by Frank on topic New Term Types
That's awesome!
I'm glad the script helped you!!
Frank
I'm glad the script helped you!!
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
Please Log in or Create an account to join the conversation.
Time to create page: 0.204 seconds