2017-01-11 8 views
1

私は作っているエディタスクリプトで単純なResources.Load呼び出しを実装しようとしていましたが、私が試した数多くのアプローチにもかかわらずnullが返され続けます。Resources.Loadはエディタスクリプトでnullを返します

目的

  • ユーザーは、この場合には、プロジェクト内のフォルダに.JPG年代を追加資産/リソース/ 360Photos

      後処理スクリプトがこのファイルを検出

    • 、キューブマップテクスチャのインポートを適用します

    • スクリプトはskybox/cubemapマテリアルを作成し、テクスチャtを適用しますそれはポストキューブマップとして処理されており、どのようにそれがその後、スカイボックス/キューブマップ日陰の_Texプロパティに適用される後の材料

oを私がヒットしているバリケードは、テクスチャオブジェクトを取得していますマテリアルは、インポートしてキューブマップに処理するテクスチャに関連付けられたリソースをロードすることさえできないようです。

Unity Editor Scripting(特に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); 
} 
} 

ヌル戻り値の階層構造とコンソール検証のための画像をご参照ください: - それは場合に役立ちますユニティ5.5.0f3を使用して

http://imgur.com/a/ruNhc

+0

あなたはキューブマップとして 'キューブマップキューブマップ= Resources.Load(「360Photos/FrontDriveway」)を試みることができる;'そして、あなたのリソースの「テクスチャタイプは、」「キューブマップ」 – Chong

+0

こんにちは@Chong、であることを検査官に確認してくださいあなたの応答に感謝します。 あなたが言ったようにコードを試しましたが、まだ結果はありませんでした。インスペクタのテクスチャタイプにもう "Cubemap"という型がなくなり、Unity 5.5でそれを変更したと思います。そのような型は、テクスチャシェイプオブキューブでDefaultです。 必要に応じて画像リンクをご覧ください: - http://imgur.com/a/A4aYj – AndrewMMG

+0

FrontDrivewayファイルのフォーマット/拡張子とは何ですか?また、これはあなたのプロジェクトで現在使用しているコードですか? – Programmer

答えて

0

拡張子は.jpgですが、.jpgと拡張子が でないと同じ結果が得られません。 .cubemap

.JPG!=問題だ

。キューブマップは資産に行く取得するには

- >レガシー - - >キューブマップ>を作成します。

あなたはFrontDrivewayそれを名前を付け、動作するはずです/ 360Photos資源、でそれを配置した場合。

EDIT

私は上記の言ったことには例外があります。TextureImportergenerateCubemaptextureShapeのプロパティを変更すると、画像はCubemapとしてインポートできます。

ユニティの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

あなたは私を失った。私の答えは、キューブマップ( '(Cubemap)Resources.Load(" ... ")')にjpgをキャストできないということです。 skyboxにはこれが必要ですか?スカイボックスの素材を作成するには? – Programmer

関連する問題