Unity API HOOK 之妙用

作者: 2b75747cf703 | 来源:发表于2016-09-07 11:06 被阅读1185次

    产品要做国际化,而一开始没做这方面的考虑,传统 方式修改起来很麻烦。所以决定从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
    

    相关文章

      网友评论

      • df4b92afcd90:感谢分享。
      • 85c9881f8c89:好东西。谢谢分享
      • 阿飞咯:那如果需要Hook unity系统函数呢?请问一下怎么做呀
        阿飞咯:@千里伏骥 我说的C#的接口 unity.pdb里面的源码
        2b75747cf703:@阿飞咯 你说的系统函数是指?这个就是Unity的API了。。

      本文标题:Unity API HOOK 之妙用

      本文链接:https://www.haomeiwen.com/subject/pfsjettx.html