2017-01-17 3 views
0

エディタスクリプトでマテリアルテクスチャを変更しています。一度実行すると、エディタウィンドウに変更が表示されますが、シーンやヒットプレイを保存すると変更内容は失われます。エディタスクリプトからの材料の変更が保存時に失われる

var mySkyBox = Resources.Load<Material>("Materials/MySkyBox"); 
Undo.RecordObject(mySkyBox, "setting mySkyBox texture"); 
var texture = new Texture2D(2, 2); 
texture.LoadImage(File.ReadAllBytes("texturePath")); 
mySkyBox.SetTexture("_UpTex", texture); 
EditorUtility.SetDirty(mySkyBox); 

私は、材料をロードするためにLoadMainAssetAtPathを使用して試してみましたが、それはどちらか動作しません:

は、ここに私のコードです。

私は私が正しくエディタスクリプトの材料を編集し、変更が持続することができどのよう5.3.7f1

ユニティを使用していますか?

+0

EditorSceneManager.MarkAllScenesDirtyは()もねえ、それはあなたが 'を呼び出した後団結はそれを保存しないことを面白そう – lockstock

+0

動作しません。 SetDirty'。しかし、あなたは 'AssetDatabase.SaveAssets();'を呼び出すことができます - それは助けるかもしれません。 アセットを読み込むために 'AssetDatabase'を使うこともできます: https://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAssetAtPath.html – zhekazheka

答えて

0
Material tempSkyBox = Resources.Load<Material>("Materials/MySkyBox"); 
// this will make a copy from the loaded material 

tempSkyBox.SetTexture("_MainTex",(Texture2D) AssetDatabase.LoadAssetAtPath("texturePath", typeof(Texture2D))); 
// Sets the main texture on the copy of the material that you just made 
// Make sure that the image file is located somewhere in the unityproject folder 

AssetDatabase.CreateAsset(tempSkyBox, "materialPath" + "/mySkybox.mat"); 
// Saves the copy of the material to the disk 

AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); 
// force the editor to update the AssetsFolder 

myCamera.GetComponentInChildren<Skybox>().material = tempSkyBox; 
// Sets the reference 

この方法は、私のプロジェクトの1 幸運に私のために働いた^^

関連する問題