2017-01-17 7 views
0

Ich ändere eine Material Textur in einem Editor-Skript. Nach dem Ausführen kann ich die Änderungen im Editor-Fenster sehen, aber wenn ich meine Szene speichere oder spiele, sind die Änderungen verloren.Materialänderungen von Editor-Skript gehen beim Speichern verloren

Hier ist mein Code:

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); 

Ich habe auch versucht LoadMainAssetAtPath mit dem Material zu laden, aber das funktioniert auch nicht.

Ich bin mit Unity 5.3.7f1

Wie ich ein Material in einem Editor Skript korrekt bearbeiten kann und die Änderungen bestehen bleiben?

+0

EditorSceneManager.MarkAllScenesDirty() auch nicht – lockstock

+0

Hey funktioniert, klingt es interessant, dass die Einheit der es nicht speichern, nachdem Sie 'nennen SetDirty'. Aber Sie können versuchen, 'AssetDatabase.SaveAssets();' aufzurufen - es könnte helfen. Sie können auch versuchen, AssetDatabase zu verwenden, um ein Asset zu laden: https://docs.unity3d.com/ScriptReference/AssetDatabase.LoadAssetAtPath.html – zhekazheka

Antwort

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 

diese Methode funktioniert hat für mich in einem meiner Projekte Viel Glück ^^

Verwandte Themen