Created
December 1, 2019 12:07
-
-
Save onotchi/38c8884b553f08d84584c3fa857e077a to your computer and use it in GitHub Desktop.
UnityEditor上でMeshにUV2-UV4を追加する。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Linq; | |
using System.Text.RegularExpressions; | |
using UnityEditor; | |
using UnityEngine; | |
namespace Onoty3D | |
{ | |
enum UVType | |
{ | |
//UV1, | |
UV2, | |
UV3, | |
UV4, | |
} | |
public class UVCreator | |
{ | |
#if UNITY_EDITOR | |
[MenuItem("Onoty3D/CreateUV2(from SkinnedMeshRenderer)")] | |
private static void CreateUV2() | |
{ | |
UVCreator.Create(UVType.UV2); | |
} | |
[MenuItem("Onoty3D/CreateUV3(from SkinnedMeshRenderer)")] | |
private static void CreateUV3() | |
{ | |
UVCreator.Create(UVType.UV3); | |
} | |
[MenuItem("Onoty3D/CreateUV4(from SkinnedMeshRenderer)")] | |
private static void CreateUV4() | |
{ | |
UVCreator.Create(UVType.UV4); | |
} | |
private static void Create(UVType uv) | |
{ | |
var hasSkinnedMesh = false; | |
var skinnedMeshRenderer = default(SkinnedMeshRenderer); | |
if (Selection.gameObjects.Length != 0) | |
{ | |
var gameObject = Selection.gameObjects[0]; | |
skinnedMeshRenderer = gameObject.GetComponentInChildren<SkinnedMeshRenderer>(); | |
if (skinnedMeshRenderer != null) | |
{ | |
hasSkinnedMesh = true; | |
} | |
} | |
//選択したオブジェクトにSkinnedMeshRendererが無かったら終了 | |
if (!hasSkinnedMesh) | |
{ | |
EditorUtility.DisplayDialog( | |
"Select SkinnedMeshRender", | |
"SkinnedMeshRenderを含むオブジェクトを選択してね!", | |
"Ok"); | |
return; | |
} | |
var path = EditorUtility.SaveFilePanel( | |
"Save File", | |
"Assets", | |
Selection.gameObjects[0].name + uv.ToString(), | |
"asset"); | |
if (string.IsNullOrEmpty(path)) | |
{ | |
return; | |
} | |
var match = Regex.Match(path, @"Assets/.*"); | |
path = match.Value; | |
//メッシュのクローンをSkinnedMeshRendererにセット | |
var mesh = skinnedMeshRenderer.sharedMesh; | |
mesh = GameObject.Instantiate(mesh); | |
var vertices = mesh.vertices; | |
var uvs = new Vector2[vertices.Length]; | |
var minX = vertices.Min(x => x.x); | |
var maxX = vertices.Max(x => x.x); | |
var minY = vertices.Min(x => x.y); | |
var maxY = vertices.Max(x => x.y); | |
var scaleX = Mathf.Abs(maxX - minX); | |
var scaleY = Mathf.Abs(maxY - minY); | |
for (int i = 0; i < uvs.Length; i++) | |
{ | |
var x = 1 - (vertices[i].x - minX) / scaleX; | |
var y = (vertices[i].y - minY) / scaleY; | |
uvs[i] = new Vector2(x, y); | |
} | |
switch (uv) | |
{ | |
case UVType.UV2: | |
mesh.uv2 = uvs; | |
break; | |
case UVType.UV3: | |
mesh.uv3 = uvs; | |
break; | |
case UVType.UV4: | |
mesh.uv4 = uvs; | |
break; | |
default: | |
break; | |
} | |
//出力 | |
AssetDatabase.CreateAsset(mesh, path); | |
skinnedMeshRenderer.sharedMesh = mesh; | |
} | |
#endif | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment