直接上代码:

using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
using System.IO;
using System.Text; public class RubbishCodeGenerator
{
static string filePath = "/RubbishCode/RubbishCode.cs";
static bool hasReturnValue;
static string returnType;
static string methodNamePrefix= "AutoGBCode";
static string className = "AutoCreateRubbish";
static List<string> usedMethodName = new List<string>(); static int methodCount = ;
static int methodLineCount = ;
/// <summary>
/// 垃圾代码类中的控制变量,用于控制垃圾方法的具体行为
/// </summary>
static string forLoopCycleCount = "forLoopCycleCount";
static string whileLoopCycleCount = "whileLoopCycleCount";
static string openLoop = "openForLoop";
static string openWhile = "openWhile";
static string openIfElse = "openIfElse"; [MenuItem("Window/垃圾代码生成器")]
static void CreateCode()
{ string realPath = Application.dataPath + filePath; var fs = new FileStream(realPath, FileMode.OpenOrCreate); if (!fs.CanWrite)
{
Debug.LogError("无法写入文件");
return;
} string data = CreateClass(false); var bytes = Encoding.UTF8.GetBytes(data);
Debug.Log("class总长度:" + bytes.Length); fs.Write(bytes, , bytes.Length); fs.Flush();
fs.Close();
} static string CreateClass(bool implementMono)
{
StringBuilder sb = new StringBuilder(); sb.Append(CreateUsing());
var str = CreateClassHead(false, className);
sb.Append(str);
sb.Append(CreateControlVariables()); for (int i = ; i < methodCount; i++)
{
int j = UnityEngine.Random.Range(, );
bool k = i % == ;
string returnValue = GetReturnValue();
sb.Append(CreateMethod(k, returnValue, methodNamePrefix, methodLineCount)); } sb.Append("\n}"); return sb.ToString();
} private static string GetReturnValue()
{
int i = UnityEngine.Random.Range(, ); switch (i)
{
case :
return "int";
case :
return "string";
case :
return "long";
case :
return "object";
case :
return "UnityEngine.Vector3";
default:
return "int";
}
} static string CreateUsing()
{
StringBuilder sb = new StringBuilder(); sb.Append("using UnityEngine;\nusing System;"); return sb.ToString();
} static string CreateClassHead(bool implementMono,string className)
{
string str = implementMono ? ":MonoBehaviour" : "";
return "\npublic static class " + className + str + "\n{";
} /// <summary>
/// 创建类的控制类变量,包含:
/// 1.for循环次数
/// 2.是否开启循环
/// 3.是否开启switch语句
/// 4.是否开启判断语句
/// </summary>
/// <returns></returns>
static string CreateControlVariables()
{
string _forLoop = "\n\tpublic static int "+forLoopCycleCount+" = 1000;";
string _openLoop = "\n\tpublic static bool "+openLoop+" = true;";
string _openWhile = "\n\tpublic static bool "+openWhile+" = true;";
string _openIfElse = "\n\tpublic static bool "+openIfElse+" = true;";
string _whileLoop = "\n\tpublic static int " + whileLoopCycleCount + " = 1000;";
return _forLoop + _openLoop + _openWhile + _openIfElse+_whileLoop;
} /// <summary>
/// 创建一个随机函数
/// </summary>
/// <param name="hasReturnValue"></param>
/// <param name="methodNamePrefix"></param>
/// <returns></returns>
static string CreateMethod(bool hasReturnValue,string returnValueType,string methodNamePrefix,int totalLine)
{
StringBuilder sb = new StringBuilder(); sb.Append(CreateMethodHead(hasReturnValue, methodNamePrefix,returnValueType)); sb.Append(CreateMethodBody(totalLine, hasReturnValue, returnValueType)); sb.Append("\n\t}"); return sb.ToString();
} /// <summary>
/// 创建函数头部,格式为 public 返回值 函数名(){},需要注意这些函数全部没有参数名,方便调用
/// </summary>
/// <param name="hasReturnValue"> 是否有返回值</param>
/// <param name="methodNamePrefix">如果有返回值 返回值类型</param>
/// <returns></returns>
static string CreateMethodHead(bool hasReturnValue,string methodNamePrefix,string returnType)
{
var methodName = methodNamePrefix + RandomMethodName();
var returnStr = hasReturnValue ? returnType : "void";
return "\n\n\tpublic " + returnStr + " " + methodName + "()\n\t{";
} /// <summary>
/// 创建函数体,为包含在函数{}内部的代码,由几部分组拼而成
/// </summary>
/// <param name="needToRun">需不需函数运行,如果不需要,直接return</param>
/// <param name="totalLine">总共需要多少行代码</param>
/// <param name="hasReturnValue">是否有返回值</param>
/// <param name="ReturnValueType">如果有返回值,返回值的类型</param>
/// <returns></returns>
static string CreateMethodBody(int totalLine,bool hasReturnValue, string ReturnValueType)
{
string returnStatement = CreateReturnStatement(ReturnValueType,);//返回语句 if (totalLine<)
{
totalLine = ;
}
int totalCount = UnityEngine.Random.Range(, totalLine); StringBuilder sb = new StringBuilder(); for (int i = ; i < totalCount; i++)
{
int j = UnityEngine.Random.Range(, );
int k = UnityEngine.Random.Range(, ); switch (j)
{
case :
sb.Append(CreateForLoop(CreateSingleRandomStatement(k)));
break;
case :
int m = UnityEngine.Random.Range(, );
sb.Append(CreateIfElse(CreateSingleRandomStatement(k),CreateSingleRandomStatement(m)));
break;
case :
sb.Append(CreateWhile(CreateSingleRandomStatement(k)));
break;
default:
break;
}
} sb.Append(returnStatement); return sb.ToString(); } /// <summary>
/// 创建For循环,其中循环次数为类的静态全局变量控制
/// int forLoops:控制循环次数
/// bool openLoop: 是否开启循环
/// </summary>
/// <param name="statementInForLoop">要放入for循环的具体语句</param>
/// <returns></returns>
static string CreateForLoop(string statementInForLoop)
{
return "\n\n\t\tif("+ openLoop + ")\n\t\t{\n\t\t\tfor(int i = 0;i<"+ forLoopCycleCount + ";i++)\n\t\t\t{\n\t\t\t\t"+ statementInForLoop + "\n\t\t\t}\n\t\t}";
} /// <summary>
/// 创建 if-else判断
/// </summary>
/// <param name="ifString">if语句里面要执行的东西</param>
/// <param name="elseString">else语句里面要执行的东西</param>
/// <returns></returns>
static string CreateIfElse(string ifString,string elseString)
{
return "\n\n\t\tif("+ openIfElse + ")\n\t\t{\n\t\t\t"+ ifString + "\n\t\t}\n\t\telse\n\t\t{\n\t\t\t"+ elseString + "\n\t\t}";
} /// <summary>
/// 创建while循环
/// </summary>
/// <param name="whileStr">while循环中要执行的东西</param>
/// <returns></returns>
static string CreateWhile(string whileStr)
{
return "\n\n\t\tif("+ openWhile + ")\n\t\t{\n\t\t\tint i =0;\n\t\t\twhile(i<"+ whileLoopCycleCount + ")\n\t\t\t{\n\t\t\t\t"+ whileStr + "\n\t\t\t}\n\t\t}";
} /// <summary>
/// 创建返回语句
/// </summary>
/// <param name="returnValueType"></param>
/// <param name="suojin"></param>
/// <returns></returns>
static string CreateReturnStatement(string returnValueType,int suojin)
{
return "\n"+GetSuoJin(suojin)+"return default("+ returnValueType + ");";
} /// <summary>
/// 获取缩进的字符串
/// </summary>
/// <param name="suojin"></param>
/// <returns></returns>
static string GetSuoJin(int suojin)
{
if (suojin<=)
{
return "";
} string suojinstr = string.Empty; for (int i = ; i < suojin; i++)
{
suojinstr += "\t";
} return suojinstr;
} /// <summary>
/// 随机函数名字
/// </summary>
/// <returns></returns>
static string RandomMethodName()
{
int methodLength = UnityEngine.Random.Range(, ); StringBuilder sb = new StringBuilder(); for (int i = ; i < methodLength; i++)
{
sb.Append(GetLetter(UnityEngine.Random.Range(, )));
} if (usedMethodName.Contains(sb.ToString()))
{
return RandomMethodName();
}
else
{
usedMethodName.Add(sb.ToString());
return sb.ToString();
} } static string CreateSingleRandomStatement(int index)
{
switch (index)
{
case :
return "int neverMSDFA = UnityEngine.Random.Range(0,100);";
case :
return "UnityEngine.Debug.Log(\"HELLO WORLD\");";
case :
return "var str = \"Hello world\";";
default:
return "";
} } static string GetLetter(int index)
{
switch (index)
{
case :
return "A";
case :
return "B";
case :
return "C";
case :
return "D";
case :
return "E";
case :
return "F";
case :
return "G";
case :
return "H";
case :
return "I";
case :
return "J";
case :
return "K";
case :
return "L";
case :
return "M";
case :
return "N";
case :
return "O";
case :
return "P";
case :
return "Q";
case :
return "R";
case :
return "S";
case :
return "T";
case :
return "U";
case :
return "V";
case :
return "W";
case :
return "X";
case :
return "Y";
case :
return "Z";
default:
return ""; }
} }

最新文章

  1. PHP webservice的使用
  2. WebAPi添加常用扩展方法及思维发散
  3. Android MVP + 泛型,实现了友好VP交互及Activity潜在的内存泄露的优化
  4. IPMI
  5. mybatis 3.2.8 + log4j2.0.2 控制台输出sql语句
  6. Hinet 日本数据处理流程
  7. nyoj 168 房间安排(区间覆盖)
  8. 使用XHProf分析PHP性能瓶颈(二)
  9. java实现注册的短信验证码
  10. spring 普通类注入为null,通过自定义SpringUtils解决
  11. 搭建servlet+jsp环境
  12. MySql的创建时间和修改时间
  13. 建立Heapster Influxdb Grafana集群性能监控平台
  14. Flask蓝图
  15. Nanopi2基本使用
  16. 【腾讯云的1001种玩法】 Laravel 整合微视频上传管理能力,轻松打造视频App后台
  17. C++ Com控件调用
  18. suoi63 树与路径 (倍增lca)
  19. dev ChartControl 备忘
  20. (欧拉公式 很水) Coprimes -- sgu -- 1002

热门文章

  1. SpringMVC入门和常用注解
  2. Linux入门-程序开发
  3. 011_go语言中的range遍历
  4. 曲线生成与求交—Bezier曲线
  5. CentOS7安装MinIO教程,并在C#客户端WPF中实现监控上传进度
  6. PYTHON实战完整教程1-配置VSCode开发环境
  7. Python 进程与多线程
  8. pytorch载入模型的参数总是变化,比如说某个conv(3,3)kernel的几个参数总是变化:
  9. 2020-07-09:mysql如何开启慢查询?
  10. C#算法设计排序篇之06-堆排序(附带动画演示程序)