2017-01-11 27 views
1

我一直試圖在編輯器腳本中實現一個簡單的Resources.Load調用,但它仍繼續返回null,儘管我嘗試了許多不同的方法。Resources.Load在編輯器腳本中返回null

目的

  • 用戶增加.JPG的一個文件夾中的項目,在這種情況下資產/資源/ 360Photos

    • 後處理腳本檢測該文件,適用立方體貼圖進口設置到它

    • 腳本將創建一個天空盒/立方體貼圖材質並應用紋理t ○材料

我已擊中越來越紋理對象它已經經過後處理的作爲一個立方體貼圖,以及如何將被應用到空中包廂/立方體貼圖陰影的_Tex屬性的路障材質,因爲我似乎甚至無法加載與我導入並處理到立方體貼圖的紋理相關的資源。

是否有可能在Unity編輯器腳本中使用Resources.Load(特別是AssetPostProcessor)還是我試圖執行僅在運行時可用的功能?

如果有人可以查看我的代碼,並查看我提供的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); 
} 
} 

請參閱圖像的層次結構,並返回null值的控制檯驗證: -

http://imgur.com/a/ruNhc

使用Unity 5.5.0f3是否有幫助。

+0

可以嘗試'立方體貼圖立方體貼圖= Resources.Load(「360Photos/FrontDriveway」)作爲立方體貼圖;'而在檢查檢查你的資源「紋理類型」爲「立方體貼圖」 – Chong

+0

嗨@Chong,感謝您的答覆。 我按照你所說的嘗試了代碼,但仍然沒有結果。檢查器紋理類型中不再有類型「Cubemap」,我認爲他們改變了Unity 5.5,並且類型爲默認值,具有立方體的紋理形狀。 如果需要,請參閱圖像鏈接: - http://imgur.com/a/A4aYj – AndrewMMG

+0

FrontDriveway文件的格式/擴展名是什麼?另外,這是您現在在項目中使用的確切代碼嗎? – Programmer

回答

0

擴展名爲.jpg,但使用.jpg和不使用 擴展名時會產生相同的結果。

這就是問題所在

.JPG!=.cubemap

爲了得到一個立方體貼圖去資產 - >創建 - >傳統 - >立方體貼圖

如果你的名字FrontDriveway,並將其放置在資源/ 360Photos,應該工作。

編輯

有一個例外,就是我上面說的。如果您更改TextureImportergenerateCubemaptextureShape屬性,則可以將圖像導入爲Cubemap

看起來像Unity的Resources.Load函數存在一個錯誤。您不能從OnPostprocessTexture函數調用Resources.Load。也許這是一個Thread的問題?我不知道。

經過很長時間的實驗,我發現了一個解決方法。處理完圖像後,您需要實現EditorApplication.update回調函數並使用它來調用您的Resources.Load代碼。

public class Postprocess360Photo : AssetPostprocessor 
{ 
    EditorApplication.CallbackFunction doneEvent; 
    string path; 

    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); 
     path = assetPath; 

     doneEvent = null; 
     doneEvent = new EditorApplication.CallbackFunction(afterProcessing); 

     //Add doneEvent function to call back! 
     EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Combine(EditorApplication.update, doneEvent); 
    } 

    private void afterProcessing() 
    { 
     //Remove doneEvent function from call back! 
     EditorApplication.update = (EditorApplication.CallbackFunction)Delegate.Remove(EditorApplication.update, doneEvent); 

     Material mat = CreateMaterial(path); 

     //Change skybox? 
     RenderSettings.skybox = mat; 
    } 


    Material CreateMaterial(string filePath) 
    { 
     string fileName = Path.GetFileNameWithoutExtension(filePath); 
     fileName = fileName + ".mat"; 

     /* 
     assetPath/path returns as "Assets/Resources/360Photos/FrontDriveway.jpg" 
     Remove the "Assets/Resources/" 
     */ 
     filePath = filePath.Replace("Assets/Resources/", ""); 

     //Remove the extension name (.jpg) 
     filePath = filePath.Substring(0, filePath.LastIndexOf(".")); 

     // Cubemap cubemap = (Cubemap)Resources.Load("360Photos/FrontDriveway"); 
     Cubemap cubemap = (Cubemap)Resources.Load(filePath); 
     Debug.Log(cubemap); 

     Material skyBoxMat = new Material(Shader.Find("Skybox/Cubemap")); 
     skyBoxMat.SetTexture("_Tex", cubemap); 

     //Optional, saves the material to Assets/Resources/360Photos/" + fileName 
     AssetDatabase.CreateAsset(skyBoxMat, "Assets/Resources/360Photos/" + fileName); 
     AssetDatabase.SaveAssets(); 
     AssetDatabase.Refresh(); 

     return skyBoxMat; 
    } 
} 
+0

這是真的,但是我意識到我應該給出腳本的目的的上下文。我將編輯我的描述,同時這使得一個立方體貼圖,它不會達到我以後的目的。 – AndrewMMG

+0

嗨@Programmer,請參閱我添加到我的問題的目的部分,因爲這將提供一些上下文,爲什麼我試圖在導入設置中訪問立方體貼圖作爲立方體貼圖,而不是6面立方體貼圖遺留文件。 如果你能提供任何見解,我感謝你的幫助我的努力。謝謝。 – AndrewMMG

+0

你迷失了我。我的回答說你不能將jpg轉換爲Cubemap('(Cubemap)Resources.Load(「...」)')。你需要這個天空盒?爲天空盒創建素材? – Programmer