unity随机生成乱码图片并保存本地,各项参数均可调整
  v5bEezpf7PPs 2023年11月02日 55 0


@TOC

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

前言

最近有个小需求,要生成随机的乱码图片,用于ar的识别,于是我写了这个小demo,有需要的小伙伴可以拿去用,我也是借此留个备份。

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">


一、基础元素库的随机

二、生成位置、生成数量的随机

三、遮挡块位置的随机

注意

图片存放位置是Application.persistentDataPath; 按下快捷键F1,即可随机生成一张图片,图片的大小和game视窗的大小是一样的。

<hr style=" border:solid; width:100px; height:1px;" color=#000000 size=1">

总结:最后上全部代码

完整的unitypackage包也可以自行下载,环境是用的unity2019.4 下载package包,点击这里https://download.csdn.net/download/Wrinkle2017/16635247

/*生成图片的大小等于game视窗的大小*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Random = UnityEngine.Random;

/// <summary>
/// 随机生成唯一特征码图片
/// </summary>
public class SetIDUINormalBig : MonoBehaviour
{
    /// <summary>
    /// 特征元素的集合
    /// </summary>
    public List<GameObject> UiList;

    /// <summary>
    /// 中间用于遮挡的方块
    /// </summary>
    public GameObject wardImages;
    /// <summary>
    /// 随机遮挡的方块
    /// </summary>
    public Transform randomImage;
    public GameObject canvas;
    private GameObject setTargets;
    private List<Vector3> setTransformList, nowTranspformList;
    private List<GameObject> instantiateList;
    private int nameNum = 0, listCount;
    private bool ifCreat, ifScreen;
    /// <summary>
    /// 生成图片的数量
    /// </summary>
    private int creatPictureNum;
    private float nowTime;

    private void Start()
    {
        ifCreat = false;
        ifScreen = false;
        creatPictureNum = 0;
        CreatPositionList();
        listCount = setTransformList.Count;
    }

    private void Update()
    {
        //按下F1键,随机创建一张图片
        if (Input.GetKeyDown(KeyCode.F1))
        {
            ifCreat = true;
            creatPictureNum = 0;
        }

        if (ifCreat)
        {
            nowTime += Time.deltaTime;
            if (nowTime >= 0.2 && !ifScreen)
            {
                //随机遮挡的方块位置
                randomImage.localPosition=new Vector3(Random.Range(-360,360),Random.Range(-200,200),0);
                //开始生成
                CreatPositionList();
                listCount = setTransformList.Count;
                nowTranspformList = setTransformList;
                listCount = nowTranspformList.Count;
                if (nameNum > 0)
                {
                    int num = instantiateList.Count;
                    for (int i = 0; i < num; i++)
                    {
                        Destroy(instantiateList[i]);
                    }
                }

                instantiateList = new List<GameObject>();

                //生成特征点数量
                for (int i = 0; i < Random.Range(30,50); i++)
                {
                    nowTranspformList = setTransformList;
                    listCount--;
                    setTargets = Instantiate(UiList[Random.Range(0, UiList.Count)]);
                    setTargets.transform.eulerAngles=new Vector3(0,0,setTargets.transform.eulerAngles.z+Random.Range(-15,15));
                    setTargets.transform.parent = canvas.transform;
                    RandomVector3();
                    instantiateList.Add(setTargets);
                    wardImages.transform.SetAsLastSibling();
                }

                // int num2 = instantiateList.Count;
                // for (int i = 0; i < num2; i++)
                // {
                //     if (instantiateList[i].transform.localPosition.x > -200 && instantiateList[i].transform.localPosition.x < 200 &&
                //         instantiateList[i].transform.localPosition.y > -60 && instantiateList[i].transform.localPosition.y < 60)
                //     {
                //         Debug.Log(instantiateList[i].transform.position.x + "+++" + instantiateList[i].transform.position.y);
                //     }
                // }

                ifScreen = true;
            }

            if (nowTime >= 0.4)
            {
                ScreenCapture.CaptureScreenshot(Application.persistentDataPath + "/000" + nameNum + ".jpg");
                Debug.Log("生成的图片存放的位置:"+Application.persistentDataPath);
                nameNum++;
                nowTime = 0;
                ifScreen = false;
                creatPictureNum++;
                //生成图片数量
                if (creatPictureNum > 0)
                {
                    ifCreat = false;
                }
            }
        }
    }

    /// <summary>
    /// 创建位置库
    /// </summary>
    public void CreatPositionList()
    {
        setTransformList = new List<Vector3>();
        for (int i = 0; i < 15; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                setTransformList.Add(new Vector3(-300 + i * Random.Range(40,55), -150 + j * Random.Range(40,50), 0));
            }
        }
    }

    /// <summary>
    /// 随机vector3
    /// </summary>
    public void RandomVector3()
    {
        int randomNum = Random.Range(0, listCount + 1);
        Vector3 randomPos = setTransformList[randomNum];
        setTargets.transform.localPosition = randomPos;
        setTransformList.Remove(randomPos);
    }
}

欢迎大佬多多来给萌新指正,欢迎大家来共同探讨。


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

v5bEezpf7PPs