유 니 티 컷 이 여러 장의 그림 으로 변환 되 었 습 니 다.

2941 단어 unity절단 앨범
본 논문 의 사례 는 유 니 티 절단 그림 집 을 여러 장의 그림 으로 전환 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
이것 은 인터넷 에서 볼 수 있 는 도구 입 니 다.유 니 티 에서 그림 을 여러 장의 형식 으로 자 른 후에 이 sprite 를 한 장의 그림 으로 바 꾸 는 데 사 용 됩 니 다.바로 절단 속도 가 너무 느 리 고 그림 속 의 그림 이 많 을 때 일부 그림 을 잃 어 버 리 며 시간 이 있 으 면 본인 은 이 블 로 그 를 개선 하고 수정 할 것 입 니 다.
1.자 를 그림 을 먼저 선택 하고 texture type 을 default 로 선택 하고 Advanced 의 read/Write Enabled 를 선택 합 니 다.
2.texture type 을 sprite(2D and UI)로 변경 하고 Sprite mode 를 Multiple 로 선택 하여 apply 합 니 다.
3.Sprite Editor 를 누 르 면 그림 을 자 릅 니 다.
4.그림 집 오른쪽 단 추 를 선택 하고 imageslicer 를 선택 하여 process to Sprites 를 선택 합 니 다.
5.절단 이 완 료 될 때 까지 기 다 립 니 다.
스 크 립 트 는 다음 과 같 습 니 다:

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
/// <summary>
///   
/// </summary>
public static class ImageSlicer
{
  [MenuItem("Assets/ImageSlicer/Process to Sprites")]
  static void ProcessToSprite()
  {
    Texture2D image = Selection.activeObject as Texture2D;//       
    string rootPath = Path.GetDirectoryName(AssetDatabase.GetAssetPath(image));//      
    string path = rootPath + "/" + image.name + ".PNG";//      


    TextureImporter texImp = AssetImporter.GetAtPath(path) as TextureImporter;//      


    AssetDatabase.CreateFolder(rootPath, image.name);//     


    foreach (SpriteMetaData metaData in texImp.spritesheet)//     
    {
      Texture2D myimage = new Texture2D((int)metaData.rect.width, (int)metaData.rect.height);

      //abc_0:(x:2.00, y:400.00, width:103.00, height:112.00)
      for (int y = (int)metaData.rect.y; y < metaData.rect.y + metaData.rect.height; y++)//Y   
      {
        for (int x = (int)metaData.rect.x; x < metaData.rect.x + metaData.rect.width; x++)
          myimage.SetPixel(x - (int)metaData.rect.x, y - (int)metaData.rect.y, image.GetPixel(x, y));
      }


      //     EncodeToPNG    
      if (myimage.format != TextureFormat.ARGB32 && myimage.format != TextureFormat.RGB24)
      {
        Texture2D newTexture = new Texture2D(myimage.width, myimage.height);
        newTexture.SetPixels(myimage.GetPixels(0), 0);
        myimage = newTexture;
      }
      var pngData = myimage.EncodeToPNG();


      //AssetDatabase.CreateAsset(myimage, rootPath + "/" + image.name + "/" + metaData.name + ".PNG");
      File.WriteAllBytes(rootPath + "/" + image.name + "/" + metaData.name + ".PNG", pngData);
      //         
      AssetDatabase.Refresh();
    }
  }
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기