Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created April 29, 2013 03:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/5479504 to your computer and use it in GitHub Desktop.
Save tsubaki/5479504 to your computer and use it in GitHub Desktop.
Unityでテクスチャサイズと同等のパネルを作るデモ。 これでパネルを作る場合、カメラの設定をorthographic かつ sizeをテクスチャのheightの半分にすると、大体大きさが一致する。
using UnityEditor;
using UnityEngine;
using System.Collections;
public class PanelMaker
{
private static readonly string ModellNameFormat = "panel({0},{1})";
private static readonly string MaterialNameFormat = "{0}.mat";
private static readonly string ShaderNameFormat = "Unlit/Transparent";
private static readonly string PanelNameFormat = "{0}";
private static readonly string modelDir = "Assets/Model/";
private static readonly string materialDir = "Assets/Materials/";
public Material CreateMat(Texture2D texture)
{
string matName = string.Format(MaterialNameFormat, texture.name );
string assetPath = materialDir + matName;
Material mat = AssetDatabase.LoadAssetAtPath( assetPath , typeof(Material) ) as Material;
if( mat != null )
return mat;
mat = new Material (Shader.Find (ShaderNameFormat));
mat.mainTexture = texture;
mat.name = matName;
if( !System.IO.Directory.Exists( materialDir ) )
System.IO.Directory.CreateDirectory( materialDir );
AssetDatabase.CreateAsset (mat, assetPath);
return mat;
}
public Mesh CreateMesh (int x, int y)
{
string assetPath = string.Format (modelDir + ModellNameFormat + ".asset", x, y);
Mesh mesh = AssetDatabase.LoadAssetAtPath(assetPath, typeof( Mesh )) as Mesh;
if( mesh != null )
return mesh;
mesh = new Mesh();
mesh.name = string.Format(ModellNameFormat, x, y);
float vx = x * 0.5f, vy = y * 0.5f;
mesh.vertices = new Vector3[]{
Vector3.left * vx + Vector3.up * vy,
Vector3.right * vx + Vector3.up * vy,
Vector3.right * vx + Vector3.down * vy,
Vector3.left * vx + Vector3.down * vy
};
mesh.triangles = new int[]{
0, 1, 2,
2, 3, 0
};
mesh.uv = new Vector2[]{
Vector2.up,
Vector2.right + Vector2.up,
Vector2.right,
Vector2.zero
};
mesh.RecalculateNormals ();
mesh.RecalculateBounds ();
mesh.Optimize ();
if( !System.IO.Directory.Exists( modelDir ) )
System.IO.Directory.CreateDirectory(modelDir);
AssetDatabase.CreateAsset (mesh, assetPath);
return mesh;
}
[MenuItem("Assets/Create/Panel with selected texture %g")]
public static void CreateObject ()
{
PanelMaker panel = new PanelMaker();
foreach(Object obj in Selection.objects)
{
if( (obj is Texture) == false )
{
Debug.LogWarning(string.Format("{0} is not texture", obj.name));
continue;
}
Texture2D tex2D = (Texture2D)obj;
GameObject model = new GameObject (string.Format(PanelNameFormat, tex2D.name));
MeshRenderer renderer = model.AddComponent<MeshRenderer> ();
renderer.material = panel.CreateMat(tex2D);
renderer.castShadows = false;
renderer.receiveShadows = false;
MeshFilter meshFilter = model.AddComponent<MeshFilter> ();
Mesh mesh = panel.CreateMesh(tex2D.width, tex2D.height);
meshFilter.sharedMesh = mesh;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment