前言
在我們開發(fā)游戲的過程中独榴,經(jīng)常會碰到腳本引用丟失的情況,但是怎么把它們修復(fù)到我們的理想情況呢奕枝?先在這打個預(yù)防針棺榔,不是所有情況下的腳本引用丟失都能修復(fù),但絕大多數(shù)情況下都是可行的隘道,只要你知道原來腳本的GUID和FILEID(不知道也可以在prefab中找到)掷豺,最重要的是你要有(必須有)用來做修復(fù)的腳本GUID和FILEID,要不然就沒辦法修復(fù) 了薄声。
我舉個極端情況当船,假如Prefab掛了A腳本,但是這個Prefab是第三方的默辨,但是它卻沒有把A腳本給你德频,這種情況下你就沒辦法修復(fù)了,除非你通過其它途徑知道了A腳本的實現(xiàn)缩幸,你自己在本地創(chuàng)建了一個類A的腳本壹置,這樣才可能被修復(fù)
腳本在Prefab中被引用的原理
腳本被引用有兩種情況
a.prefab引用的是cs文件腳本
b.prefab引用的是dll文件中的腳本
區(qū)別一:
對于第一種情況竞思,腳本的文件名必須和類名相同,且必須為MonoBehaviour類钞护,不管腳本里面有一個或多個類盖喷,只有和文件名相同的類名的才能被掛接上
對于第二種情況,一個腳本可以包含多個MonoBehaviour類难咕,如果把它打成dll后课梳,它里面的所有類都是可以被掛接的
區(qū)別二:
prefab掛了腳本,打成AssetBundle后余佃,加載運行的時候暮刃,只有第一種情況的腳本是可以生效的,掛的dll是無效爆土,因為bundle加載并初始化的時候椭懊,unity是從包內(nèi)的腳本中去搜索的,并不會從包內(nèi)的dll中去搜索(這也是腳本更新的攔路虎之一步势,解決方法要么動態(tài)掛腳本氧猬,要么掛的腳本不熱更,跟包走)
引用的原理坏瘩,如下圖:
用文本編輯器打開prefab文件盅抚,如上圖,掛載的腳本如上紫框內(nèi)所示桑腮,fileID腳本中類的信息,guild表示腳本所在的文件ID
a.直接掛載腳本
這種情況下蛉幸,fileID的值永遠(yuǎn)是11500000破讨,它實際上指的是MonoScript類型組件,它的值由ClassID * 10000所得奕纫,詳情見官方文檔,而guid能直接定位到MonoScript腳本文件
所以它是通過guid找到腳本文件提陶,然后掛載這個腳本中與腳本文件名相同的類
b.直接掛載DLL中的腳本
這種情況下,fileID的值是由"s\0\0\0" + type.Namespace + type.Name的值轉(zhuǎn)換成MD4的值匹层,guid指的是這個dll所對應(yīng)的文件(MD4的算法貼在最后面)
所以它是通過guid找到dll文件隙笆,然后生成里面所有類的MD4,然后和這個值做比對升筏,相同的則掛載上去
腳本引用修復(fù)
一撑柔、原來是掛的腳本(可以通過fileID等于1150000判斷),要替換成新的腳本
a.把prefab中的guid通過文本工具統(tǒng)一替換成新腳本的guid
b.直接把新腳本的.mate文件里面的guid改成prefab中的
c.如果新腳本已經(jīng)有地方有的您访,不能改.mate铅忿,就只能使用a方法
二、原來是掛的DLL中的腳本(可以通過fileID不等于1150000判斷)灵汪,要替換成新的腳本
a.把prefab中的guid改成新腳本的guid,把fileID統(tǒng)一改成1150000
三檀训、原來是掛的腳本柑潦,要替換成dll中的腳本
a.把prefab中的guid改成dll的guid,把fileID統(tǒng)一改成DLL中類的Type算出來的MD4的值
四、原來是掛的DLL中的腳本峻凫,要替換成新DLL中的腳本
a.把prefab中的guid改成新dll的guid,把fileID統(tǒng)一改成新DLL中類的Type算出來的MD4的值
注:如寫工具時渗鬼,要獲得一個本地腳本中包含哪一些類,可以使用AssetDatabase.Load荧琼,它返回的是一個MonoScript譬胎,通過它可以獲得到所有類的信息
MD4算法如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
namespace cn.crashByNull
{
public class MD4 : HashAlgorithm
{
private uint _a;
private uint _b;
private uint _c;
private uint _d;
private uint[] _x;
private int _bytesProcessed;
public MD4()
{
_x = new uint[16];
Initialize();
}
public override void Initialize()
{
_a = 0x67452301;
_b = 0xefcdab89;
_c = 0x98badcfe;
_d = 0x10325476;
_bytesProcessed = 0;
}
protected override void HashCore(byte[] array, int offset, int length)
{
ProcessMessage(Bytes(array, offset, length));
}
protected override byte[] HashFinal()
{
try
{
ProcessMessage(Padding());
return new[] { _a, _b, _c, _d }.SelectMany(word => Bytes(word)).ToArray();
}
finally
{
Initialize();
}
}
private void ProcessMessage(IEnumerable
bytes)
{
foreach (byte b in bytes)
{
int c = _bytesProcessed & 63;
int i = c >> 2;
int s = (c & 3) << 3;
_x[i] = (_x[i] & ~((uint)255 << s)) | ((uint)b << s);
if (c == 63)
{
Process16WordBlock();
}
_bytesProcessed++;
}
}
private static IEnumerable
Bytes(byte[] bytes, int offset, int length)
{
for (int i = offset; i < length; i++)
{
yield return bytes[i];
}
}
private IEnumerable
Bytes(uint word)
{
yield return (byte)(word & 255);
yield return (byte)((word >> 8) & 255);
yield return (byte)((word >> 16) & 255);
yield return (byte)((word >> 24) & 255);
}
private IEnumerable
Repeat(byte value, int count)
{
for (int i = 0; i < count; i++)
{
yield return value;
}
}
private IEnumerable
Padding() { return Repeat(128, 1) .Concat(Repeat(0, ((_bytesProcessed + 8) & 0x7fffffc0) + 55 - _bytesProcessed)) .Concat(Bytes((uint)_bytesProcessed << 3)) .Concat(Repeat(0, 4)); } private void Process16WordBlock() { uint aa = _a; uint bb = _b; uint cc = _c; uint dd = _d; foreach (int k in new[] { 0, 4, 8, 12 }) { aa = Round1Operation(aa, bb, cc, dd, _x[k], 3); dd = Round1Operation(dd, aa, bb, cc, _x[k + 1], 7); cc = Round1Operation(cc, dd, aa, bb, _x[k + 2], 11); bb = Round1Operation(bb, cc, dd, aa, _x[k + 3], 19); } foreach (int k in new[] { 0, 1, 2, 3 }) { aa = Round2Operation(aa, bb, cc, dd, _x[k], 3); dd = Round2Operation(dd, aa, bb, cc, _x[k + 4], 5); cc = Round2Operation(cc, dd, aa, bb, _x[k + 8], 9); bb = Round2Operation(bb, cc, dd, aa, _x[k + 12], 13); } foreach (int k in new[] { 0, 2, 1, 3 }) { aa = Round3Operation(aa, bb, cc, dd, _x[k], 3); dd = Round3Operation(dd, aa, bb, cc, _x[k + 8], 9); cc = Round3Operation(cc, dd, aa, bb, _x[k + 4], 11); bb = Round3Operation(bb, cc, dd, aa, _x[k + 12], 15); } unchecked { _a += aa; _b += bb; _c += cc; _d += dd; } } private static uint ROL(uint value, int numberOfBits) { return (value << numberOfBits) | (value >> (32 - numberOfBits)); } private static uint Round1Operation(uint a, uint b, uint c, uint d, uint xk, int s) { unchecked { return ROL(a + ((b & c) | (~b & d)) + xk, s); } } private static uint Round2Operation(uint a, uint b, uint c, uint d, uint xk, int s) { unchecked { return ROL(a + ((b & c) | (b & d) | (c & d)) + xk + 0x5a827999, s); } } private static uint Round3Operation(uint a, uint b, uint c, uint d, uint xk, int s) { unchecked { return ROL(a + (b ^ c ^ d) + xk + 0x6ed9eba1, s); } } } public static class FileIDUtil { public static int Compute(Type t) { string toBeHashed = "s\0\0\0" + t.Namespace + t.Name; using (HashAlgorithm hash = new MD4()) { byte[] hashed = hash.ComputeHash(System.Text.Encoding.UTF8.GetBytes(toBeHashed)); int result = 0; for (int i = 3; i >= 0; --i) { result <<= 8; result |= hashed[i]; } return result; } } } }
原文鏈接:https://blog.csdn.net/gz_huangzl/article/details/52486509