Hi @Yoraiz0r !
I had a read through your post and I can see you hit a lot of walls along the way, sorry to hear that!
Since reading this post, I tried 2 different approaches (one of them failed and the 2nd one seems like it could work).
The first failed approach was to use a SerializedObject and try to wrap a Texture in it, such that I could Add a property to it. There’s no AddProperty method to SerializedObject, but CopyFromSerializedProperty does exist and I tried adding a new serialized property that way. I had it compiling, and then when I ran it I got the error:
Destination property could not be found
Which means that a new property can’t be applied, which is a bit of a downer.
So, anyways, I looked through the codebase and found we could do something with the following APIs:
- AssetImporter.AddRemap
- AssetImporter.GetExternalObjectMap
The idea here, is to add a reference to another object, such that we could load the object if needed (and the object could be anything we want, like a ScriptableObject or a Texture).
I’ve got some code working here, where we can add a reference to a texture for a specific Model:
public class ExternalReferenceTest
{
[MenuItem("AssetDatabase/VerifyExternalReferences")]
public static void VerifyExternalReferences()
{
var texturePath = "Assets/test.png";
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(texturePath);
var fbxPath = "Assets/coloredCube.fbx";
var allFbxAssets = AssetDatabase.LoadAllAssetsAtPath(fbxPath);
var fbxImporter = (ModelImporter)AssetImporter.GetAtPath(fbxPath);
var textureCounter = 0;
var externalReferences = fbxImporter.GetExternalObjectMap();
foreach (var curRef in externalReferences)
{
if (curRef.Value is Texture2D)
{
Debug.Log($"Found reference to {curRef.Value.name}");
textureCounter++;
}
}
if(textureCounter == 0)
Debug.Log("Found no textures");
fbxImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(texture), texture);
fbxImporter.SaveAndReimport();
externalReferences = fbxImporter.GetExternalObjectMap();
foreach (var curRef in externalReferences)
{
if(curRef.Value is Texture2D)
Debug.Log($"Found reference to {curRef.Value.name}");
}
}
}
If you look at the corresponding .meta file (in my case coloredCube.fbx.meta) you’ll notice that the externalObjects array has been populated. For example, it would look like this:
ModelImporter:
serializedVersion: 22200
internalIDToNameTable: []
externalObjects:
- first:
type: UnityEngine:Texture2D
assembly: UnityEngine.CoreModule
name: test
second: {fileID: 2800000, guid: d38ec503f07e72140b7fa54c8abed249, type: 3}
This way we can add extra typed data to your objects, without having to mess with the userData.
Does this help?