Unity脚本引用原理,修复Unity脚本引用丢失,源码脚本与dll中的脚本引用互换

原文出处:http://blog.csdn.net/gz_huangzl/article/details/52486509


前言

在我们开发游戏的过程中,经常会碰到脚本引用丢失的情况,但是怎么把它们修复到我们的理想情况呢?先在这打个预防针,不是所有情况下的脚本引用丢失都能修复,但绝大多数情况下都是可行的,只要你知道原来脚本的GUID和FILEID(不知道也可以在prefab中找到),最重要的是你要有(必须有)用来做修复的脚本GUID和FILEID,要不然就没办法修复 了。

我举个极端情况,假如Prefab挂了A脚本,但是这个Prefab是第三方的,但是它却没有把A脚本给你,这种情况下你就没办法修复了,除非你通过其它途径知道了A脚本的实现,你自己在本地创建了一个类A的脚本,这样才可能被修复


脚本在Prefab中被引用的原理

脚本被引用有两种情况

a.prefab引用的是cs文件脚本

b.prefab引用的是dll文件中的脚本


区别一:

对于第一种情况,脚本的文件名必须和类名相同,且必须为MonoBehaviour类,不管脚本里面有一个或多个类,只有和文件名相同的类名的才能被挂接上

对于第二种情况,一个脚本可以包含多个MonoBehaviour类,如果把它打成dll后,它里面的所有类都是可以被挂接

区别二:

prefab挂了脚本,打成AssetBundle后,加载运行的时候,只有第一种情况的脚本是可以生效的,挂的dll是无效,因为bundle加载并初始化的时候,unity是从包内的脚本中去搜索的,并不会从包内的dll中去搜索(这也是脚本更新的拦路虎之一,解决方法要么动态挂脚本,要么挂的脚本不热更,跟包走)


引用的原理,如下图:


用文本编辑器打开prefab文件,如上图,挂载的脚本如上紫框内所示,fileID脚本中类的信息,guild表示脚本所在的文件ID

a.直接挂载脚本

    这种情况下,fileID的值永远是11500000,它实际上指的是MonoScript类型组件,它的值由ClassID * 10000所得,详情见官方文档,而guid能直接定位到MonoScript脚本文件

    所以它是通过guid找到脚本文件,然后挂载这个脚本中与脚本文件名相同的类

b.直接挂载DLL中的脚本

   这种情况下,fileID的值是由"s\0\0\0" + type.Namespace + type.Name的值转换成MD4的值,guid指的是这个dll所对应的文件(MD4的算法贴在最后面)

   所以它是通过guid找到dll文件,然后生成里面所有类的MD4,然后和这个值做比对,相同的则挂载上去


脚本引用修复

一、原来是挂的脚本(可以通过fileID等于1150000判断),要替换成新的脚本

       a.把prefab中的guid通过文本工具统一替换成新脚本的guid

       b.直接把新脚本的.mate文件里面的guid改成prefab中的

       c.如果新脚本已经有地方有的,不能改.mate,就只能使用a方法

二、原来是挂的DLL中的脚本(可以通过fileID不等于1150000判断),要替换成新的脚本

       a.把prefab中的guid改成新脚本的guid,把fileID统一改成1150000

三、原来是挂的脚本,要替换成dll中的脚本

       a.把prefab中的guid改成dll的guid,把fileID统一改成DLL中类的Type算出来的MD4的值

四、原来是挂的DLL中的脚本,要替换成新DLL中的脚本

       a.把prefab中的guid改成新dll的guid,把fileID统一改成新DLL中类的Type算出来的MD4的值


注:如写工具时,要获得一个本地脚本中包含哪一些类,可以使用AssetDatabase.Load,它返回的是一个MonoScript,通过它可以获得到所有类的信息


MD4算法如下:

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Security.Cryptography;  
  5.   
  6. namespace cn.crashByNull  
  7. {  
  8.     public class MD4 : HashAlgorithm  
  9.     {  
  10.         private uint _a;  
  11.         private uint _b;  
  12.         private uint _c;  
  13.         private uint _d;  
  14.         private uint[] _x;  
  15.         private int _bytesProcessed;  
  16.   
  17.         public MD4()  
  18.         {  
  19.             _x = new uint[16];  
  20.   
  21.             Initialize();  
  22.         }  
  23.   
  24.         public override void Initialize()  
  25.         {  
  26.             _a = 0x67452301;  
  27.             _b = 0xefcdab89;  
  28.             _c = 0x98badcfe;  
  29.             _d = 0x10325476;  
  30.   
  31.             _bytesProcessed = 0;  
  32.         }  
  33.   
  34.         protected override void HashCore(byte[] array, int offset, int length)  
  35.         {  
  36.             ProcessMessage(Bytes(array, offset, length));  
  37.         }  
  38.   
  39.         protected override byte[] HashFinal()  
  40.         {  
  41.             try  
  42.             {  
  43.                 ProcessMessage(Padding());  
  44.   
  45.                 return new[] { _a, _b, _c, _d }.SelectMany(word => Bytes(word)).ToArray();  
  46.             }  
  47.             finally  
  48.             {  
  49.                 Initialize();  
  50.             }  
  51.         }  
  52.   
  53.         private void ProcessMessage(IEnumerable<byte> bytes)  
  54.         {  
  55.             foreach (byte b in bytes)  
  56.             {  
  57.                 int c = _bytesProcessed & 63;  
  58.                 int i = c >> 2;  
  59.                 int s = (c & 3) << 3;  
  60.   
  61.                 _x[i] = (_x[i] & ~((uint)255 << s)) | ((uint)b << s);  
  62.   
  63.                 if (c == 63)  
  64.                 {  
  65.                     Process16WordBlock();  
  66.                 }  
  67.   
  68.                 _bytesProcessed++;  
  69.             }  
  70.         }  
  71.   
  72.         private static IEnumerable<byte> Bytes(byte[] bytes, int offset, int length)  
  73.         {  
  74.             for (int i = offset; i < length; i++)  
  75.             {  
  76.                 yield return bytes[i];  
  77.             }  
  78.         }  
  79.   
  80.         private IEnumerable<byte> Bytes(uint word)  
  81.         {  
  82.             yield return (byte)(word & 255);  
  83.             yield return (byte)((word >> 8) & 255);  
  84.             yield return (byte)((word >> 16) & 255);  
  85.             yield return (byte)((word >> 24) & 255);  
  86.         }  
  87.   
  88.         private IEnumerable<byte> Repeat(byte value, int count)  
  89.         {  
  90.             for (int i = 0; i < count; i++)  
  91.             {  
  92.                 yield return value;  
  93.             }  
  94.         }  
  95.   
  96.         private IEnumerable<byte> Padding()  
  97.         {  
  98.             return Repeat(128, 1)  
  99.                .Concat(Repeat(0, ((_bytesProcessed + 8) & 0x7fffffc0) + 55 - _bytesProcessed))  
  100.                .Concat(Bytes((uint)_bytesProcessed << 3))  
  101.                .Concat(Repeat(0, 4));  
  102.         }  
  103.   
  104.         private void Process16WordBlock()  
  105.         {  
  106.             uint aa = _a;  
  107.             uint bb = _b;  
  108.             uint cc = _c;  
  109.             uint dd = _d;  
  110.   
  111.             foreach (int k in new[] { 0, 4, 8, 12 })  
  112.             {  
  113.                 aa = Round1Operation(aa, bb, cc, dd, _x[k], 3);  
  114.                 dd = Round1Operation(dd, aa, bb, cc, _x[k + 1], 7);  
  115.                 cc = Round1Operation(cc, dd, aa, bb, _x[k + 2], 11);  
  116.                 bb = Round1Operation(bb, cc, dd, aa, _x[k + 3], 19);  
  117.             }  
  118.   
  119.             foreach (int k in new[] { 0, 1, 2, 3 })  
  120.             {  
  121.                 aa = Round2Operation(aa, bb, cc, dd, _x[k], 3);  
  122.                 dd = Round2Operation(dd, aa, bb, cc, _x[k + 4], 5);  
  123.                 cc = Round2Operation(cc, dd, aa, bb, _x[k + 8], 9);  
  124.                 bb = Round2Operation(bb, cc, dd, aa, _x[k + 12], 13);  
  125.             }  
  126.   
  127.             foreach (int k in new[] { 0, 2, 1, 3 })  
  128.             {  
  129.                 aa = Round3Operation(aa, bb, cc, dd, _x[k], 3);  
  130.                 dd = Round3Operation(dd, aa, bb, cc, _x[k + 8], 9);  
  131.                 cc = Round3Operation(cc, dd, aa, bb, _x[k + 4], 11);  
  132.                 bb = Round3Operation(bb, cc, dd, aa, _x[k + 12], 15);  
  133.             }  
  134.   
  135.             unchecked  
  136.             {  
  137.                 _a += aa;  
  138.                 _b += bb;  
  139.                 _c += cc;  
  140.                 _d += dd;  
  141.             }  
  142.         }  
  143.   
  144.         private static uint ROL(uint value, int numberOfBits)  
  145.         {  
  146.             return (value << numberOfBits) | (value >> (32 - numberOfBits));  
  147.         }  
  148.   
  149.         private static uint Round1Operation(uint a, uint b, uint c, uint d, uint xk, int s)  
  150.         {  
  151.             unchecked  
  152.             {  
  153.                 return ROL(a + ((b & c) | (~b & d)) + xk, s);  
  154.             }  
  155.         }  
  156.   
  157.         private static uint Round2Operation(uint a, uint b, uint c, uint d, uint xk, int s)  
  158.         {  
  159.             unchecked  
  160.             {  
  161.                 return ROL(a + ((b & c) | (b & d) | (c & d)) + xk + 0x5a827999, s);  
  162.             }  
  163.         }  
  164.   
  165.         private static uint Round3Operation(uint a, uint b, uint c, uint d, uint xk, int s)  
  166.         {  
  167.             unchecked  
  168.             {  
  169.                 return ROL(a + (b ^ c ^ d) + xk + 0x6ed9eba1, s);  
  170.             }  
  171.         }  
  172.     }  
  173.   
  174.     public static class FileIDUtil  
  175.     {  
  176.         public static int Compute(Type t)  
  177.         {  
  178.             string toBeHashed = "s\0\0\0" + t.Namespace + t.Name;  
  179.   
  180.             using (HashAlgorithm hash = new MD4())  
  181.             {  
  182.                 byte[] hashed = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toBeHashed));  
  183.   
  184.                 int result = 0;  
  185.   
  186.                 for (int i = 3; i >= 0; --i)  
  187.                 {  
  188.                     result <<= 8;  
  189.                     result |= hashed[i];  
  190.                 }  
  191.   
  192.                 return result;  
  193.             }  
  194.         }  
  195.     }  
  196. }  
  197. </byte></byte></byte></byte></byte>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值