public static Material LoadUnityMaterial(string materialPath)
		{
			if(materialPath.StartsWith(HEU_Defines.DEFAULT_UNITY_BUILTIN_RESOURCES))
			{
				return HEU_AssetDatabase.LoadUnityAssetFromUniqueAssetPath<Material>(materialPath);
			}
			else if (materialPath.StartsWith("Packages/"))
			{
				return HEU_AssetDatabase.LoadAssetAtPath(materialPath, typeof(Material)) as Material;
			}

			string relativePath = materialPath;
			if (relativePath.StartsWith(Application.dataPath))
			{
				// If absolute path, change to relative path
				relativePath = HEU_AssetDatabase.GetAssetRelativePath(materialPath);
			}

			Material material = null;

			string mainPath = null;
			string subPath = null;
			HEU_AssetDatabase.GetSubAssetPathFromPath(relativePath, out mainPath, out subPath);
			if(subPath != null)
			{
				// This is a subasset. Need to find it by first loading the main asset.
				Object subObject = HEU_AssetDatabase.LoadSubAssetAtPath(mainPath, subPath);
				if(subObject != null)
				{
					material = subObject as Material;
				}
			}
			else
			{
				// Try loading from Resources first
				material = Resources.Load<Material>(relativePath) as Material;
				if (material == null)
				{
					// If not in Resources, try loading from project
					HEU_AssetDatabase.ImportAsset(relativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default);
					material = HEU_AssetDatabase.LoadAssetAtPath(relativePath, typeof(Material)) as Material;
				}
			}

			return material;
		}
		public static Texture2D ExtractHoudiniImageToTextureFile(HEU_SessionBase session, HAPI_MaterialInfo materialInfo, string imagePlanes, string assetCacheFolderPath)
		{
			Texture2D textureResult = null;

			// Get the Textures folder in the assetCacheFolderPath. Make sure it exists.
			assetCacheFolderPath = HEU_AssetDatabase.AppendTexturesPathToAssetFolder(assetCacheFolderPath);
			HEU_AssetDatabase.CreatePathWithFolders(assetCacheFolderPath);

			// Need to pass in full path to Houdini to write out the file
			assetCacheFolderPath = HEU_AssetDatabase.GetAssetFullPath(assetCacheFolderPath);
			if (assetCacheFolderPath == null)
			{
				return textureResult;
			}

			HAPI_ImageInfo imageInfo = new HAPI_ImageInfo();
			if (!session.GetImageInfo(materialInfo.nodeId, ref imageInfo))
			{
				return textureResult;
			}

			// This will return null if the current imageInfo file format is supported by Unity, otherwise
			// returns a Unity supported file format.
			string desiredFileFormatName = HEU_MaterialData.GetSupportedFileFormat(session, ref imageInfo);

			// Extract image to file
			string writtenFilePath = null;
			if (!session.ExtractImageToFile(materialInfo.nodeId, desiredFileFormatName, imagePlanes, assetCacheFolderPath, out writtenFilePath))
			{
				return textureResult;
			}
			

			HEU_AssetDatabase.SaveAndRefreshDatabase();

			// Convert full path back to relative in order to work with AssetDatabase
			string assetRelativePath = HEU_AssetDatabase.GetAssetRelativePath(writtenFilePath);

			// Re-import to refresh the project
			HEU_AssetDatabase.ImportAsset(assetRelativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default);

			textureResult = HEU_AssetDatabase.LoadAssetAtPath(assetRelativePath, typeof(Texture2D)) as Texture2D;
			//Debug.LogFormat("Loaded texture to file {0} with format {1}", writtenFilePath, textureResult != null ? textureResult.format.ToString() : "none");

			return textureResult;
		}
		/// <summary>
		/// Returns the valid relative path if the given path is under the project Assets/ directory.
		/// If not, it returns the given path as is, but warns the user that the path is outside the project
		/// and therefore not safe to use if the project changes location on another workstation (eg. source control).
		/// </summary>
		/// <param name="inPath">Path to validate.</param>
		/// <returns>Relative path to project Assets/ folder, or just returns the path as is if outside the project.</returns>
		public static string GetValidRelativePath(string inPath)
		{
			// If the selected path is outside the project directory, warn user
			// If inside project, make sure its relative (for version control and multi-user projects)
			if (!HEU_AssetDatabase.IsPathRelativeToAssets(inPath))
			{
				string relativePath = HEU_AssetDatabase.GetAssetRelativePath(inPath);
				if (string.IsNullOrEmpty(relativePath))
				{
					string message = string.Format("Path: {0} is outside the project!\n This is not recommended if using version control or for multi-user projects.",
						inPath);
					Debug.LogWarning(message);
				}
				else
				{
					inPath = relativePath;
				}
			}
			return inPath;
		}
Beispiel #4
0
		public static Material LoadUnityMaterial(string materialPath)
		{
			if(materialPath.StartsWith(HEU_Defines.DEFAULT_UNITY_BUILTIN_RESOURCES))
			{
				return HEU_AssetDatabase.LoadUnityAssetFromUniqueAssetPath<Material>(materialPath);
			}

			string relativePath = materialPath;
			if (relativePath.StartsWith(Application.dataPath))
			{
				// If absolute path, change to relative path
				relativePath = HEU_AssetDatabase.GetAssetRelativePath(materialPath);
			}

			// Try loading from Resources first
			Material material = Resources.Load<Material>(relativePath) as Material;
			if(material == null)
			{
				// If not in Resources, try loading from project
				HEU_AssetDatabase.ImportAsset(relativePath, HEU_AssetDatabase.HEU_ImportAssetOptions.Default);
				material = HEU_AssetDatabase.LoadAssetAtPath(relativePath, typeof(Material)) as Material;
			}
			return material;
		}