2016-04-29 4 views
7

エディタスクリプトを使用してマテリアルアセットをオーバーライドしています。マテリアルはうまく上書きされます。新しい材料のプロパティが適用されるのがわかります。個々の材料をクリックすると、新しいテクスチャが適用されているのが見えます。しかし、私がプレイしたとき、材料はプリエディット状態にリセットされました。私の材料はすべて元の状態に戻っています。unity3dのマテリアルアセットを変更する

変更をデータベースに保存し、再生したときにどのように変更することができますか?

using UnityEngine; 
using UnityEditor; 
using Newtonsoft.Json; 
using Unify.Utilities; 
using System.Collections.Generic; 
using System; 
using System.IO; 

public class ProcessMaterials : MonoBehaviour 
{ 
    [MenuItem("Unify/ProcessMaterials")] 
    static void UnifyProcessMaterials() 
    { 
    ImportTextures(); 
    ApplyMaterials(); 
    } 

private static void ImportTextures() 
{ 
    // check if folder exists and create one if not 
    if (!AssetDatabase.IsValidFolder("Assets/Resources")) 
    { 
     AssetDatabase.CreateFolder("Assets", "Resources"); 
    } 

    // load settings file 
    TextAsset ta = Resources.Load("UnifySettings") as TextAsset; 
    string json = ta.text; 
    List<List<UnifyObject>> unifyObj = JsonConvert.DeserializeObject<List<List<UnifyObject>>>(json); 
    List<UnifyObject> allMats = unifyObj[3]; 

    // copy textures over to unity folders 
    HashSet<string> uniqueTextures = new HashSet<string>(); 
    foreach (UnifyObject obj in allMats) 
    { 
     if (obj.DiffuseTexture != null && uniqueTextures.Add(obj.DiffuseTexture)) 
     { 
      CopyImageAsset(obj.DiffuseTexture); 
     } 
     if (obj.BumpTexture != null && uniqueTextures.Add(obj.BumpTexture)) 
     { 
      CopyImageAsset(obj.BumpTexture); 
     } 
     if (obj.TransparencyTexture != null && uniqueTextures.Add(obj.TransparencyTexture)) 
     { 
      CopyImageAsset(obj.TransparencyTexture); 
     } 
     if (obj.EnvironmentTexture != null && uniqueTextures.Add(obj.EnvironmentTexture)) 
     { 
      CopyImageAsset(obj.EnvironmentTexture); 
     } 
    } 
} 

private static void CopyImageAsset(string sourceFilePath) 
{ 
    string fileName = "Resources\\" + Path.GetFileName(sourceFilePath); 
    string destFile = Path.Combine(Application.dataPath, fileName); 

    try 
    { 
     File.Copy(sourceFilePath, destFile, true); 
    } 
    catch (Exception) { } 
} 

private static void ApplyMaterials() 
{ 
    TextAsset ta = Resources.Load("UnifySettings") as TextAsset; 
    string json = ta.text; 
    List<List<UnifyObject>> unifyObj = JsonConvert.DeserializeObject<List<List<UnifyObject>>>(json); 

    GameObject cube; 
    cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 
    Renderer cubeRenderer = cube.GetComponent<Renderer>(); 

    List<UnifyObject> allMaterials = unifyObj[3]; 
    foreach (UnifyObject obj in allMaterials) 
    { 
     // skip layers with no materials assigned/default 
     if (obj.Guid != Guid.Empty.ToString()) 
     { 
      // obj replaces all dashes in names with underscores hence material assets will have different names than in Rhino 
      // if layers had dashes in their names 
      string objUniqueName = obj.UniqueName.Replace("-", "_"); 
      Material m = (Material)AssetDatabase.LoadAssetAtPath("Assets/Resources/Model/Materials/" + objUniqueName + "Mat.mat", typeof(UnityEngine.Object)); 
      if (m != null) 
      { 
       OverrideMaterial(m, obj, cubeRenderer); 
       AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(m)); 
      } 
     } 
    } 
    DestroyImmediate(cube); 
} 

private static Material OverrideMaterial(Material m, UnifyObject obj, Renderer renderer) 
{ 
    renderer.material = m; 
    // set main color 
    // set transparency 
    if (obj.Transparency != "0") 
    { 
     Color newColor = Utilities.ConvertToUnityColor(obj.Diffuse, obj.Transparency); 
     renderer.sharedMaterial.SetFloat("_Mode", 3); 
     renderer.sharedMaterial.SetColor("_Color", newColor); 
    } 
    else 
    { 
     Color newColor = Utilities.ConvertToUnityColor(obj.Diffuse); 
     renderer.sharedMaterial.SetColor("_Color", newColor); 
    } 

    // set main texture 
    if (obj.DiffuseTexture != null) 
    { 
     renderer.sharedMaterial.mainTexture = Utilities.Texture2dFromPath(obj.DiffuseTexture); 
    } 

    // set bump map 
    if (obj.BumpTexture != null) 
    { 
     Texture2D bumpTexture = Utilities.Texture2dFromPath(obj.BumpTexture); 
     float strength = Convert.ToSingle("1.0"); 
     Texture2D normalBump = Utilities.CreateNormalMap(bumpTexture, strength); 
     renderer.sharedMaterial.SetTexture("_BumpMap", normalBump); 
     // need to get that value from Rhino somehow 
     renderer.sharedMaterial.SetFloat("_BumpScale", 0.3f); 
    } 

    // set metallic 
    renderer.sharedMaterial.SetFloat("_Metallic", Utilities.ConvertRange(0, 255, 0, 1, Convert.ToSingle(obj.Metallic))); 

    // set emission color 
    Color emissionColor = Utilities.ConvertToUnityColor(obj.EmissionColor); 
    renderer.sharedMaterial.SetColor("_EmissionColor", emissionColor); 
    return renderer.sharedMaterial; 
} 
} 
+0

私の解決策を試しましたか?それは動作しましたか? – Programmer

+0

明日の朝にこれをチェックします。今週末はコンピュータから離れていた。投稿していただきありがとうございます。 FYI。後でリフレッシュを呼び出すときに、重要なアセットを汚れて設定して保存しようとしましたが、それは役に立たなかった。私はキューブとトリックを試して、報告する。 – konrad

+0

Unityパーティションに10kb以上の空き容量がありますか?私はあなたがすると確信していますが、質問する必要が... –

答えて

1

あなたが材料を上書きした後、ロードされたマテリアルを割り当て、にシンプルなキューブを作成する上記の方法がうまくいかなかった場合は、うまくいくかもしれないもう一つの方法は、にある

UnityEditor.EditorUtility.SetDirty(AssetName); 
UnityEditor.AssetDatabase.SaveAssets(); 
UnityEditor.AssetDatabase.Refresh(); 

次の関数を呼び出しますキューブにRenderer.materialの代わりにキューブのRenderer.sharedMaterialを変更します。通常、sharedMaterialを変更すると元の素材が永久に変更されますが、ロードされた素材に適用されるかどうかわからない場合はAssetDatabaseです。これはあなたのOverrideMaterial関数内で行う必要があります。 GameObject.CreatePrimitive(PrimitiveType.Cube);関数は一度だけ呼び出す必要があります。

GameObject cube; 
cube = GameObject.CreatePrimitive(PrimitiveType.Cube); 

Renderer cubeRenderer = cube.GetComponent<Renderer>(); 

//Change the cube material to the material that is loaded from the disk 
cubeRenderer.material = m; 

//Now modify the shared array of the cube 
cubeRenderer.sharedMaterial.SetFloat("_Mode", 3); 
cubeRenderer.sharedMaterial.SetColor("_Color", newColor); 
//cubeRenderer.sharedMaterial. 
//cubeRenderer.sharedMaterial. 
+0

私はこれらの提案を試しましたが、私はまだ同じ結果を得ています。私はあなたが提案した解決策で私の質問を更新するでしょう、おそらく私はそれを正しく実装していません。 – konrad

関連する問題