I need to use some custom materials (in my own material file format) to apply to my game objects. Following this tutorial:
http://80hdgames.com/chris/?p=100#comment-1410
I found a solution that works fine playing my game in the Unity game player (hitting the “play” button), but when I build the standalone .exe, it crashes at certain point of my code:
// on OpenGL ES there is no way to query texture extents from native texture id
#if (UNITY_IPHONE || UNITY_ANDROID) && !UNITY_EDITOR
#define UNITY_GLES_RENDERER
#endif
using UnityEngine;
using System.Collections;
using System;
using System.Runtime.InteropServices;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
public static class CustomAssetUtility
{
public static void CreateAsset()
{
MyMaterial asset = ScriptableObject.CreateInstance<MyMaterial> ();
string path = AssetDatabase.GetAssetPath (Selection.activeObject);
if (path == "")
{
path = "Assets";
}
else if (Path.GetExtension (path) != "")
{
path = path.Replace (Path.GetFileName (AssetDatabase.GetAssetPath (Selection.activeObject)), "");
}
asset.path = EditorUtility.OpenFilePanel("Load MYMAT file","","mymat");
string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath (path + "/MYMAT " + Path.GetFileNameWithoutExtension(asset.path) + ".asset");
AssetDatabase.CreateAsset (asset, assetPathAndName);
AssetDatabase.SaveAssets ();
EditorUtility.FocusProjectWindow ();
Selection.activeObject = asset;
}
}
[System.Serializable]
#endif
public class MyMaterial : ScriptableObject
{
public string path; // path to the .mymat file
}
#if UNITY_EDITOR
public class CreateMyMaterial
{
[MenuItem("Assets/Create/My Material")]
public static void CreateAsset()
{
CustomAssetUtility.CreateAsset();
}
}
#endif
public class UseRenderingPlugin : MonoBehaviour
{
public MyMaterial mymat;
IEnumerator Start () {
yield return StartCoroutine("CallPluginAtEndOfFrames");
}
private IEnumerator CallPluginAtEndOfFrames()
{
int prev = -1;
while (true) {
// Wait until all frame rendering is done
yield return new WaitForEndOfFrame();
int a = (int)Math.Floor(Time.timeSinceLevelLoad);
if( a > prev )
{
prev = a;
Debug.LogWarning("A warning test!");
// Material
if( mymat.path != "" ) -- that line goes wrong and the rest of the code is not executed!!!!
{
// do my stuff
}
// ...
}
// ...
}
}
}
Should I init that “mymat” variable for the standalone build? I guess I have to retrieve the MyMaterial field using something like “GetComponent”.