/// <summary> /// Loads a copy of the srcAsset at copyPath. Creates a copy if not found. /// </summary> /// <param name="srcAsset">The source asset object</param> /// <param name="copyPath">The full path to the copy</param> /// <param name="type">The type of source asset</param> /// <param name="bOverwriteExisting">Whether to overwrite existing copy if found</param> /// <returns>Returns loaded copy if exists or created, otherwise null</returns> public static Object CopyAndLoadAssetAtAnyPath(Object srcAsset, string copyPath, System.Type type, bool bOverwriteExisting) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath)) { CreatePathWithFolders(copyPath); string fileName = HEU_Platform.GetFileName(srcAssetPath); string fullCopyPath = HEU_Platform.BuildPath(copyPath, fileName); if ((!bOverwriteExisting && HEU_Platform.DoesFileExist(fullCopyPath)) || CopyAsset(srcAssetPath, fullCopyPath)) { // Refresh database as otherwise we won't be able to load it in the next line. SaveAndRefreshDatabase(); return LoadAssetAtPath(fullCopyPath, type); } else { Debug.LogErrorFormat("Failed to copy and load asset from {0} to {1}!", srcAssetPath, fullCopyPath); } } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? Debug.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Copy the file of the given srcAsset into the given targetPath, which must be absolute. /// If targetPath doesn't have a file name, the srcAsset's file name will be used. /// </summary> /// <param name="srcAsset">Source asset to copy</param> /// <param name="targetPath">Absolute path of destination</param> /// <param name="type">Type of the asset</param> /// <returns></returns> public static Object CopyAndLoadAssetAtGivenPath(Object srcAsset, string targetPath, System.Type type) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath)) { string targetFolderPath = HEU_Platform.GetFolderPath(targetPath); CreatePathWithFolders(targetFolderPath); string targetFileName = HEU_Platform.GetFileName(targetPath); if (string.IsNullOrEmpty(targetFileName)) { Debug.LogErrorFormat("Copying asset failed. Destination path must end with a file name: {0}!", targetPath); return null; } if (CopyAsset(srcAssetPath, targetPath)) { // Refresh database as otherwise we won't be able to load it in the next line. SaveAndRefreshDatabase(); return LoadAssetAtPath(targetPath, type); } else { Debug.LogErrorFormat("Failed to copy and load asset from {0} to {1}!", srcAssetPath, targetPath); } } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? Debug.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
public static HEU_MaterialData CreateUnitySubstanceMaterialData(int materialKey, string materialPath, string substanceName, int substanceIndex, List<HEU_MaterialData> materialCache, string assetCacheFolderPath) { // Let's make sure we can find the Unity or Substance material first Material material = null; HEU_MaterialData.Source sourceType = HEU_MaterialData.Source.UNITY; if (!string.IsNullOrEmpty(substanceName)) { sourceType = HEU_MaterialData.Source.SUBSTANCE; material = HEU_MaterialFactory.LoadSubstanceMaterialWithName(materialPath, substanceName); } else if (substanceIndex >= 0) { sourceType = HEU_MaterialData.Source.SUBSTANCE; material = HEU_MaterialFactory.LoadSubstanceMaterialWithIndex(materialPath, substanceIndex); } else if (!string.IsNullOrEmpty(materialPath)) { material = HEU_MaterialFactory.LoadUnityMaterial(materialPath); } if (material != null) { HEU_MaterialData materialData = ScriptableObject.CreateInstance<HEU_MaterialData>(); materialData._materialSource = sourceType; materialData._materialKey = materialKey; materialData._material = material; materialCache.Add(materialData); return materialData; } else { // We can't find the material in Unity, so notify user and use a default one which allows to at least get the geometry in. if (string.IsNullOrEmpty(materialPath)) { HEU_Logger.LogWarningFormat("Empty material name found. Using default material."); } else if (materialPath.Contains("Resources/unity_builtin_extra")) { // Built in material. Don't display error. } else { HEU_Logger.LogErrorFormat("Unable to find {0} material {1}. Using a default material instead. Please check material exists in project and reload asset!", sourceType, materialPath); } // The materialKey helps uniquely identify this material for further look ups. But we also need to get a valid file name // to create the default material, so strip out just the file name. string strippedFileName = HEU_Platform.GetFileName(materialPath); return CreateMaterialInCache(materialKey, strippedFileName, HEU_MaterialData.Source.UNITY, false, materialCache, assetCacheFolderPath); } }
public static string CreateUniqueBakePath(string assetName) { #if UNITY_EDITOR string assetBakedPath = GetAssetBakedPathWithAssetName(assetName); assetBakedPath = AssetDatabase.GenerateUniqueAssetPath(assetBakedPath); string folderName = HEU_Platform.GetFileName(assetBakedPath); AssetDatabase.CreateFolder(assetBakedPath, folderName); return assetBakedPath; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? Debug.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }
/// <summary> /// Loads a copy of the given asset. Creates the copy if not found. /// </summary> /// <param name="srcAsset">Source asset whose copy will be loaded (and created if no copy exists).</param> /// <param name="newAssetFolderPath">Folder of to look for copy or create in</param> /// <param name="type">Type of asset</param> /// <returns>Loaded copy of the asset</returns> public static Object LoadAssetCopy(Object srcAsset, string newAssetFolderPath, System.Type type, bool bOverwriteExisting) { #if UNITY_EDITOR string srcAssetPath = GetAssetPath(srcAsset); if (!string.IsNullOrEmpty(srcAssetPath) && IsPathInAssetCache(srcAssetPath)) { string subFolderPath = newAssetFolderPath; if(type == typeof(Material)) { subFolderPath = AppendMaterialsPathToAssetFolder(newAssetFolderPath); } else if(type == typeof(Texture)) { subFolderPath = AppendTexturesPathToAssetFolder(newAssetFolderPath); } else if (type == typeof(Mesh)) { subFolderPath = AppendMeshesPathToAssetFolder(newAssetFolderPath); } else if (type == typeof(TerrainData)) { subFolderPath = AppendTerrainPathToAssetFolder(newAssetFolderPath); } CreatePathWithFolders(subFolderPath); string fileName = HEU_Platform.GetFileName(srcAssetPath); string newAssetPath = HEU_Platform.BuildPath(subFolderPath, fileName); if ((!bOverwriteExisting && HEU_Platform.DoesFileExist(newAssetPath)) || CopyAsset(srcAssetPath, newAssetPath)) { // Refresh database as otherwise we won't be able to load it in the next line. SaveAndRefreshDatabase(); return LoadAssetAtPath(newAssetPath, type); } else { Debug.LogErrorFormat("Failed to copy and load asset from {0} to {1}!", srcAssetPath, newAssetPath); } } return null; #else // TODO RUNTIME: AssetDatabase is not supported at runtime. Do we need to support this for runtime? Debug.LogWarning(HEU_Defines.HEU_USERMSG_NONEDITOR_NOT_SUPPORTED); return null; #endif }