【发布时间】:2017-01-11 22:25:32
【问题描述】:
我一直在尝试在我正在制作的编辑器脚本中实现一个简单的 Resources.Load 调用,但尽管我尝试了许多不同的方法,但它仍然返回 null。
目的
-
用户将 .jpg 文件添加到项目中的文件夹中,在本例中为 Assets/Resources/360Photos
后处理脚本检测到此文件,对其应用立方体贴图纹理导入设置
然后脚本将创建天空盒/立方体贴图材质并将纹理应用到材质
我遇到的障碍是在将纹理对象作为立方体贴图进行后期处理后获取纹理对象,以及如何将其应用于 Skybox/Cubemap 着色材质的 _Tex 属性,因为我什至无法理解将与我导入和处理的纹理关联的资源加载到立方体贴图。
是否可以在 Unity 编辑器脚本(尤其是 AssetPostProcessor)中使用 Resources.Load,或者我是否尝试执行仅在运行时可用的功能?
如果有人可以查看我的代码并查看我提供的 Unity 屏幕截图,那将不胜感激。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class Postprocess360Photo : AssetPostprocessor {
void OnPostprocessTexture(Texture2D texture)
{
string lowerCaseAssetPath = assetPath.ToLower ();
bool isIn360PhotoDirectory = lowerCaseAssetPath.IndexOf ("360photos") != -1;
if (isIn360PhotoDirectory)
{
TextureImporter textureImporter = (TextureImporter)assetImporter;
textureImporter.textureType = TextureImporterType.Default;
textureImporter.textureShape = TextureImporterShape.TextureCube;
textureImporter.generateCubemap = TextureImporterGenerateCubemap.Cylindrical;
textureImporter.sRGBTexture = true;
textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;
textureImporter.alphaIsTransparency = true;
textureImporter.npotScale = TextureImporterNPOTScale.ToSmaller;
textureImporter.isReadable = true;
textureImporter.mipmapEnabled = false;
textureImporter.wrapMode = TextureWrapMode.Clamp;
textureImporter.filterMode = FilterMode.Bilinear;
}
AssetDatabase.ImportAsset (assetPath);
CreateMaterial ();
}
void CreateMaterial ()
{
Cubemap cubemap = (Cubemap)Resources.Load ("360Photos/FrontDriveway");
Debug.Log (cubemap);
}
}
空返回值的层次结构和控制台验证请看图片:-
如果有帮助,请使用 Unity 5.5.0f3。
【问题讨论】:
-
你可以试试
Cubemap cubemap = Resources.Load("360Photos/FrontDriveway") as Cubemap ;并在检查器中检查你的资源的“纹理类型”是“立方体贴图” -
嗨@Chong,感谢您的回复。我按你说的试了代码,还是没有结果。检查器纹理类型中不再有“立方体贴图”类型,我认为他们用 Unity 5.5 改变了它,因此类型是默认的,纹理形状为立方体。如果需要,请查看图片链接:- imgur.com/a/A4aYj
-
FrontDriveway 文件的格式/扩展名是什么?另外,这是您现在在项目中使用的确切代码吗?
-
嗨@Programmer,是的,这是目前正在使用的确切代码。扩展名是 .jpg,但是尝试使用 .jpg 和不使用扩展名会产生相同的结果。为了简洁起见,我只是将它留在了当前的脚本副本中。
-
我在想可能是因为它只是 Unity 中的立方体贴图,而不是文件结构中的立方体贴图,所以我可能无法在立方体贴图上执行 Resources.Load?