顯示具有 C# 標籤的文章。 顯示所有文章
顯示具有 C# 標籤的文章。 顯示所有文章

2013年8月29日 星期四

[Unity] 更改Asset的Import Settings後,執行ImportAsset以正確更新

  當利用script進行批次的Import Settings設定時,需注意設定參數完成之後得要重新Import Asset才能夠讓Unity成功判讀到你的更改,不然會發生在Inspector中的資訊是對的,但執行Save Project後卻還是沒有正確寫入到Prefab或meta檔案之中。

舉例:
// 設定貼圖檔案的圖片壓縮格式
textureImporter.textureFormat = TextureImporterFormat.PVRTC_RGBA4;
// 設定了Import Settings之後再使用AssetDatabase.ImportAsset進行asset的更新
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate );

參考資料

2013年7月30日 星期二

[Unity] Assign Texture and Material (Editor Script)

複製已存在材質並該改貼圖後賦予至模型

// create material
Material newMaterial = new Material( Shader.Find("Diffuse"));
// assign shader
newMaterial.shader = sourceMaterial.shader;
// copy properties from existing material
newMaterial.CopyPropertiesFromMaterial(sourceMaterial);
// create texture from asset(asset path)
Texture texture = (Texture)AssetDatabase.LoadAssetAtPath( textureRelativePath, typeof(Texture));
// assign texture to material
newMaterial.SetTexture("_MainTex", texture);
// create material asset
AssetDatabase.CreateAsset(newMaterial, newMaterialAssetRelativePath);
// assign material to gameObject
AssignMaterial( gameObject, newMaterial );

在建立texture時需注意,在editor部份可以使用AssetDatabase.LoadAssetAtPath,而runtime則需使用Resource.Load

2013年4月2日 星期二

Unity - Editor Script Coding - 編寫Editor Script之參考資料

入門站:
資料:

參考文章蒐集:
  • http://forum.unity3d.com/threads/92148-need-help-with-texture-import-setting-script

零散紀錄:
  • 取得Type
    GameObject.GetType().Name
  • 取得某個選擇中的Asset的路徑(亦可作用於目錄)
    AssetDatabase.GetAssetPath(OBJECT)
  • 取得 某個Asset檔案 內的所有元件
    AssetDatabase.LoadAllAssetsAtPath("AssetPath")