public override void OnImportAsset(AssetImportContext ctx)
        {
            try
            {
                ctx.DependsOnCustomDependency("EntityBinaryFileFormatVersion");

                var sceneWithBuildConfiguration = SceneWithBuildConfigurationGUIDs.ReadFromFile(ctx.assetPath);

                // Ensure we have as many dependencies as possible registered early in case an exception is thrown
                var scenePath = AssetDatabase.GUIDToAssetPath(sceneWithBuildConfiguration.SceneGUID.ToString());
                ctx.DependsOnSourceAsset(scenePath);

                if (sceneWithBuildConfiguration.BuildConfiguration.IsValid)
                {
                    var buildConfigurationPath = AssetDatabase.GUIDToAssetPath(sceneWithBuildConfiguration.BuildConfiguration.ToString());
                    ctx.DependsOnSourceAsset(buildConfigurationPath);
                    var buildConfigurationDependencies = AssetDatabase.GetDependencies(buildConfigurationPath);
                    foreach (var dependency in buildConfigurationDependencies)
                    {
                        ctx.DependsOnSourceAsset(dependency);
                    }
                }

                var dependencies = AssetDatabase.GetDependencies(scenePath);
                foreach (var dependency in dependencies)
                {
                    if (dependency.ToLower().EndsWith(".prefab"))
                    {
                        ctx.DependsOnSourceAsset(dependency);
                    }
                }

                var config = BuildConfiguration.LoadAsset(sceneWithBuildConfiguration.BuildConfiguration);

                var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);

                try
                {
                    var settings = new GameObjectConversionSettings();

                    settings.SceneGUID          = sceneWithBuildConfiguration.SceneGUID;
                    settings.BuildConfiguration = config;
                    settings.AssetImportContext = ctx;

                    var sectionRefObjs = new List <ReferencedUnityObjects>();
                    var sectionData    = EditorEntityScenes.ConvertAndWriteEntityScene(scene, settings, sectionRefObjs);
                    WriteRefGuids(sectionRefObjs, sectionData, ctx);
                }
                finally
                {
                    EditorSceneManager.CloseScene(scene, true);
                }
            }
            // Currently it's not acceptable to let the asset database catch the exception since it will create a default asset without any dependencies
            // This means a reimport will not be triggered if the scene is subsequently modified
            catch (Exception e)
            {
                Debug.Log($"Exception thrown during SubScene import: {e}");
            }
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            try
            {
                var sceneWithBuildSettings = ReadSceneWithBuildSettings(ctx.assetPath);

                // Ensure we have as many dependencies as possible registered early in case an exception is thrown
                var scenePath = AssetDatabase.GUIDToAssetPath(sceneWithBuildSettings.SceneGUID.ToString());
                ctx.DependsOnSourceAsset(scenePath);

                if (sceneWithBuildSettings.BuildSettings.IsValid)
                {
                    var buildSettingPath = AssetDatabase.GUIDToAssetPath(sceneWithBuildSettings.BuildSettings.ToString());
                    ctx.DependsOnSourceAsset(buildSettingPath);
                    var buildSettingDependencies = AssetDatabase.GetDependencies(buildSettingPath);
                    foreach (var dependency in buildSettingDependencies)
                    {
                        ctx.DependsOnSourceAsset(dependency);
                    }
                }

                var dependencies = AssetDatabase.GetDependencies(scenePath);
                foreach (var dependency in dependencies)
                {
                    if (dependency.ToLower().EndsWith(".prefab"))
                    {
                        ctx.DependsOnSourceAsset(dependency);
                    }
                }

                var buildSettings = BuildSettings.LoadBuildSettings(sceneWithBuildSettings.BuildSettings);

                var scene = EditorSceneManager.OpenScene(scenePath, OpenSceneMode.Additive);

                try
                {
                    var settings = new GameObjectConversionSettings();

                    settings.SceneGUID          = sceneWithBuildSettings.SceneGUID;
                    settings.BuildSettings      = buildSettings;
                    settings.AssetImportContext = ctx;

                    EditorEntityScenes.WriteEntityScene(scene, settings);
                }
                finally
                {
                    EditorSceneManager.CloseScene(scene, true);
                }
            }
            // Currently it's not acceptable to let the asset database catch the exception since it will create a default asset without any dependencies
            // This means a reimport will not be triggered if the scene is subsequently modified
            catch (Exception e)
            {
                Debug.Log($"Exception thrown during SubScene import: {e.Message}");
            }
        }
Beispiel #3
0
 static void RegisterDependencies(AssetImportContext importContext, ConversionDependencies dependencies)
 {
     using (var assets = dependencies.AssetDependentsByInstanceId.GetKeyArray(Allocator.Temp))
     {
         for (int i = 0; i < assets.Length; i++)
         {
             var asset = EditorUtility.InstanceIDToObject(assets[i]);
             importContext.DependsOnSourceAsset(AssetDatabase.GetAssetPath(asset));
         }
     }
 }
Beispiel #4
0
        static void RegisterDependencies(AssetImportContext importContext, ConversionDependencies dependencies)
        {
            using (var assets = dependencies.AssetDependentsByInstanceId.GetKeyArray(Allocator.Temp))
            {
                for (int i = 0; i < assets.Length; i++)
                {
                    var asset = EditorUtility.InstanceIDToObject(assets[i]);
                    if (asset == null)
                    {
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on instance ID {assets[i]} - this instance ID does not correspond to an object.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg);
                        continue;
                    }

                    var path = AssetDatabase.GetAssetPath(asset);
                    if (string.IsNullOrEmpty(path))
                    {
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on object {asset.name}. This object does not have a valid asset path.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg, asset);
                        continue;
                    }

                    var guid = new GUID(AssetDatabase.AssetPathToGUID(path));
                    if (GUIDHelper.IsBuiltinAsset(in guid))
                    {
                        // AssetImportContext does not support dependencies on inbuilt assets
                        continue;
                    }

                    if (guid.Empty())
                    {
                        // This should never happen
                        var    dependents = FormatDependents(assets[i]);
                        string errorMsg   =
                            $"Invalid asset dependency on object {asset.name} at path {path}. It doesn't have a valid GUID.\n" +
                            "This dependency was registered by: " + dependents;
                        Debug.LogWarning(errorMsg, asset);
                        continue;
                    }

                    importContext.DependsOnSourceAsset(path);
                }
            }

            string FormatDependents(int assetInstance)
            {
                var    iter = dependencies.AssetDependentsByInstanceId.GetValuesForKey(assetInstance);
                string deps = "";

                while (iter.MoveNext())
                {
                    if (deps.Length > 0)
                    {
                        deps += ", ";
                    }
                    var obj = EditorUtility.InstanceIDToObject(iter.Current);
                    deps += $"{(obj == null ? "NULL" : obj.name)}";
                }

                return(deps);
            }
        }