Unity 图片、物体...按照图片文字排版

一、Unity 图片按照图片文字排版

1.准备好一张白底蓝字图片,尺寸不宜过大(字可以是其他颜色)

蓝底白字

2.获取文字图片

 //文字排版     
  //文字排列
 public Texture2D wordTexture; // 文字图片的纹理
 private List<Vector3> wordPosList = new List<Vector3>(); // 文字位置列表
 public string wordPath = “替换成自己的文字图片路径”;
star(){
 wordTexture = GetTexture(wordPath);
  if (wordPosList.Count == 0)
 {
     wordPosList.AddRange(GetWordPosByTexture(wordTexture));
 }
}
  private Texture2D GetTexture(string path)
  {
      List<Texture2D> textureList = new List<Texture2D>();
      textureList = LoadImages(path);
      if (textureList == null || textureList.Count == 0)
      {
          Debug.LogError("文字图片缺失");
          return null;
      }
      return textureList[0];
  }
   public List<Texture2D> LoadImages(string imagePath)
   {
       List<Texture2D> texList = new List<Texture2D>();
       List<string> filePaths = new List<string>();
       string imgtype = ".bmp|.jpg|.png";
       string[] ImageType = imgtype.Split('|');
       if (Directory.Exists(imagePath))
       {
           DirectoryInfo direc = new DirectoryInfo(imagePath);
           for (int i = 0; i < ImageType.Length; i++)
           {
               //获取d盘中a文件夹下所有的图片路径            
               FileInfo[] files = direc.GetFiles("*", SearchOption.AllDirectories);

               for (int j = 0; j < files.Length; ++j)
               {
                   if (files[j].Name.EndsWith(ImageType[i]))
                   {
                       filePaths.Add(imagePath + files[j].Name);
                   }
               }

           }
       }
       for (int i = 0; i < filePaths.Count; i++)
       {
           Texture2D tx = new Texture2D(100, 100);
           tx.LoadImage(getImageByte(filePaths[i]));
           texList.Add(tx);
       }
       return texList;
   }

   public byte[] getImageByte(string imagePath)
   {
       FileStream files = new FileStream(imagePath, FileMode.Open);
       byte[] imgByte = new byte[files.Length];
       files.Read(imgByte, 0, imgByte.Length);
       files.Close();
       return imgByte;
   }

3.获取像素点

 private int wordWidthStep = 20;
 private int wordHeightStep = 20;
 List<Vector3> GetWordPosByTexture(Texture2D texture)
 {
     List<Vector3> posList = new List<Vector3>();
     int num = 0;

     if (texture != null)
     {
         int width = texture.width ;
         int height = texture.height ;
         Color[] colors = texture.GetPixels();

         // 遍历像素点
         for (int i = 0; i < height; i += wordHeightStep)
         {
             for (int j = 0; j < width; j += wordWidthStep)
             {
                 int index = i * width + j;
                 if (nearBlack(colors[index]))
                 {
                     num++;
                     Vector3 itemPos = new Vector3();

                     // 修正 X 和 Y 的计
                     itemPos.x = (j - (width / 2))* ScaleValue*1.5f;   // 左右坐标
                     itemPos.y = (i - (height / 2))* ScaleValue*1.5f;  // 从底部开始计算
                     itemPos.z = 0;

                     posList.Add(itemPos);
                 }
             }
         }

         Debug.Log("数量数量wordPoint:" + num);
     }

     return posList;
 }
 bool nearBlack(Color color)
 {
     if (color.r < 0.8f || color.g < 0.8f || color.b < 0.8f)
         return true;
     else
         return false;
    

4、图片按照文字像素点排列

  IEnumerator SetPositionToItem(List<Vector3> pos)
  {
      for (int i = 0; i < pos.Count; i++)
      {
          Transform child = ListObj.transform.GetChild(i);
          GameObject Obj = Instantiate(child.gameObject);           
          Obj.transform.SetParent(ListObj.transform, false);
          // 设置位置
          RectTransform rectTransform = Obj.GetComponent<RectTransform>();
          rectTransform.anchoredPosition = pos[i];
          rectTransform.localScale = Vector3.one * 0.24f / ScaleValue;
          // 将复制体存储到列表
          clonedObjects.Add(Obj); 
          yield return null;
      }

      NoActionSaveFlag = false;
      yield return null;
  }

5、运行结果

图片按照文字像素排版

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值