NGUI Localization的修改

本文详细介绍了如何使用单例模式在Localization.cs脚本中实现资源加载与管理,包括静态变量定义、Awake()方法应用以及场景中物体的配置与语言文件的导入,最终实现动态语言切换。


打开Localization.cs

首先将静态类变成非静态类,继承MonoBehaviour类;

增加静态变量(单例模式)

static Localization mInstance;
static public Localization instance
	{
		get
		{
			if (mInstance == null)
			{
				mInstance = Object.FindObjectOfType(typeof(Localization)) as Localization;

				if (mInstance == null)
				{
					GameObject go = new GameObject("_Localization");
					DontDestroyOnLoad(go);
					mInstance = go.AddComponent<Localization>();
				}
			}
			return mInstance;
		}
	}

	public string startingLanguage = "china";

	public TextAsset[] languages;

Dictionary<string, string> myDictionary = new Dictionary<string, string>();
	string myLanguage;

public string currentLanguage
	{
		get
		{
			return myLanguage;
		}
		set
		{
			if (myLanguage != value)
			{
				startingLanguage = value;

				if (!string.IsNullOrEmpty(value))
				{
					// Check the referenced assets first
					if (languages != null)
					{
						for (int i = 0, imax = languages.Length; i < imax; ++i)
						{
							TextAsset asset = languages[i];

							if (asset != null && asset.name == value)
							{
								Load(asset);
								return;
							}
						}
					}

					// Not a referenced asset -- try to load it dynamically
					TextAsset txt = Resources.Load(value, typeof(TextAsset)) as TextAsset;

					if (txt != null)
					{
						Load(txt);
						return;
					}
				}

				// Either the language is null, or it wasn't found
				myDictionary.Clear();
				PlayerPrefs.DeleteKey("Language");
			}
		}
	}

然后在Awake()方法中开始调用:

void Awake()
	{
		if (mInstance == null)
		{
			mInstance = this;
			DontDestroyOnLoad(gameObject);

			currentLanguage = PlayerPrefs.GetString("Language", startingLanguage);

			if (string.IsNullOrEmpty(mLanguage) && (languages != null && languages.Length > 0))
			{
				currentLanguage = languages[0].name;
			}
		}
		else Destroy(gameObject);
	}

之后在场景中新建一个物体,并把这个脚本添加在这个物体上;

最后在工程新建文件夹“Localization”,把我们的语言(.txt)放进这个文件夹下;然后把语言文件拖进上面新建物体的脚本中,如图:

语言文件,例如:



再我们的Lable组件下添加:


并且把key设置为“UI_Play2”;

这样就OK了。

3.5.0 - NEW: Localization system now supports CSV type input. - NEW: UILocalize script now has key lookup and localized preview options. - NEW: UICamera now has a new event type that supports 2D colliders. - NEW: Added justified alignment support for labels. - NEW: Scroll views now have a Content Origin point. - NEW: You can now freely adjust width and height of anchored widgets. - NEW: UIDragResize script now has a maximum size limiting option as well. - FIX: Improved scroll view resizing and logic regarding how it repositions the content. - FIX: Fixed an issue with how changing panel's alpha would not propagate to children in certain cases. - FIX: NGUI will no longer intercept RMB events that occur outside the selected widget's area. - FIX: UICenterOnClick should now work as expected when there is no UICenterOnChild present. - FIX: UICenterOnClick shouldn't cache the panel anymore, making it work properly with drag & drop. - FIX: Widget inspector's Dimensions field should no longer be grayed out if the widget is partially anchored. - FIX: UIRoot's FixedSizeOnMobiles setting should now recognize BB and WP8 as mobile devices - FIX: UICamera will now clear all active touch events when the application is paused. - FIX: Work-around for dynamic font delegate subscriptions causing epic GC. - FIX: Setting label text will now auto-adjust the collider size. - FIX: Inlined italic text should now look better. 3.5.1 - NEW: CSV reader will now convert the "\n" character sequence into a new line char. - FIX: Scenes using NGUI should no longer get marked edited so much. - FIX: Reduced the size of meshes used by NGUI draw calls. - FIX: Changing the panel's alpha will now properly inform child panels. - FIX: Fix for how URL tags get parsed in labels.
### NGUI 中实时修改材质 在 Unity 使用 NGUI 时,可以通过脚本实现实时修改 UI 组件的材质。这通常涉及到访问 `UILabel` 或者 `UISprite` 的属性并动态更改其使用的材质。 对于 `UISprite` 和 `UILabel` 这样的组件来说,可以获取到它们所关联的游戏对象,并通过该游戏对象上的渲染器来改变材质。下面是一个简单的 C# 脚本示例,展示了如何实现这一功能: ```csharp using UnityEngine; using System.Collections; public class ChangeMaterial : MonoBehaviour { public Material newMaterial; // 在Inspector中指定新的材质 void Update () { if (Input.GetKeyDown(KeyCode.M)) { // 当按下M键时更换材质 UISprite spriteComponent = GetComponent<UISprite>(); if (spriteComponent != null){ spriteComponent.mainTexture = newMaterial.mainTexture; Renderer rend = spriteComponent.GetComponent<Renderer>(); if(rend != null){ rend.material = newMaterial; } } UILabel labelComponent = GetComponent<UILabel>(); if(labelComponent != null){ labelComponent.trueTypeFont = null; // 如果是TTF字体则设为空以应用材质变化 MeshRenderer meshRend = labelComponent.GetComponent<MeshRenderer>(); if(meshRend != null){ meshRend.material = newMaterial; } } } } } ``` 此代码片段允许开发者在运行期间按 M 键切换指定游戏对象(带有 `UISprite` 或 `UILabel` 组件的对象)的材质[^1]。 需要注意的是,在某些情况下可能还需要调整其他参数才能使新材质正确显示出来,比如当使用 TTF 字体时需要将 `trueTypeFont` 设置为 `null` 才能让自定义材质生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值