how to get texture Image Contents Hash property.
I want to write a function that can delete the same texture in my project, even those textures content are same, but have different name, just like the texture duplicated from the other one. At the beginning, i use GetPixels() function to get pixels for texture's comparison, but the data is gigantic, then i found "Image Contents Hash" property(Byte[16]) of the texture in Debug mode, and i found even if a pixel change in a texture, this property will change too, so i want to use this property for comparison, now i can get all the textures in the project by script, but i don't know how to get this one. can anyone tell me, i am really appreciate about it. Sorry for my poor English.
Answer by Adam-Mechtley · Sep 28, 2016 at 08:36 AM
@canonner You can use SerializedProperty to get it. What you do with the bytes is up to you! For example:
Texture tex = Selection.activeObject as Texture;
if (tex == null)
{
return;
}
List<byte> bytes = new List<byte> (16);
SerializedProperty sp = new SerializedObject (tex).FindProperty ("m_ImageContentsHash");
while (sp.Next (true) && sp.propertyPath.StartsWith ("m_ImageContentsHash"))
bytes.Add ((byte)sp.intValue);
WOW, i can get it now. i am really appreciate for you help. thanks a lot. God bless you !!!
Note that the serialized value you're accessing here is not really a byte Array, but rather a Hash128— a struct Unity typically uses for deter$$anonymous$$ing whether Assets/AssetBundles. There are 16 bytes, each group of 4 represents a single unsigned int, and the 4 uint's make up the 128 bit hash. Here's an extension method that allows you to get the Hash128 value directly from a SerializedProperty.
public static Hash128 GetHash128Value(this SerializedProperty property)
{
if (property.type != "Hash128")
{
throw new Exception("SerializedProperty does not represent a Hash128 struct.");
}
var bytes = new byte[4][];
for (var i = 0; i < 4; i++)
{
bytes[i] = new byte[4];
for (var j = 0; j < 4; j++)
{
property.Next(true);
bytes[i][j] = (byte)property.intValue;
}
}
var hash = new Hash128(
BitConverter.ToUInt32(bytes[0], 0),
BitConverter.ToUInt32(bytes[1], 0),
BitConverter.ToUInt32(bytes[2], 0),
BitConverter.ToUInt32(bytes[3], 0));
return hash;
}
Answer by srivello · Sep 28, 2016 at 08:21 AM
Hi,
Here is a discussion that shares your proposed solution. Perhaps contacting those authors directly will help you find a solution.
Best wishes!
http://answers.unity3d.com/questions/722645/compare-texture-by-content.html
-Sam
yes, the solution you direct to me looks workable too, i get another workable solution by @Adam-$$anonymous$$echtley. anyway, thanks for you help. Wish you good.!!!