产品要做国际化,而一开始没做这方面的考虑,传统 方式修改起来很麻烦。所以决定从API调用处入手,直接替换为国际化音频。
下图是策划给过来的音频文档。
Paste_Image.png把它转换为xml
核心代码如下
Paste_Image.png原理: 签名相同的情况下,C#调用会优先找自身程序集的代码实现。
#if RESOURCESHOOK
using UnityEngine;
using System;
using Object = UnityEngine.Object;
using Babybus.Framework.Serialization;
using System.Collections.Generic;
public class AudioMapping
{
public string id;
public string zh;
private static Dictionary<string, string> ids = new Dictionary<string, string>();
static AudioMapping()
{
var bytes = UnityEngine.Resources.Load<TextAsset>("配置/AudioMapping").bytes;
var list = XmlUtility.DeserializeXml<List<AudioMapping>>(bytes);
foreach (var item in list)
{
if (string.IsNullOrEmpty(item.zh))
continue;
ids[item.zh] = item.id;
}
}
public static string GetId(string zh)
{
var index = zh.LastIndexOf('/');
if (index != -1)
zh = zh.Substring(index + 1);
var id = "";
if (!ids.TryGetValue(zh, out id))
return "";
return id;
}
}
public class Resources
{
public static Object[] FindObjectsOfTypeAll(Type type)
{
return UnityEngine.Resources.FindObjectsOfTypeAll(type);
}
public static T[] FindObjectsOfTypeAll<T>() where T : Object
{
return UnityEngine.Resources.FindObjectsOfTypeAll<T>();
}
public static Object GetBuiltinResource(Type type, string path)
{
return UnityEngine.Resources.GetBuiltinResource(type, path);
}
public static T GetBuiltinResource<T>(string path) where T : Object
{
return UnityEngine.Resources.GetBuiltinResource<T>(path);
}
public static Object Load(string path)
{
return UnityEngine.Resources.Load(path);
}
public static Object Load(string path, Type systemTypeInstance)
{
if (systemTypeInstance == typeof(AudioClip))
{
var id = AudioMapping.GetId(path);
if(id != "")
path = "Audios/en/" + id;
}
return UnityEngine.Resources.Load(path, systemTypeInstance);
}
public static T Load<T>(string path) where T : Object
{
return Load(path, typeof(T)) as T;
}
public static Object[] LoadAll(string path)
{
return UnityEngine.Resources.LoadAll(path);
}
public static Object[] LoadAll(string path, Type systemTypeInstance)
{
return UnityEngine.Resources.LoadAll(path, systemTypeInstance);
}
public static T[] LoadAll<T>(string path) where T : Object
{
return UnityEngine.Resources.LoadAll<T>(path);
}
public static ResourceRequest LoadAsync(string path)
{
return UnityEngine.Resources.LoadAsync(path);
}
public static ResourceRequest LoadAsync(string path, Type type)
{
return UnityEngine.Resources.LoadAsync(path, type);
}
public static ResourceRequest LoadAsync<T>(string path) where T : Object
{
return UnityEngine.Resources.LoadAsync(path);
}
public static void UnloadAsset(Object assetToUnload)
{
UnityEngine.Resources.UnloadAsset(assetToUnload);
}
public static AsyncOperation UnloadUnusedAssets()
{
return UnityEngine.Resources.UnloadUnusedAssets();
}
}
#endif
网友评论