/**
* Editor Wizard for easily managing global defines in Unity
* Place in Assets/Editor folder, or if you choose to place elsewhere
* be sure to also modify the DEF_MANAGER_PATH constant.
* @khenkel
*/

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class DefineManager : EditorWindow
{
const string DEF_MANAGER_PATH = "Assets/Editor/DefineManager.cs";

enum Compiler
{
CSharp,
Editor,
UnityScript,
Boo
}
Compiler compiler = Compiler.Editor;

// http://forum.unity3d.com/threads/93901-global-define/page2
// Do not modify these paths
const int COMPILER_COUNT = 4;
const string CSHARP_PATH = "Assets/smcs.rsp";
const string EDITOR_PATH = "Assets/gmcs.rsp";
const string UNITYSCRIPT_PATH = "Assets/us.rsp";
const string BOO_PATH = "Assets/boo.rsp";

List<string> csDefines = new List<string>();
List<string> booDefines = new List<string>();
List<string> usDefines = new List<string>();
List<string> editorDefines = new List<string>();

[MenuItem("Window/Define Manager")]
public static void OpenDefManager()
{
EditorWindow.GetWindow<DefineManager>(true, "Global Define Manager", true);
}

void OnEnable()
{
csDefines = ParseRspFile(CSHARP_PATH);
usDefines = ParseRspFile(UNITYSCRIPT_PATH);
booDefines = ParseRspFile(BOO_PATH);
editorDefines = ParseRspFile(EDITOR_PATH);
}

List<string> defs;
Vector2 scroll = Vector2.zero;
void OnGUI()
{
Color oldColor = GUI.backgroundColor;

GUILayout.BeginHorizontal();
for(int i = 0; i < COMPILER_COUNT; i++)
{
if(i == (int)compiler)
GUI.backgroundColor = Color.gray;

GUIStyle st;
switch(i)
{
case 0:
st = EditorStyles.miniButtonLeft;
break;
case COMPILER_COUNT-1:
st = EditorStyles.miniButtonRight;
break;
default:
st = EditorStyles.miniButtonMid;
break;
}

if(GUILayout.Button( ((Compiler)i).ToString(), st))
compiler = (Compiler)i;

GUI.backgroundColor = oldColor;
}
GUILayout.EndHorizontal();

switch(compiler)
{
case Compiler.CSharp:
defs = csDefines;
break;

case Compiler.Editor:
defs = editorDefines;
break;

case Compiler.UnityScript:
defs = usDefines;
break;

case Compiler.Boo:
defs = booDefines;
break;
}

GUILayout.Label(compiler.ToString() + " User Defines");

scroll = GUILayout.BeginScrollView(scroll);
for(int i = 0; i < defs.Count; i++)
{
GUILayout.BeginHorizontal();

defs[i] = EditorGUILayout.TextField(defs[i]);

GUI.backgroundColor = Color.red;
if(GUILayout.Button("x", GUIStyle.none, GUILayout.MaxWidth(18)))
defs.RemoveAt(i);
GUI.backgroundColor = oldColor;

GUILayout.EndHorizontal();

}

GUILayout.Space(4);

GUI.backgroundColor = Color.cyan;
if(GUILayout.Button("Add"))
defs.Add("NEW_DEFINE");

GUILayout.EndScrollView();

GUILayout.BeginHorizontal();
GUI.backgroundColor = Color.green;
if( GUILayout.Button("Apply") )
{
SetDefines(compiler, defs);
AssetDatabase.ImportAsset(DEF_MANAGER_PATH, ImportAssetOptions.ForceUpdate);
OnEnable();
}

GUI.backgroundColor = Color.red;
if(GUILayout.Button("Apply All", GUILayout.MaxWidth(64)))
for(int i = 0; i < COMPILER_COUNT; i++)
{
SetDefines((Compiler)i, defs);
AssetDatabase.ImportAsset(DEF_MANAGER_PATH, ImportAssetOptions.ForceUpdate);
OnEnable();
}

GUILayout.EndHorizontal();
GUI.backgroundColor = oldColor;
}

void SetDefines(Compiler compiler, List<string> defs)
{
switch(compiler)
{
case Compiler.CSharp:
WriteDefines(CSHARP_PATH, defs);
break;

case Compiler.UnityScript:
WriteDefines(UNITYSCRIPT_PATH, defs);
break;

case Compiler.Boo:
WriteDefines(BOO_PATH, defs);
break;

case Compiler.Editor:
WriteDefines(EDITOR_PATH, defs);
break;
}
}

List<string> ParseRspFile(string path)
{
if(!File.Exists(path))
return new List<string>();

string[] lines = File.ReadAllLines(path);
List<string> defs = new List<string>();

foreach(string cheese in lines)
{
if(cheese.StartsWith("-define:"))
{
defs.AddRange( cheese.Replace("-define:", "").Split(';') );
}
}

return defs;
}

void WriteDefines(string path, List<string> defs)
{
if(defs.Count < 1 && File.Exists(path))
{
File.Delete(path);

if(File.Exists(path + ".meta"))
File.Delete(path + ".meta");

AssetDatabase.Refresh();
return;
}

StringBuilder sb = new StringBuilder();
sb.Append("-define:");

for(int i = 0; i < defs.Count; i++)
{
sb.Append(defs[i]);
if(i < defs.Count-1) sb.Append(";");
}

using (StreamWriter writer = new StreamWriter(path, false))
{
writer.Write(sb.ToString());
}
}
}

unity3d DefineManager 全局宏定义的更多相关文章

  1. SW4STM32 全局宏定义

    /************************************************************************************ * SW4STM32 全局宏 ...

  2. android C/C++ source files 全局宏定义 .

    \system\core\include\arch\linux-arm AndroidConfig.h * ============================================== ...

  3. 项目中常用的全局宏定义#define

    一 关于屏幕大小 #pragma mark - 屏幕宽高 #define SCREEN_BOUNDS ([UIScreen mainScreen].bounds) #define SCREEN_WID ...

  4. Qt全局宏和变量

    1.  Qt 全局宏定义 Qt版本号: QT_VERSION :  (major << 16) + (minor << 8) + patch 检测版本号: QT_VERSION ...

  5. Object_C 定义全局宏的颜色时,报“Expected identifier”的错误

    在定义全局颜色宏的时候,为了整齐把空格删了,写在了同一行里,调用的时候,出错提示“Expected identifier”,如下: 如果宏定义如上那样的话,在调用的时候,会出现如下的问题: 百思不得解 ...

  6. 如何为Swift进行宏定义

    这阵子一直在自学Swift, 因为之前iOS的开发一直用Objective-C, 所以习惯了C语言那种宏定义方式, Swift作为一款更加安全的语言, 放弃了C语言中的宏定义, 有效的防止预编译时代码 ...

  7. App开发流程之通用宏定义及头文件

    工欲善其事,必先利其器. 在正式实现各种炫酷的功能和UI前,做好准备工作是提高后续开发效率的必经之路. 所以,这个系列,我不是在各种堆技术,更关注的是“兵马动”之前的“粮草行”,有些繁琐,但当清晰理出 ...

  8. Obective-C之宏定义

    优(dan)美(teng)的前奏 宏定义这个东东,估计大家在代码中应该天天用吧. 在我刚刚做的一个项目中,各种往代码里码“#define”这种预处理指令. 什么动画时长啊,cell高度啊,cell的个 ...

  9. Swift开发之 ---- Swift宏定义

    swift中没有了#Define这种宏定义了,可以用let来声明常量来取代,判断当前系统版本 let IS_IOS7 = (UIDevice.currentDevice().systemVersion ...

随机推荐

  1. scrapy 的 selector 练习

    网页结构: <html> <head> <base href='http://example.com/' /> <title>Example websi ...

  2. 每日一SQL-善用DATEADD和DATEDIFF

    转自:http://www.dotblogs.com.tw/lastsecret/archive/2010/10/04/18097.aspx 上個星期去Tech-Day聽了幾場有趣的課,其中一堂是楊志 ...

  3. JS调用Silverlight方法拾遗

    在最近做的物联网项目中,需要利用封装过的Silverlight刻度控件显示温度,湿度,二氧化碳浓度等值.由于最新的数据是通过js ajax获取的,所以需要把这些数据传递给silverlight显示,这 ...

  4. Java系列:Collection.toArray用法研究

    该方法的签名如下: <T> T[] Collection.toArray(T[] arrayToFill); 这里想验证两个问题: 1)arrayToFill什么时候会被填充: 2)arr ...

  5. [CareerCup] 9.5 Permutations 全排列

    9.5 Write a method to compute all permutations of a string. LeetCode上的原题,请参加我之前的博客Permutations 全排列和P ...

  6. 网络封包分析工具Charles使用

    网址:http://www.charlesproxy.com/ 截取网络封包的工具. 简介 Charles是在Mac下常用的截取网络封包的工具,在做iOS开发时,我们为了调试与服务器端的网络通讯协议, ...

  7. 20145215《Java程序设计》第2周学习总结

    20145215<Java程序设计>第二周学习总结 教材学习内容总结 Java语言中的很多基本语法都和C语言类似,在这里我总结一下Java中的基本语法: 标识符: 标识符是程序中自定义的一 ...

  8. 系统安全扫描工具(appscan)的扫描类型小记

    扫描分类 不同场景需要使用不同方式的扫描类型.不能盲目的.暴力的去折腾. 自动扫描 刚开始扫描的时候适合用这种方式.有助于,理解整个网站的结构. 需要注意的是:去伪静态和业务冗余 伪静态 url结构相 ...

  9. 浅析Javascript

    Javascript是一种脚本语言,从出生就被唾弃,一开始人们使用它只是为了解决诸如页面数据校验之类的问题.它基于prototype的面向对象实现一度被认为很丑很难用,甚至很多身处一线Web开发者都不 ...

  10. Java学习笔记(十八)——Java DTO

    [前面的话] 在和技术人员的交流中,各种专业术语会出现,每次都是默默的记录下出现的术语,然后再去网上查看是什么意思.最近做项目,需要使用到DTO,然后学习一下吧. 这篇文章是关于Java DTO的,选 ...